日期:2014-05-18  浏览次数:20436 次

.NET 如何给图片加水印?
我在网上找了好几个都不行,图片加上水印后,质量发生了变化,比原图模糊不少,且文件大小也比原图小了不少;
因为我们要求对图片清晰度相当的高;所以这种方法不能行,那位朋友有没有更好的方法,加了水印后,图片质量不能发生变化.只是在图片左上或右下加上我们的图标和文字就可以了;
我的代码如下:
C# code
       System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.DrawImage(image, 0, 0, image.Width, image.Height);
            System.Drawing.Font f = new System.Drawing.Font("Verdana", 9);
            System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Black);

            try
            {
                g.DrawString(addText, f, b, image.Width - 150, image.Height - 20);
                g.Dispose();

                image.Save(Path_sy);
            }



------解决方案--------------------
http://blog.csdn.net/rjzou2006/archive/2007/06/14/1651383.aspx
这个是个现成的类
------解决方案--------------------
http://hi.baidu.com/liyukun8203/blog/item/a8dc368f0e43aaeaf01f367a.html
------解决方案--------------------
参考
http://www.cnblogs.com/skywind/archive/2007/04/17/716586.html
http://www.cnblogs.com/xufengtian/archive/2007/03/21/682773.html
http://www.cnblogs.com/mbskys/articles/714648.html
------解决方案--------------------
http://www.cnblogs.com/345563452/archive/2008/10/14/1311057.html
这个也可以看下
------解决方案--------------------
这个是以前收藏的
Asp.net(C#)给图片加上水印效果 
http://www.zebcn.com/html/200504/1698.html
--------------------------------------------
 
时间:2005-4-20
  
下面的代码中,加文字水印和加图片水印的代码不能共存,为了方便显示才写在一块的 
private void Btn_Upload_Click(object sender, System.EventArgs e) 

if(UploadFile.PostedFile.FileName.Trim()!="") 

//上传文件 
string extension = Path.GetExtension(UploadFile.PostedFile.FileName).ToUpper(); 
string fileName = DateTime.Now.ToString("yyyyMMddhhmmss"); 
string path = Server.MapPath(".") + "/UploadFile/" + fileName + extension; 
UploadFile.PostedFile.SaveAs(path); 
 
 
//加文字水印,注意,这里的代码和以下加图片水印的代码不能共存 
System.Drawing.Image image = System.Drawing.Image.FromFile(path); 
Graphics g = Graphics.FromImage(image); 
g.DrawImage(image, 0, 0, image.Width, image.Height); 
Font f = new Font("Verdana", 32); 
Brush b = new SolidBrush(Color.White); 
string addText = AddText.Value.Trim(); 
g.DrawString(addText, f, b, 10, 10); 
g.Dispose(); 
 
 
//加图片水印 
System.Drawing.Image image = System.Drawing.Image.FromFile(path); 
System.Drawing.Image copyImage = System.Drawing.Image.FromFile( Server.MapPath(".") + "/Alex.gif"); 
Graphics g = Graphics.FromImage(image); 
g.DrawImage(copyImage, new Rectangle(image.Width-copyImage.Width, image.Height-copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel); 
g.Dispose(); 
 
 
//保存加水印过后的图片,删除原始图片 
string newPath = Server.MapPath(".") + "/UploadFile/" + fileName + "_new" + extension;