C#實現圖片縮放,並給圖片加上帶著作權的浮水印

來源:互聯網
上載者:User
  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. }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.