zoukankan      html  css  js  c++  java
  • C# 水印透明度图片

    /// <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();
            }
  • 相关阅读:
    浅谈UML学习笔记之用例图
    流媒体服务器
    浅谈UML的概念和模型之UML视图
    浅谈UML的概念和模型之UML九种图
    SQL 语法
    吴裕雄--天生自然数据结构:链式队列及基本操作
    吴裕雄--天生自然数据结构:顺序队列
    吴裕雄--天生自然数据结构:队列存储结构
    吴裕雄--天生自然数据结构:链栈及基本操作
    吴裕雄--天生自然数据结构:顺序栈及基本操作
  • 原文地址:https://www.cnblogs.com/Googler/p/4760417.html
Copyright © 2011-2022 走看看