圖片上加浮水印相信每位程式員都會遇到這個需求,下面這篇文章主要給大家介紹了asp .net實現給圖片添加圖片浮水印的方法,文中給出了完整的執行個體代碼,相信對大傢具有一定的參考價值,需要的朋友們下面來一起看看吧。
前言
本文主要給大家介紹的是asp .net實現給圖片添加圖片浮水印的方法,注意是圖片浮水印,需要的朋友們可以參考學習,下面話不多說來看看詳細的介紹吧。
方法如下
首先寫好一個寫入圖片浮水印的類,先建立一個ImageWriter類庫 (該類中有包含枚舉類型和方法)
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using System.Collections;using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imaging;namespace ImageWriter{ /// <summary> /// 圖片處理類 /// </summary> public class ImageManager { #region 變數聲明開始 /// <summary> /// 枚舉: 浮水印位置 /// </summary> public enum WatermarkPosition { /// <summary> /// 左上 /// </summary> LeftTop, /// <summary> /// 左中 /// </summary> Left, /// <summary> /// 左下 /// </summary> LeftBottom, /// <summary> /// 正上 /// </summary> Top, /// <summary> /// 正中 /// </summary> Center, /// <summary> /// 正下 /// </summary> Bottom, /// <summary> /// 右上 /// </summary> RightTop, /// <summary> /// 右中 /// </summary> RightCenter, /// <summary> /// 右下 /// </summary> RigthBottom } #endregion 變數聲明結束 #region 建構函式開始 /// <summary> /// 建構函式: 預設 /// </summary> public ImageManager() { } #endregion 建構函式結束 #region 私人函數開始 /// <summary> /// 擷取: 圖片去副檔名(包含完整路徑及其檔案名稱)小寫字串 /// </summary> /// <param name="path">圖片路徑(包含完整路徑,檔案名稱及其副檔名): string</param> /// <returns>返回: 圖片去副檔名(包含完整路徑及其檔案名稱)小寫字串: string</returns> private string GetFileName(string path) { return path.Remove(path.LastIndexOf('.')).ToLower(); } /// <summary> /// 擷取: 圖片以'.'開頭的小寫字串副檔名 /// </summary> /// <param name="path">圖片路徑(包含完整路徑,檔案名稱及其副檔名): string</param> /// <returns>返回: 圖片以'.'開頭的小寫字串副檔名: string</returns> private string GetExtension(string path) { return path.Remove(0, path.LastIndexOf('.')).ToLower(); } /// <summary> /// 擷取: 圖片以 '.' 開頭的小寫字串副檔名對應的 System.Drawing.Imaging.ImageFormat 對象 /// </summary> /// <param name="format">以 '. '開頭的小寫字串副檔名: string</param> /// <returns>返回: 圖片以 '.' 開頭的小寫字串副檔名對應的 System.Drawing.Imaging.ImageFormat 對象: System.Drawing.Imaging.ImageFormat</returns> private ImageFormat GetImageFormat(string format) { switch (format) { case ".bmp": return ImageFormat.Bmp; case ".emf": return ImageFormat.Emf; case ".exif": return ImageFormat.Exif; case ".gif": return ImageFormat.Gif; case ".ico": return ImageFormat.Icon; case ".png": return ImageFormat.Png; case ".tif": return ImageFormat.Tiff; case ".tiff": return ImageFormat.Tiff; case ".wmf": return ImageFormat.Wmf; default: return ImageFormat.Jpeg; } } /// <summary> /// 擷取: 枚舉 Uinatlex.ToolBox.ImageManager.WatermarkPosition 對應的 System.Drawing.Rectangle 對象 /// </summary> /// <param name="positon">枚舉 Uinatlex.ToolBox.ImageManager.WatermarkPosition: Uinatlex.ToolBox.ImageManager.WatermarkPosition</param> /// <param name="X">原圖寬度: int</param> /// <param name="Y">原圖高度: int</param> /// <param name="x">浮水印寬度: int</param> /// <param name="y">浮水印高度: int</param> /// <param name="i">邊距: int</param> /// <returns>返回: 枚舉 Uinatlex.ToolBox.ImageManager.WatermarkPosition 對應的 System.Drawing.Rectangle 對象: System.Drawing.Rectangle</returns> private Rectangle GetWatermarkRectangle(WatermarkPosition positon, int X, int Y, int x, int y, int i) { switch (positon) { case WatermarkPosition.LeftTop: return new Rectangle(i, i, x, y); case WatermarkPosition.Left: return new Rectangle(i, (Y - y) / 2, x, y); case WatermarkPosition.LeftBottom: return new Rectangle(i, Y - y - i, x, y); case WatermarkPosition.Top: return new Rectangle((X - x) / 2, i, x, y); case WatermarkPosition.Center: return new Rectangle((X - x) / 2, (Y - y) / 2, x, y); case WatermarkPosition.Bottom: return new Rectangle((X - x) / 2, Y - y - i, x, y); case WatermarkPosition.RightTop: return new Rectangle(X - x - i, i, x, y); case WatermarkPosition.RightCenter: return new Rectangle(X - x - i, (Y - y) / 2, x, y); default: return new Rectangle(X - x - i, Y - y - i, x, y); } } #endregion 私人函數結束 #region 文字產生開始 #endregion 文字產生結束 #region 設定透明度開始 /// <summary> /// 設定: 圖片 System.Drawing.Bitmap 對象透明度 /// </summary> /// <param name="sBitmap">圖片 System.Drawing.Bitmap 對象: System.Drawing.Bitmap</param> /// <param name="transparence">浮水印透明度(值越高透明度越低,範圍在0.0f~1.0f之間): float</param> /// <returns>圖片 System.Drawing.Bitmap: System.Drawing.Bitmap</returns> public Bitmap SetTransparence(Bitmap bm, float transparence) { if (transparence == 0.0f || transparence == 1.0f) throw new ArgumentException("透明度值只能在0.0f~1.0f之間"); float[][] floatArray = { new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f }, new float[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f }, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }, new float[] { 0.0f, 0.0f, 0.0f, transparence, 0.0f }, new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f } }; ImageAttributes imageAttributes = new ImageAttributes(); imageAttributes.SetColorMatrix(new ColorMatrix(floatArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap); Bitmap bitmap = new Bitmap(bm.Width, bm.Height); Graphics graphics = Graphics.FromImage(bitmap); graphics.DrawImage(bm, new Rectangle(0, 0, bm.Width, bm.Height), 0, 0, bm.Width, bm.Height, GraphicsUnit.Pixel, imageAttributes); graphics.Dispose(); imageAttributes.Dispose(); bm.Dispose(); return bitmap; } /// <summary> /// 設定: 圖片 System.Drawing.Bitmap 對象透明度 /// </summary> /// <param name="readpath">圖片路徑(包含完整路徑,檔案名稱及其副檔名): string</param> /// <param name="transparence">浮水印透明度(值越高透明度越低,範圍在0.0f~1.0f之間): float</param> /// <returns>圖片 System.Drawing.Bitmap: System.Drawing.Bitmap</returns> public Bitmap SetTransparence(string readpath, float transparence) { return SetTransparence(new Bitmap(readpath), transparence); } #endregion 設定透明度結束 #region 添加浮水印開始 /// <summary> /// 產生: 原圖繪製浮水印的 System.Drawing.Bitmap 對象 /// </summary> /// <param name="sBitmap">原圖 System.Drawing.Bitmap 對象: System.Drawing.Bitmap</param> /// <param name="wBitmap">浮水印 System.Drawing.Bitmap 對象: System.Drawing.Bitmap</param> /// <param name="position">枚舉 Uinatlex.ToolBox.ImageManager.WatermarkPosition : Uinatlex.ToolBox.ImageManager.WatermarkPosition</param> /// <param name="margin">浮水印邊距: int</param> /// <returns>返回: 原圖繪製浮水印的 System.Drawing.Bitmap 對象 System.Drawing.Bitmap</returns> public Bitmap CreateWatermark(Bitmap sBitmap, Bitmap wBitmap, WatermarkPosition position, int margin) { Graphics graphics = Graphics.FromImage(sBitmap); graphics.DrawImage(wBitmap, GetWatermarkRectangle(position, sBitmap.Width, sBitmap.Height, wBitmap.Width, wBitmap.Height, margin)); graphics.Dispose(); wBitmap.Dispose(); return sBitmap; } #endregion 添加浮水印結束 #region 圖片切割開始 #endregion 圖片切割結束 #region 圖片縮放開始 #endregion 圖片縮放結束 #region 儲存圖片到檔案開始 #region 普通儲存開始 /// <summary> /// 儲存: System.Drawing.Bitmap 對象到圖片檔案 /// </summary> /// <param name="bitmap">System.Drawing.Bitmap 對象: System.Drawing.Bitmap</param> /// <param name="writepath">儲存路徑(包含完整路徑,檔案名稱及其副檔名): string</param> public void Save(Bitmap bitmap, string writepath) { try { bitmap.Save(writepath, GetImageFormat(GetExtension(writepath))); bitmap.Dispose(); } catch { throw new ArgumentException("圖片儲存錯誤"); } } /// <summary> /// 儲存: 對象到圖片檔案 /// </summary> /// <param name="readpath">原圖路徑(包含完整路徑,檔案名稱及其副檔名): string</param> /// <param name="writepath">儲存路徑(包含完整路徑,檔案名稱及其副檔名): string</param> public void Save(string readpath, string writepath) { if (string.Compare(readpath, writepath) == 0) throw new ArgumentException("源圖片與靶心圖表片地址相同"); try { Save(new Bitmap(readpath), writepath); } catch { throw new ArgumentException("圖片讀取錯誤"); } } #endregion 普通儲存結束 #region 文字繪圖儲存開始 #endregion 文字繪圖儲存結束 #region 透明度調整儲存開始 /// <summary> /// 儲存: 設定透明度的對象到圖片檔案 /// </summary> /// <param name="sBitmap">圖片 System.Drawing.Bitmap 對象: System.Drawing.Bitmap</param> /// <param name="transparence">浮水印透明度(值越高透明度越低,範圍在0.0f~1.0f之間): float</param> /// <param name="writepath">儲存路徑(包含完整路徑,檔案名稱及其副檔名): string</param> public void SaveTransparence(Bitmap bitmap, float transparence, string writepath) { Save(SetTransparence(bitmap, transparence), writepath); } /// <summary> /// 儲存: 設定透明度的象到圖片檔案 /// </summary> /// <param name="readpath">原圖路徑(包含完整路徑,檔案名稱及其副檔名): string</param> /// <param name="transparence">浮水印透明度(值越高透明度越低,範圍在0.0f~1.0f之間): float</param> /// <param name="writepath">儲存路徑(包含完整路徑,檔案名稱及其副檔名): string</param> public void SaveTransparence(string readpath, float transparence, string writepath) { Save(SetTransparence(readpath, transparence), writepath); } #endregion 透明度調整儲存結束 #region 浮水印圖片儲存開始 /// <summary> /// 儲存: 繪製浮水印的對象到圖片檔案 /// </summary> /// <param name="sBitmap">原圖 System.Drawing.Bitmap 對象: System.Drawing.Bitmap</param> /// <param name="wBitmap">浮水印 System.Drawing.Bitmap 對象: System.Drawing.Bitmap</param> /// <param name="position">枚舉 Uinatlex.ToolBox.ImageManager.WatermarkPosition : Uinatlex.ToolBox.ImageManager.WatermarkPosition</param> /// <param name="margin">浮水印邊距: int</param> /// <param name="writepath">儲存路徑(包含完整路徑,檔案名稱及其副檔名): string</param> public void SaveWatermark(Bitmap sBitmap, Bitmap wBitmap, WatermarkPosition position, int margin, string writepath) { Save(CreateWatermark(sBitmap, wBitmap, position, margin), writepath); } /// <summary> /// 儲存: 繪製浮水印的對象到圖片檔案 /// </summary> /// <param name="readpath">圖片路徑(包含完整路徑,檔案名稱及其副檔名): string</param> /// <param name="watermarkpath">浮水印圖片路徑(包含完整路徑,檔案名稱及其副檔名): string</param> /// <param name="transparence">浮水印透明度(值越高透明度越低,範圍在0.0f~1.0f之間): float</param> /// <param name="position">枚舉 Uinatlex.ToolBox.ImageManager.WatermarkPosition : Uinatlex.ToolBox.ImageManager.WatermarkPosition</param> /// <param name="margin">浮水印邊距: int</param> /// <param name="writepath">儲存路徑(包含完整路徑,檔案名稱及其副檔名): string</param> public void SaveWatermark(string readpath, string watermarkpath, float transparence, WatermarkPosition position, int margin, string writepath) { if (string.Compare(readpath, writepath) == 0) throw new ArgumentException("源圖片與靶心圖表片地址相同"); if (transparence == 0.0f) Save(readpath, writepath); else if (transparence == 1.0f) SaveWatermark(new Bitmap(readpath), new Bitmap(watermarkpath), position, margin, writepath); else SaveWatermark(new Bitmap(readpath), SetTransparence(watermarkpath, transparence), position, margin, writepath); } #endregion 浮水印圖片儲存結束 #region 圖片切割儲存開始 #endregion 圖片切割儲存結束 #region 圖片縮放儲存開始 #endregion 圖片縮放儲存開始 #endregion 儲存圖片到檔案結束 }}/* 調用很簡單 im.SaveWatermark(原圖地址, 浮水印地址, 透明度, 浮水印位置, 邊距,儲存位置); Uinatlex.ToolBox.ImageManager im = new Uinatlex.ToolBox.ImageManager(); im.SaveWatermark(Server.MapPath("/原圖.jpg"), Server.MapPath("/浮水印.jpg"), 0.5f, Uinatlex.ToolBox.ImageManager.WatermarkPosition.RigthBottom, 10, Server.MapPath("/原圖.jpg")); */
在用戶端頁面中調用上面這個類
protected void Page_Load(object sender, EventArgs e) { string newfileName = Server.MapPath("img/") + DateTime.Now.Millisecond + "" + DateTime.Now.Second + ".jpg"; ImageManager im = new ImageManager(); //調用很簡單 im.SaveWatermark(原圖地址, 浮水印地址, 透明度, 浮水印位置, 邊距,儲存到的位置); im.SaveWatermark(Server.MapPath("img/Chrysanthemum.jpg"), Server.MapPath("img/woshi.jpg"), 0.9f, ImageManager.WatermarkPosition.RigthBottom, 10, newfileName); }