C# 水印

  1.  private void Btn_Upload_Click(object sender, System.EventArgs e)
  2.         {
  3.             if(UploadFile.PostedFile.FileName.Trim()!="")
  4.             {
  5.                 //上传文件
  6.                 string extension = Path.GetExtension(UploadFile.PostedFile.FileName).ToUpper();
  7.                 string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
  8.                 string path = Server.MapPath(".") + "/UploadFile/" + fileName + extension;
  9.                 UploadFile.PostedFile.SaveAs(path);
  10.                 //加文字水印,注意,这里的代码和以下加图片水印的代码不能共存
  11.                 System.Drawing.Image image = System.Drawing.Image.FromFile(path);
  12.                 Graphics g = Graphics.FromImage(image);
  13.                 g.DrawImage(image, 0, 0, image.Width, image.Height);
  14.                 Font f = new Font("Verdana", 32);
  15.                 Brush b = new SolidBrush(Color.White);
  16.                 string addText = AddText.Value.Trim();
  17.                 g.DrawString(addText, f, b, 10, 10);
  18.                 g.Dispose();
  19.                 //加图片水印
  20.                 System.Drawing.Image image = System.Drawing.Image.FromFile(path);
  21.                 System.Drawing.Image copyImage = System.Drawing.Image.FromFile( Server.MapPath(".") + "/Alex.gif");
  22.                 Graphics g = Graphics.FromImage(image);
  23.                 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);
  24.                 g.Dispose();
  25.                 //保存加水印过后的图片,删除原始图片
  26.                 string newPath = Server.MapPath(".") + "/UploadFile/" + fileName + "_new" + extension;
  27.                 image.Save(newPath);
  28.                 image.Dispose();
  29.                 if(File.Exists(path))
  30.                 {
  31.                     File.Delete(path);
  32.                 }
  33.                 Response.Redirect(newPath);
  34.             }
  35.         }

第二个

  1. //############################################################################## 
  2. //图片操作类 
  3. //############################################################################## 
  4. public class ImageHelper 
  5.     
  6.     #region " 水印存放的相对路径 " 
  7.     public static string GetLogoPath() 
  8.     { 
  9.         return "/images/logo.png";    ///水印图路径
  10.     } 
  11.     #endregion 
  12.     
  13.     #region " 图片水印 " 
  14.     // <summary> 
  15.     // 在图片上生成图片水印,此方法不支持Gif类型的图片 
  16.     // </summary> 
  17.     // <param name="Path">原服务器图片路径</param> 
  18.     // <param name="Path_syp">生成的带图片水印的图片路径</param> 
  19.     // <param name="Path_sypf">水印图片路径</param> 
  20.     public static void MarkImage(Stream InUploadImagePath, string inLogoImagePath, string InSavePath) 
  21.     { 
  22.         System.Drawing.Image Image = System.Drawing.Image.FromStream(InUploadImagePath); 
  23.         System.Drawing.Image newimage = Image.FromFile(Current.Server.MapPath(inLogoImagePath)); 
  24.         Graphics g = Graphics.FromImage(Image); 
  25.         g.DrawImage(newimage, new Rectangle(Image.Width - newimage.Width, Image.Height - newimage.Height, newimage.Width, newimage.Height), 0, 0, newimage.Width, newimage.Height, GraphicsUnit.Pixel); 
  26.         try { 
  27.             Image.Save(Current.Server.MapPath(InSavePath)); 
  28.         } 
  29.         catch (Exception ex) { 
  30.         } 
  31.         finally { 
  32.             g.Dispose(); 
  33.             Image.Dispose(); 
  34.             newimage.Dispose(); 
  35.         } 
  36.     } 
  37.     #endregion 
  38.     

第三个

 

http://writeblog.csdn.net/PostEditPlain.aspx

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值