/*
*
* 使用說明:
* 建議先定義一個WaterImage實例
* 然后利用實例的屬性,去匹配需要進行操作的參數
* 然后定義一個WaterImageManage實例
* 利用WaterImageManage實例進行DrawImage(),印圖片水印
* DrawWords()印文字水印
*
-*/
using System;using System.Drawing;using System.Drawing.Imaging;using System.Drawing.Drawing2D;using System.IO;namespace ABC{ /// <summary> /// 圖片位置 /// </summary> public enum ImagePosition { LeftTop, //左上 LeftBottom, //左下 RightTop, //右上 RigthBottom, //右下 TopMiddle, //頂部居中 BottomMiddle, //底部居中 Center //中心 } /// <summary> /// 水印圖片的操作管理 Design by Gary Gong From Demetersoft.com /// </summary> public class WaterImageManage { /// <summary> /// 生成一個新的水印圖片制作實例 /// </summary> public WaterImageManage() { // // TODO: Add constructor logic here // } /// <summary> /// 添加圖片水印 /// </summary> /// <param name="sourcePicture">源圖片文件名</param> /// <param name="waterImage">水印圖片文件名</param> /// <param name="alpha">透明度(0.1-1.0數值越小透明度越高)</param> /// <param name="position">位置</param> /// <param name="PicturePath" >圖片的路徑</param> /// <returns>返回生成于指定文件夾下的水印文件名</returns> public string DrawImage(string sourcePicture, string waterImage, float alpha, ImagePosition position, string PicturePath) { // // 判斷參數是否有效 // if (sourcePicture == string.Empty || waterImage == string.Empty || alpha == 0.0 || PicturePath == string.Empty) { return sourcePicture; } // // 源圖片,水印圖片全路徑 // string sourcePictureName = PicturePath + sourcePicture; string waterPictureName = PicturePath + waterImage; string fileSourceExtension = System.IO.Path.GetExtension(sourcePictureName).ToLower(); string fileWaterExtension = System.IO.Path.GetExtension(waterPictureName).ToLower(); // // 判斷文件是否存在,以及類型是否正確 // if (System.IO.File.Exists(sourcePictureName) == false || System.IO.File.Exists(waterPictureName) == false || ( fileSourceExtension != ".gif" && fileSourceExtension != ".jpg" && fileSourceExtension != ".png") || ( fileWaterExtension != ".gif" && fileWaterExtension != ".jpg" && fileWaterExtension != ".png") ) { return sourcePicture; } // // 目標圖片名稱及全路徑 // string targetImage = sourcePictureName.Replace(System.IO.Path.GetExtension(sourcePictureName), "") + "_1101.jpg"; // // 將需要加上水印的圖片裝載到Image對象中 // Image imgPhoto = Image.FromFile(sourcePictureName); // // 確定其長寬 // int phWidth = imgPhoto.Width; int phHeight = imgPhoto.Height; // // 封裝 GDI+ 位圖,此位圖由圖形圖像及其屬性的像素數據組成。 // Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bpPRgb); // // 設定分辨率 // bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); // // 定義一個繪圖畫面用來裝載位圖 // Graphics grPhoto = Graphics.FromImage(bmPhoto); // //同樣,由于水印是圖片,我們也需要定義一個Image來裝載它 // Image imgWatermark = new Bitmap(waterPictureName); // // 獲取水印圖片的高度和寬度 // int wmWidth = imgWatermark.Width; int wmHeight = imgWatermark.Height; //SmoothingMode:指定是否將平滑處理(消除鋸齒)應用于直線、曲線和已填充區域的邊緣。 // 成員名稱 說明 // AntiAlias 指定消除鋸齒的呈現。 // Default 指定不消除鋸齒。 // HighQuality 指定高質量、低速度呈現。 // HighSpeed 指定高速度、低質量呈現。 // Invalid 指定一個無效模式。 // None 指定不消除鋸齒。 grPhoto.SmoothingMode = SmoothingMode.AntiAlias; // // 第一次描繪,將我們的底圖描繪在繪圖畫面上 // grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, phWidth, phHeight), 0, 0, phWidth, phHeight, GraphicsUnit.Pixel); // // 與底圖一樣,我們需要一個位圖來裝載水印圖片。并設定其分辨率 // Bitmap bmWatermark = new Bitmap(bmPhoto); bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); // // 繼續,將水印圖片裝載到一個繪圖畫面grWatermark // Graphics grWatermark = Graphics.FromImage(bmWatermark); // //ImageAttributes 對象包含有關在呈現時如何操作位圖和圖元文件顏色的信息。 // ImageAttributes imageAttributes = new ImageAttributes(); // //Colormap: 定義轉換顏色的映射 // ColorMap colorMap = new ColorMap(); // //我的水印圖被定義成擁有綠色背景色的圖片被替換成透明 // colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); ColorMap[] remapTable = { colorMap }; imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap); float[][] colorMatrixElements = { new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, // red紅色 new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, //green綠色 new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, //blue藍色 new float[] {0.0f, 0.0f, 0.0f, alpha, 0.0f}, //透明度 new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}};// // ColorMatrix:定義包含 RGBA 空間坐標的 5 x 5 矩陣。 // ImageAttributes 類的若干方法通過使用顏色矩陣調整圖像顏色。 ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); // //上面設置完顏色,下面開始設置位置 // int xPosOfWm; int yPosOfWm; switch (position) { case ImagePosition.BottomMiddle: xPosOfWm = (phWidth - wmWidth) / 2; yPosOfWm = phHeight - wmHeight - 10; break; case ImagePosition.Center: xPosOfWm = (phWidth - wmWidth) / 2; yPosOfWm = (phHeight - wmHeight) / 2; break; case ImagePosition.LeftBottom: xPosOfWm = 10; yPosOfWm = phHeight - wmHeight - 10; break; case ImagePosition.LeftTop: xPosOfWm = 10; yPosOfWm = 10; break; case ImagePosition.RightTop: xPosOfWm = phWidth - wmWidth - 10; yPosOfWm = 10; break; case ImagePosition.RigthBottom: xPosOfWm = phWidth - wmWidth - 10; yPosOfWm = phHeight - wmHeight - 10; break; case ImagePosition.TopMiddle: xPosOfWm = (phWidth - wmWidth) / 2; yPosOfWm = 10; break; default: xPosOfWm = 10; yPosOfWm = phHeight - wmHeight - 10; break; } // // 第二次繪圖,把水印印上去 // grWatermark.DrawImage(imgWatermark, new Rectangle(xPosOfWm,
新聞熱點
疑難解答