C# 浮水印透明度圖片

來源:互聯網
上載者:User

標籤:

/// <summary>        /// 在一張圖片的指定位置處加入一張具有浮水印效果的圖片        /// </summary>        /// <param name="SourceImage">指定源圖片的絕對路徑</param>        /// <param name="WaterMarkImage">指定浮水印圖片的絕對路徑</param>        /// <param name="SaveImage">儲存圖片的絕對路徑</param>        public static void MakeWaterMark(string SourceImage, string WaterMarkImage, string SaveImage)        {            // 建立一個對象用於操作需要加浮水印的源圖片            Image imgPhoto = Image.FromFile(SourceImage);            // 擷取該源圖片的寬度和高度            int phWidth = imgPhoto.Width;            int phHeight = imgPhoto.Height;            // 建立一個BMP格式的空白圖片(寬度和高度與源圖片一致)            Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);            // 設定該建立空白BMP圖片的解析度            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);            // 將該BMP圖片設定成一個繪圖物件            Graphics grPhoto = Graphics.FromImage(bmPhoto);            // 設定產生圖片的品質            grPhoto.SmoothingMode = SmoothingMode.AntiAlias;            // 將源圖片載入至建立的BMP圖片中            grPhoto.DrawImage(                imgPhoto,                               // Photo Image object                new Rectangle(0, 0, phWidth, phHeight), // Rectangle structure                0,                                      // x-coordinate of the portion of the source image to draw.                 0,                                      // y-coordinate of the portion of the source image to draw.                 phWidth,                                // Width of the portion of the source image to draw.                 phHeight,                               // Height of the portion of the source image to draw.                 GraphicsUnit.Pixel);                    // Units of measure             // 建立浮水印圖片的 Image 對象            Image imgWatermark = new Bitmap(WaterMarkImage);            // 擷取浮水印圖片的寬度和高度            int wmWidth = imgWatermark.Width;            int wmHeight = imgWatermark.Height;            //------------------------------------------------------------            // 第一步: 插入浮水印圖片            //------------------------------------------------------------            //Create a Bitmap based on the previously modified photograph Bitmap            Bitmap bmWatermark = new Bitmap(bmPhoto);            bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);            //Load this Bitmap into a new Graphic Object            Graphics grWatermark = Graphics.FromImage(bmWatermark);            //To achieve a transulcent watermark we will apply (2) color             //manipulations by defineing a ImageAttributes object and             //seting (2) of its properties.            ImageAttributes imageAttributes = new ImageAttributes();            //The first step in manipulating the watermark image is to replace             //the background color with one that is trasparent (Alpha=0, R=0, G=0, B=0)            //to do this we will use a Colormap and use this to define a RemapTable            ColorMap colorMap = new ColorMap();            //My watermark was defined with a background of 100% Green this will            //be the color we search for and replace with transparency            colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);            colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);            ColorMap[] remapTable = { colorMap };            imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);            //The second color manipulation is used to change the opacity of the             //watermark.  This is done by applying a 5x5 matrix that contains the             //coordinates for the RGBA space.  By setting the 3rd row and 3rd column             //to 0.3f we achive a level of opacity            float[][] colorMatrixElements = {                                                 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,  0.3f, 0.0f},                                                        new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}                                            };            ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);            imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default,                ColorAdjustType.Bitmap);            //For this example we will place the watermark in the upper right            //hand corner of the photograph. offset down 10 pixels and to the             //left 10 pixles            int xPosOfWm = ((phWidth - wmWidth) - 10);            int yPosOfWm = 10;            grWatermark.DrawImage(imgWatermark,                new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight),  //Set the detination Position                0,                  // x-coordinate of the portion of the source image to draw.                 0,                  // y-coordinate of the portion of the source image to draw.                 wmWidth,            // Watermark Width                wmHeight,            // Watermark Height                GraphicsUnit.Pixel, // Unit of measurment                imageAttributes);   //ImageAttributes Object            //Replace the original photgraphs bitmap with the new Bitmap            imgPhoto.Dispose();            imgPhoto = bmWatermark;            grPhoto.Dispose();            grWatermark.Dispose();            bmPhoto.Dispose();            //------------------------------------------------------------            // 第三步:儲存圖片            //------------------------------------------------------------            imgPhoto.Save(SaveImage, ImageFormat.Jpeg);            // 釋放使用中的資源            imgPhoto.Dispose();            imgWatermark.Dispose();            bmWatermark.Dispose();        }

 

C# 浮水印透明度圖片

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.