国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C#(.net)水印圖片的生成

2019-11-17 01:45:49
字體:
來源:轉載
供稿:網友

C#(.net)水印圖片的生成

/*

*

* 使用說明:

*  建議先定義一個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,
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 金寨县| 庄河市| 沙湾县| 黄浦区| 大悟县| 四川省| 海门市| 礼泉县| 沭阳县| 图片| 新疆| 乌鲁木齐县| 定结县| 新乐市| 甘德县| 宕昌县| 石渠县| 舒城县| 灵台县| 越西县| 普定县| 西乡县| 沿河| 宁海县| 伊金霍洛旗| 正镶白旗| 滦平县| 阳西县| 北川| 花莲市| 莫力| 荣成市| 桦南县| 竹山县| 大庆市| 蓝田县| 高碑店市| 咸丰县| 溆浦县| 抚州市| 油尖旺区|