In C# Winform, add text watermark to the picture

        I have time today, record the text watermark added to the picture in the winform project, and write an article by the way.

        My input parameter is byte[] (byte array) type, so I will convert byte[] to image type below. If the input parameter is image, please change the parameter type of the method, and then remove the conversion below.

        /// <summary>
        /// 加文字水印
        /// </summary>
        /// <param name="picture_Input">imge 对象(在此图片上加水印)</param>
        /// <param name="watermarkTxt">水印文字</param>
        /// <param name="_watermarkPosition">水印位置</param>
        /// <returns></returns>
        public static byte[] GetMaterMarkText(byte[] picture_Input, string watermarkTxt, WatermarkPosition _watermarkPosition)
        {
            Bitmap bt = null;
            float fontSize = 0;
            Color fontColor = Color.Black;
            int Fwidth;
            int Fheight;
            Bitmap BigBt;
            Font f;
            Brush b;

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

            if (image.VerticalResolution < 150)
            {
                fontSize = 60;
            }
            else
            {
                fontSize = 20;
            }

            b = new SolidBrush(fontColor);
            bt = new Bitmap(368, 75);
            BigBt = new Bitmap(image);
            Graphics g = Graphics.FromImage(bt);
            Graphics g1 = Graphics.FromImage(image);
            g.Clear(Color.Gainsboro);
            f = new Font(watermarkTxt, fontSize);
            SizeF XMaxSize = g.MeasureString(watermarkTxt, f);
            Fwidth = (int)XMaxSize.Width;
            Fheight = (int)XMaxSize.Height;
            g.DrawString(watermarkTxt, f, b, (int)(368 - Fwidth) / 2, (int)(75 - Fheight) / 2);

            int xpos = 0;
            int ypos = 0;
            switch (_watermarkPosition)
            {
                case WatermarkPosition.左上:
                    xpos = 30;
                    ypos = 30;
                    break;
                case WatermarkPosition.右上:
                    xpos = BigBt.Width - Fwidth - 80;
                    ypos = 30;
                    break;
                case WatermarkPosition.中间:
                    xpos = (BigBt.Width / 2) - (Fwidth / 2);
                    ypos = (BigBt.Height / 2) - (Fheight / 2);
                    break;
                case WatermarkPosition.右下:
                    xpos = BigBt.Width - Fwidth - 80;
                    ypos = BigBt.Height - Fheight - 80;
                    break;
                case WatermarkPosition.左下:
                    xpos = 30;
                    ypos = BigBt.Height - Fheight - 80;
                    break;
            }

            g1.DrawString(watermarkTxt, f, b, xpos, ypos);
            g1.Dispose();
            BigBt.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
        }

As always, if you have any questions, welcome to communicate in the comment area.

Guess you like

Origin blog.csdn.net/weixin_45963929/article/details/131227642