C#实现图片缩放,并给图片加上带版权的水印

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Drawing;
  12. using System.Drawing.Drawing2D;
  13. using System.Drawing.Imaging;
  14. public partial class Web_MakeWaterMark : System.Web.UI.Page
  15. {
  16.     /// <param name="newFaxWidtht">缩放至宽度</param>
  17.     /// <param name="newFaxHeight">缩放至高度</param>
  18.     int newFaxWidtht = 100;
  19.     int newFaxHeight = 120;
  20.     protected void Page_Load(object sender, EventArgs e)
  21.     {
  22.         string strOldPic = "images/welcome.jpg";
  23.         string strNewPic = "images/welcome-1.jpg";
  24.         this.SmallPic(strOldPic, strNewPic, newFaxWidtht, newFaxHeight);
  25.         this.MakeWaterMark(strNewPic);
  26.     }
  27.     /**//**//**//// <summary>
  28.     /// 生成带版权水印的图片
  29.     /// </summary>
  30.     /// <param name="IconImg">要生成水印图片的文件地址</param>
  31.     private void MakeWaterMark(string IconImg)
  32.     {
  33.         //定义输出位图
  34.         Bitmap bmp = new Bitmap(newFaxWidtht, newFaxHeight);
  35.         //定义绘图对象
  36.         Graphics g = Graphics.FromImage(bmp);
  37.         //描述绘图矩形区域 
  38.         Rectangle newRect = new Rectangle(0, 0, newFaxWidtht, newFaxHeight);
  39.         //设置背景色
  40.         g.FillRectangle(new SolidBrush(Color.White), newRect);
  41.         //获取要生成水印的图片
  42.         Bitmap newBitmap = new Bitmap(IconImg);
  43.         g.DrawImage(newBitmap, 0, 0);
  44.         //定义水印字的字体
  45.         Font font = new Font( "Arial", 10, FontStyle.Bold);
  46.         //定义水印字的格式刷
  47.         LinearGradientBrush brush = new LinearGradientBrush(
  48.                                                               new Rectangle(0, 0, 10, 20), 
  49.                                                               Color.Red,
  50.                                                               Color.Blue, 
  51.                                                               10f, 
  52.                                                               true
  53.                                                            );
  54.         //给照片打上水印
  55.         g.DrawString("szrjjz.com", font, brush, 0, newFaxHeight-20);
  56.         //保存为新的输出图片
  57.         System.IO.MemoryStream ms = new System.IO.MemoryStream();
  58.         bmp.Save(ms, ImageFormat.Jpeg);
  59.         //输出图片
  60.         Response.ClearContent();
  61.         Response.ContentType = "image/jpeg";
  62.         Response.BinaryWrite(ms.ToArray());
  63.     }
  64.     /// <summary>
  65.     /// 缩放图片
  66.     /// </summary>
  67.     /// <param name="strOldPic">源图文件名(包括路径)</param>
  68.     /// <param name="strNewPic">缩放后保存为文件名(包括路径)</param>
  69.     /// <param name="intWidth">缩放至宽度</param>
  70.     /// <param name="intHeight">缩放至高度</param>
  71.     public void SmallPic(string strOldPic, string strNewPic, int intWidth, int intHeight)
  72.     {
  73.         System.Drawing.Bitmap objPic, objNewPic;
  74.         try
  75.         {
  76.             objPic = new System.Drawing.Bitmap(strOldPic);
  77.             objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
  78.             objNewPic.Save(strNewPic);
  79.         }
  80.         catch (Exception exp) { throw exp; }
  81.         finally
  82.         {
  83.             objPic = null;
  84.             objNewPic = null;
  85.         }
  86.     }
  87. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值