62,025
社区成员
发帖
与我相关
我的任务
分享
uploadFile.SaveAs(uploadpath + picname);
AddWaterPic(pathbase + picname, "/pic/watermark.png", pathbase);
//不
public void AddWaterPic(string strPath, string strPathSyp, string newpath)
{
strPath = System.Web.HttpContext.Current.Server.MapPath(strPath);
strPathSyp = System.Web.HttpContext.Current.Server.MapPath(strPathSyp);
string newpic = newpath + "tmp2" + strPath.Remove(0, strPath.LastIndexOf("."));
string strPathSypf = strPath;
File.Copy((strPath), System.Web.HttpContext.Current.Server.MapPath(newpic), true);
float transparence = 0.3f;//透明度 越小越透明。
float[][] floatArray =
{
new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f },
new float[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f },
new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
new float[] { 0.0f, 0.0f, 0.0f, transparence, 0.0f },
new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }
};
ImageAttributes imageAttributes = new ImageAttributes();
imageAttributes.SetColorMatrix(new ColorMatrix(floatArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
System.Drawing.Image image = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(newpic));
Bitmap bm = new Bitmap(image);
System.Drawing.Image copyImage = System.Drawing.Image.FromFile(strPathSyp);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bm);
//如果要使水印居中显示,只要更改bm.Width - copyImage.Width, bm.Height - copyImage.Height,这两个值
//就可以了,在实际项目中,我用的是Convert.ToInt32(image.Width * 0.3), Convert.ToInt32(image.Height * 0.3)
// g.DrawImage(copyImage, new System.Drawing.Rectangle(bm.Width - copyImage.Width, bm.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
Rectangle rect = new Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height);
g.DrawImage(copyImage, rect, 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel, imageAttributes);
g.Dispose();
copyImage.Dispose();
bm.Save(strPathSypf);
bm.Dispose();
image.Dispose();
File.Delete(System.Web.HttpContext.Current.Server.MapPath(newpic));
}
}