zoukankan      html  css  js  c++  java
  • C# 给图片添加透明的文字、图片水印

    #region 添加水印
            /// <summary>
            /// 添加文字水印
            /// </summary>
            /// <param name="image"></param>
            /// <param name="text"></param>
            /// <param name="fontSize">字体大小</param>
            /// <param name="rectX">水印开始X坐标(自动扣除文字宽度)</param>
            /// <param name="rectY">水印开始Y坐标(自动扣除文字高度</param>
            /// <param name="opacity">0-255 值越大透明度越低</param>
            /// <param name="externName">文件后缀名</param>
            /// <returns></returns>
            public static Image AddTextToImg(Image image, string text, float fontSize, float rectX, float rectY, int opacity, string externName)
            {
    
                Bitmap bitmap = new Bitmap(image, image.Width, image.Height);
    
                Graphics g = Graphics.FromImage(bitmap);          
    
                //下面定义一个矩形区域            
                float rectWidth = text.Length * (fontSize + 10);
                float rectHeight = fontSize + 10;
    
                //声明矩形域
    
                RectangleF textArea = new RectangleF(rectX - rectWidth, rectY - rectHeight, rectWidth, rectHeight);
    
                Font font = new Font("微软雅黑", fontSize, FontStyle.Bold); //定义字体
    
                Brush whiteBrush = new SolidBrush(Color.FromArgb(opacity, 193, 143, 8)); //画文字用
    
                g.DrawString(text, font, whiteBrush, textArea);
    
                MemoryStream ms = new MemoryStream();
    
                //保存图片
                switch (externName)
                {
                    case ".jpg":
                        bitmap.Save(ms, ImageFormat.Jpeg);
                        break;
                    case ".gif":
                        bitmap.Save(ms, ImageFormat.Gif);
                        break;
                    case ".png":
                        bitmap.Save(ms, ImageFormat.Png);
                        break;
                    default:
                        bitmap.Save(ms, ImageFormat.Jpeg);
                        break;
                }
    
    
                Image h_hovercImg = Image.FromStream(ms);
    
                g.Dispose();
    
                bitmap.Dispose();
    
                return h_hovercImg;
    
            }
    
    
            /// <summary>
            /// 添加图片水印
            /// </summary>
            /// <param name="image"></param>
            /// <param name="text"></param>
            /// <param name="rectX">水印开始X坐标(自动扣除图片宽度)</param>
            /// <param name="rectY">水印开始Y坐标(自动扣除图片高度</param>
            /// <param name="opacity">透明度 0-1</param>
            /// <param name="externName">文件后缀名</param>
            /// <returns></returns>
            public static Image AddImgToImg(Image image, Image watermark, float rectX, float rectY, float opacity, string externName)
            {
    
                Bitmap bitmap = new Bitmap(image, image.Width, image.Height);
    
                Graphics g = Graphics.FromImage(bitmap);
    
    
                //下面定义一个矩形区域      
                float rectWidth = watermark.Width + 10;
                float rectHeight = watermark.Height + 10;
    
                //声明矩形域
                RectangleF textArea = new RectangleF(rectX - rectWidth, rectY - rectHeight, rectWidth, rectHeight);
                
                Bitmap w_bitmap = ChangeOpacity(watermark, opacity);
    
                g.DrawImage(w_bitmap, textArea);
    
                MemoryStream ms = new MemoryStream();
    
                //保存图片
                switch (externName)
                {
                    case ".jpg":
                        bitmap.Save(ms, ImageFormat.Jpeg);
                        break;
                    case ".gif":
                        bitmap.Save(ms, ImageFormat.Gif);
                        break;
                    case ".png":
                        bitmap.Save(ms, ImageFormat.Png);
                        break;
                    default:
                        bitmap.Save(ms, ImageFormat.Jpeg);
                        break;
                }
    
                Image h_hovercImg = Image.FromStream(ms);
    
                g.Dispose();
    
                bitmap.Dispose();
                return h_hovercImg;
    
            }
    
            /// <summary>
            /// 改变图片的透明度
            /// </summary>
            /// <param name="img">图片</param>
            /// <param name="opacityvalue">透明度</param>
            /// <returns></returns>
            public static Bitmap ChangeOpacity(Image img, float opacityvalue)
            {
    
                float[][] nArray ={ new float[] {1, 0, 0, 0, 0},
    
                                    new float[] {0, 1, 0, 0, 0},
    
                                    new float[] {0, 0, 1, 0, 0},
    
                                    new float[] {0, 0, 0, opacityvalue, 0},
    
                                    new float[] {0, 0, 0, 0, 1}};
    
                ColorMatrix matrix = new ColorMatrix(nArray);
    
                ImageAttributes attributes = new ImageAttributes();
    
                attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
    
                Image srcImage = img;
    
                Bitmap resultImage = new Bitmap(srcImage.Width, srcImage.Height);
    
                Graphics g = Graphics.FromImage(resultImage);
    
                g.DrawImage(srcImage, new Rectangle(0, 0, srcImage.Width, srcImage.Height), 0, 0, srcImage.Width, srcImage.Height, GraphicsUnit.Pixel, attributes);
    
                return resultImage;
            }
    
            #endregion
    Image img = Image.FromStream(file.InputStream);          
    string externName = Path.GetExtension(file.FileName);
    
    //添加文字水印
    System.Drawing.Image t_img = Upload.AddTextToImg(img, "我是水印",14.0f, img.Width - 10, img.Height - 10, 120, externName);
    
    //添加图片水印
    Image watermark =
    Image.FromFile(Server.MapPath("/content/images/common/logo_main.png"));//水印图片
    System.Drawing.Image t2_img = Upload.AddImgToImg(img, watermark, img.Width - 10, img.Height - 10, 0.5f, externName);
  • 相关阅读:
    十二、 Spring Boot 静态资源处理
    九、 Spring Boot 拦截器
    docker之搭建私有仓库
    docker之Dokcerfile 常用指令
    docker之网络管理
    docker之故障问题解决方案
    docker之搭建LNMP
    docker之容器数据持久化
    都说岁月不饶人,我们又何曾饶过岁月
    docker之容器管理
  • 原文地址:https://www.cnblogs.com/shiliang199508/p/9238868.html
Copyright © 2011-2022 走看看