public enum WaterPositionMode
{
LeftTop,
LeftBottom,
RightTop,
RightBottom,
Center
}
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();
}
}
123abcd321 发表于 2014-10-16 14:42
能不能把这个实例发给我呀。我刚进公司,测试题就是这个。。。上传图片还要带水印,我能上传,但是不会弄水 ...
cload 发表于 2014-10-16 20:06
上面的几乎就是实例啊,你还要什么?
自己动手,丰衣足食嘛!
dyrne 发表于 2016-1-10 15:12
不知道,是否有去除水印的例子
欢迎光临 苏飞论坛 (http://www.sufeinet.com/) | Powered by Discuz! X3.4 |