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);

 

转载于:https://www.cnblogs.com/shiliang199508/p/9238868.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值