给图片添加水印图片

 /// <summary>  
        /// 图片位置  
        /// </summary>  
        public enum ImagePosition
        {
            LeftTop,        //左上  
            LeftBottom,    //左下  
            RightTop,       //右上  
            RigthBottom,  //右下  
            TopMiddle,     //顶部居中  
            BottomMiddle, //底部居中  
            Center           //中心  
        }


        public static byte[] AddWaterMark(byte[] imageBytes)
        {


            MemoryStream ms = new MemoryStream(imageBytes);
            Image imgOrign = Image.FromStream(ms);


            string waterRoad = System.AppDomain.CurrentDomain.BaseDirectory;
            Bitmap bm = new Bitmap(waterRoad + "/Images/water_logo.png");
            Image waterImg = bm;
            //加上水印
            Image finalImg = DrawImage(imgOrign, waterImg, 0.4F, ImagePosition.Center, "");
            //透明底色改为白色为底色
            Image colorImg = TransParentToWhite(finalImg);
            return PhotoImageInsert(colorImg);
        }


        /// <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 static Image DrawImage(Image imgOrign,
                                          Image waterImage,
                                          float alpha,
                                          ImagePosition position,
                                          string PicturePath)
        {
            //  
            // 将需要加上水印的图片装载到Image对象中  
            //  


            Image imgPhoto = imgOrign;
            //  
            // 确定其长宽  
            //  
            int phWidth = imgPhoto.Width;
            int phHeight = imgPhoto.Height;


            //  
            // 封装 GDI+ 位图,此位图由图形图像及其属性的像素数据组成。  
            //  PixelFormat.Format24bppRgb
            Bitmap bmPhoto = new Bitmap(phWidth, phHeight);


            //  
            // 设定分辨率  
            //   
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
            //  
            // 定义一个绘图画面用来装载位图  
            //  
            Graphics grPhoto = Graphics.FromImage(bmPhoto);


            //  
            //同样,由于水印是图片,我们也需要定义一个Image来装载它  
            //  
            Image imgWatermark = new Bitmap(waterImage);


            //  
            // 获取水印图片的高度和宽度  
            //  
            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,
                                 yPosOfWm,
                                 wmWidth,
                                 wmHeight),
                                 0,
                                 0,
                                 wmWidth,
                                 wmHeight,
                                 GraphicsUnit.Pixel,
                                 imageAttributes);


            imgPhoto = bmWatermark;
            grPhoto.Dispose();
            grWatermark.Dispose();


            imgWatermark.Dispose();
            return imgPhoto;
        }


        public static byte[] PhotoImageInsert(Image imgPhoto)
        {
            MemoryStream mstream = new MemoryStream();
            imgPhoto.Save(mstream,ImageFormat.Png);
            byte[] byData = new Byte[mstream.Length];
            mstream.Position = 0;
            mstream.Read(byData, 0, byData.Length);
            mstream.Close();
            return byData;
        }
        
        public static Image TransParentToWhite(Image sourceImg)
        {
            var img = sourceImg;
            int width = img.Width;
            int height = img.Height;
            Bitmap bmpTemp = new Bitmap(width, height);


            Graphics g = Graphics.FromImage(bmpTemp);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            g.Clear(System.Drawing.Color.White);
            g.DrawImage(img, new RectangleF(0, 0, width, height), new RectangleF(0, 0, width, height), GraphicsUnit.Pixel);
            g.Save();


            g.Dispose();
            img.Dispose();
            //bmpTemp.Dispose();
            return bmpTemp;
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值