生成图片水印

Posted 君莫笑·秋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了生成图片水印相关的知识,希望对你有一定的参考价值。

public static void AddWaterText(string oldpath,string savepath,string watertext,WaterPositionMode position,string color,int alpha)
        {
            Image image = Image.FromFile(oldpath);
            Bitmap bitmap=new Bitmap(image.Width,image.Height);
            Graphics graphics = Graphics.FromImage(bitmap);
            graphics.Clear(Color.White);
            graphics.DrawImage(image,new Rectangle(0,0,image.Width,image.Height),0,0,image.Width,image.Height,GraphicsUnit.Pixel);
            Font font=new Font("arial",18);
            SizeF ziSizeF=new SizeF();
            ziSizeF = graphics.MeasureString(watertext, font);
            float x = 0f;
            float y = 0f;
            switch (position)
            {
                case  WaterPositionMode.LeftTop:
                    x = ziSizeF.Width/2f;
                    y = 8f;
                    break;
                case WaterPositionMode.LeftBottom:
                    x = ziSizeF.Width/2f;
                    y = image.Height - ziSizeF.Height;
                    break;
                case WaterPositionMode.RightTop:
                    x = image.Width*1f - ziSizeF.Width/2f;
                    y = 8f;
                    break;
                case WaterPositionMode.RightBottom:
                    x = image.Width - ziSizeF.Width;
                    y = image.Height - ziSizeF.Height;
                    break;
                case WaterPositionMode.Center:
                    x = image.Width/2;
                    y = image.Height/2 - ziSizeF.Height/2;
                    break;
            }
            try
            {
                StringFormat stringFormat = new StringFormat {Alignment = StringAlignment.Center};
                SolidBrush solidBrush = new SolidBrush(Color.FromArgb(alpha, 0, 0, 0));
                graphics.DrawString(watertext, font, solidBrush, x + 1f, y + 1f, stringFormat);
                SolidBrush brush = new SolidBrush(Color.FromArgb(alpha, ColorTranslator.Fromhtml(color)));
                graphics.DrawString(watertext, font, brush, x, y, stringFormat);
                solidBrush.Dispose();
                brush.Dispose();
                bitmap.Save(savepath, ImageFormat.Jpeg);
            }
            catch (Exception e)
            {


            }
            finally
            {
                bitmap.Dispose();
                image.Dispose();
            }
            
        }

public enum WaterPositionMode
    {
        LeftTop,
        LeftBottom,
        RightTop,
        RightBottom,
        Center
    }

 

以上是关于生成图片水印的主要内容,如果未能解决你的问题,请参考以下文章

ASP组件AspJpeg(加水印)生成缩略图等使用方法

图片水印的生成

python库pillow:实现生成图片并加水印

java如何实现在web工程中用OpenOffice生成带有图片水印的pdf

使用FileUpload控件上传图片并自动生成缩略图自动生成带文字和图片的水印图

ios -bitmap上下文生成图片 生成水印