c# 图片水印

代码

 public class WaterMark
    {
        private const int XInterval = 380;
        private const int YInterval = 180;
        private Image image = null;
      
        public WaterMark(byte[] ImageByte)
        {
            Stream stream = new MemoryStream(ImageByte);
            if (stream != null)
            {
                image = Image.FromStream(stream);
            }
        }
        public WaterMark(string ImagePath)
        {
            if (ImagePath != null)
            {
                image = Image.FromFile(ImagePath);
            }
        }
        public byte[] Draw(string waterText = "yileichen")
        {
            //使用Graphics.DrawImage方法将图像重新绘制到一个Bitmap对象中,并指定像素格式,从而去除索引 
            using (Bitmap bmp = new Bitmap(image))
            {
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    var width = image.Width;
                    var height = image.Height;
                    var crFont = new Font("arial", 30, FontStyle.Bold);
                    var crSolidBrush = new SolidBrush(Color.FromArgb(70, 120, 120, 120));
                    g.RotateTransform(-30); 

                    for (int xpos = -width / 2; xpos < width * 1.5; xpos += XInterval)
                    {
                        for (int ypos = -height / 2; ypos < height * 1.5; ypos += YInterval)
                        {
                            g.DrawString(waterText, crFont, crSolidBrush, xpos, ypos);
                        }
                    }
                }
                image.Dispose();
                return ImageToByteArray(bmp);
            }
        }

        private byte[] ImageToByteArray(Image img)
        {
            ImageConverter imgconv = new ImageConverter();
            byte[] b = (byte[])imgconv.ConvertTo(img, typeof(byte[]));
            return b;
        }
    }

问题:

1.Graphics.TranslateTransform设置旋转角度不起作用?

http://www.cnblogs.com/yukaizhao/archive/2008/12/02/Graphics_RotateTransform_TranslateTransform_nowork.html

2.无法从带有索引像素格式的图像创建graphics对象

https://www.cnblogs.com/qixuejia/archive/2010/09/03/1817248.html

猜你喜欢

转载自blog.csdn.net/paolei/article/details/79410304