speshow--自娱自乐

MSN:Jone_yin@live.cn

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
  
using System.Drawing;
protected void AddShuiYinWord(string Path, string Path_sy)
  {
   string addText = "测试水印";
   System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
   System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
   g.DrawImage(image, 0, 0, image.Width, image.Height);
   System.Drawing.Font f = new System.Drawing.Font("Verdana", 16);
   System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Blue);

   g.DrawString(addText, f, b, 15, 15);
   g.Dispose();

   image.Save(Path_sy);
   image.Dispose();
  }

  /**/
  /// <summary>
  /// 在图片上生成图片水印
  /// </summary>
  /// <param name="Path">原服务器图片路径</param>
  /// <param name="Path_syp">生成的带图片水印的图片路径</param>
  /// <param name="Path_sypf">水印图片路径</param>
  protected void AddShuiYinPic(string Path, string Path_syp, string Path_sypf)
  {
   System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
   System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);
   //System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);

   Bitmap newBitmap = new Bitmap(image.Width, image.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
   newBitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution);
   Graphics g = Graphics.FromImage(newBitmap);
   g.SmoothingMode = SmoothingMode.AntiAlias;
   g.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
   g.Dispose();
   System.IO.MemoryStream ms = new System.IO.MemoryStream();
   newBitmap.Save(ms, ImageFormat.Jpeg);
   //重新生成Image对象
   image = System.Drawing.Image.FromStream(ms);

   g = System.Drawing.Graphics.FromImage(image);

   g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
   g.Dispose();
   
   image.Save(Path_syp);
   image.Dispose();
copyImage.Dispose();
  }

posted on 2008-06-24 10:56  speshow  阅读(369)  评论(0编辑  收藏  举报