zoukankan      html  css  js  c++  java
  • .Net给图片添加水印效果

           /// <summary>
            ///  加水印图片
            /// </summary>
            /// <param name="picture">imge 对象</param>
            /// <param name="WaterMarkPicPath">水印图片的地址</param>
            public static void addWatermarkImage(Image _Image, string WaterMarkPicPath)
            {

                try
                {
                    Graphics picture = Graphics.FromImage(_Image);

                    Image watermark = new Bitmap(WaterMarkPicPath);
                    int _width = _Image.Width;
                    int _height = _Image.Height;

                    ImageAttributes imageAttributes = new ImageAttributes();
                    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},
                                                    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.1f, 0.0f},
                                                    new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}
                                                };

                    ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);

                    imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                    //imageAttributes.SetColorKey(System.Drawing.Color.Black, System.Drawing.Color.Black, ColorAdjustType.Bitmap);

                    int xpos = 0;
                    int ypos = 0;
                    int WatermarkWidth = 0;
                    int WatermarkHeight = 0;
                    double bl = 1d;
                    WatermarkWidth = _width;
                    WatermarkHeight = _height;
                    xpos = 0;
                    ypos = 0;

                    picture.DrawImage(watermark, new Rectangle(xpos, ypos, WatermarkWidth, WatermarkHeight), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);
                    _Image = watermark;
                    watermark.Dispose();
                    imageAttributes.Dispose();
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show("Exception in addWatermarkImage" + ex.Message);
                }
            }

  • 相关阅读:
    题解 CF171G 【Mysterious numbers
    题解 P1157 【组合的输出】
    题解 P3955 【图书管理员】
    题解 P2036 【Perket】
    题解 CF837A 【Text Volume】
    题解 CF791A 【Bear and Big Brother】
    题解 CF747A 【Display Size】
    题解 P1332 【血色先锋队】
    题解 P2660 【zzc 种田】
    题解 P4470 【[BJWC2018]售票】
  • 原文地址:https://www.cnblogs.com/huaku/p/3040207.html
Copyright © 2011-2022 走看看