C# Winform中,为图片增加图片水印

4 篇文章 0 订阅
这是一个C#方法,用于在图片上添加水印。方法接受byte[]类型的图片数据,转换为Image对象,然后应用水印图片,调整其大小和位置,最后保存为JPEG并返回结果字节数组。支持设置水印的位置,如左上、右上、中间、右下、左下。
摘要由CSDN通过智能技术生成

        我的入参是byte[](字节数组)类型,所以下面将byte[]转为了image类型,如果小伙伴们入参是image类型的话,改一下方法的参数类型,再将下面的转换去掉即可。

        /// <summary>
        ///   加水印图片
        /// </summary>
        /// <param name="picture_Input">imge 对象(在此图片上加水印)</param>
        /// <param name="iTheImage">Image对象(以此图片为水印)</param>
        /// <param name="_watermarkPosition">水印位置</param>
        /// <param name="_width">被加水印图片的宽</param>
        /// <param name="_height">被加水印图片的高</param>
        public static byte[] GetMaterMarkImg(byte[] picture_Input, Image iTheImage,
         WatermarkPosition _watermarkPosition, int _width = 0, int _height = 0)
        {
            Image watermark = new Bitmap(iTheImage);

            #region 将byte[]类型的图片数据转为img类型
            MemoryStream ms = new MemoryStream(picture_Input);
            Image image = System.Drawing.Image.FromStream(ms);
            #endregion

            Graphics picture = Graphics.FromImage(image);

            if (_width == 0)
            {
                _width = image.Width;
            }
            if (_height == 0)
            {
                _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.3f, 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);

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

            //计算水印图片的比率
            //取背景的1/4宽度来比较
            if ((_width > watermark.Width * 4) && (_height > watermark.Height * 4))
            {
                bl = 1;
            }
            else if ((_width > watermark.Width * 4) && (_height < watermark.Height * 4))
            {
                bl = Convert.ToDouble(_height / 4) / Convert.ToDouble(watermark.Height);

            }
            else if ((_width < watermark.Width * 4) && (_height > watermark.Height * 4))
            {
                bl = Convert.ToDouble(_width / 4) / Convert.ToDouble(watermark.Width);
            }
            else
            {
                if ((_width * watermark.Height) > (_height * watermark.Width))
                {
                    bl = Convert.ToDouble(_height / 4) / Convert.ToDouble(watermark.Height);

                }
                else
                {
                    bl = Convert.ToDouble(_width / 4) / Convert.ToDouble(watermark.Width);

                }

            }

            WatermarkWidth = Convert.ToInt32(watermark.Width * bl);
            WatermarkHeight = Convert.ToInt32(watermark.Height * bl);

            switch (_watermarkPosition)
            {
                case WatermarkPosition.左上:
                    xpos = 10;
                    ypos = 10;
                    break;
                case WatermarkPosition.右上:
                    xpos = _width - WatermarkWidth - 10;
                    ypos = 10;
                    break;
                case WatermarkPosition.中间:
                    xpos = (_width / 2) - (WatermarkWidth / 2);
                    ypos = (_height / 2) - (WatermarkHeight / 2);
                    break;
                case WatermarkPosition.右下:
                    xpos = _width - WatermarkWidth - 10;
                    ypos = _height - WatermarkHeight - 10;
                    break;
                case WatermarkPosition.左下:
                    xpos = 10;
                    ypos = _height - WatermarkHeight - 10;
                    break;
            }
            picture.DrawImage(
             watermark,
             new Rectangle(xpos, ypos, WatermarkWidth, WatermarkHeight),
             0,
             0,
             watermark.Width,
             watermark.Height,
             GraphicsUnit.Pixel,
             imageAttributes);

            watermark.Dispose();
            imageAttributes.Dispose();

            MemoryStream ms_OutPut = new MemoryStream();
            image.Save(ms_OutPut, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] buffer = new byte[ms_OutPut.Length];
            ms_OutPut.Seek(0, SeekOrigin.Begin);
            ms_OutPut.Read(buffer, 0, buffer.Length);

            return buffer;
        }

        public enum WatermarkPosition
        {
            无 = 1000,
            左上 = 1001,
            右上 = 1002,
            中间 = 1003,
            右下 = 1004,
            左下 = 1005
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hero♞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值