专注于技术经验交流

水至清则无鱼、宁静而致远!

技术、经验、学习共同打造网络新生活!
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C# 动态生成图片保存到文件

Posted on 2009-06-25 11:05  小鱼儿  阅读(1697)  评论(0编辑  收藏  举报

调用函数部分

private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   string saveasPath = @"f:\mywork\images\Showimg.jpg";
   AddImageWatermark(saveasPath);  
   //sc();
   bz();
}

  /// <summary>
  /// 加水印并保存图
  /// </summary>
  /// <param name="strOraginalPath"></param>
  public void AddImageWatermark(string strOraginalPath)
  {
   string strWatermarkImagePath = HttpContext.Current.Server.MapPath("images/watermark.gif");

   System.Drawing.Image imgOraginal = System.Drawing.Image.FromFile(strOraginalPath);
   int iOraginalWidth = imgOraginal.Width;
   int iOraginalHeight = imgOraginal.Height;
   System.Drawing.Bitmap bitOraginal = new System.Drawing.Bitmap(imgOraginal, iOraginalWidth, iOraginalHeight);
   imgOraginal.Dispose();

   System.Drawing.Graphics gOraginal = System.Drawing.Graphics.FromImage(bitOraginal);
   //System.Drawing.Rectangle rectTextField = new System.Drawing.Rectangle(10, 10, iWidth - 20, iHeight - 20);

   System.Drawing.Image imgWatermark = System.Drawing.Image.FromFile(strWatermarkImagePath);
   int iWatermarkWidth = imgWatermark.Width;
   int iWatermarkHeight = imgWatermark.Height;

   System.Drawing.Rectangle rect;

   int iHCenter = (iOraginalWidth - iWatermarkWidth) / 2;
   int iVCenter = (iOraginalHeight - iWatermarkHeight) / 2;
   int iHRight = (iOraginalWidth - iWatermarkWidth - 10);
   int iVBottom = (iOraginalHeight - iWatermarkHeight - 10);

   //水印的位置
   rect = new System.Drawing.Rectangle(iHRight, iVBottom, iWatermarkWidth, iWatermarkHeight);

   gOraginal.DrawImage(imgWatermark, rect);

   bitOraginal.Save(strOraginalPath, System.Drawing.Imaging.ImageFormat.Jpeg);

   bitOraginal.Dispose();
   gOraginal.Dispose();
   imgWatermark.Dispose();
  }

New Document