给图片加水印好玩的事,不过也不好玩就是麻烦啊.调了不少次,别的不说了上代码吧
private void WaterMakeString(string sourceFile, string targetFile, string waterString, int postion, int border, float opacity, bool deleteSource, int scale)
{
if (opacity < 0 || opacity > 1)
throw new Exception("透明度的值应该在0~1之间的float型数值");
System.Drawing.Image SourceImage = System.Drawing.Image.FromFile(sourceFile);
//System.Drawing.Image WaterImage = (System.Drawing.Image)CreateCheckCodeImage(waterString);
Bitmap OutPut = new Bitmap(SourceImage);
Graphics GImage = Graphics.FromImage(OutPut);
#region 得到字的长宽
Font drawFont = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular, GraphicsUnit.Pixel);
SizeF crSize;
crSize = GImage.MeasureString( waterString,drawFont);
#region
string w = crSize.Width.ToString();
string h = crSize.Height.ToString();
#region 按比例计算水印图片大小
int width = (int)crSize.Width, height = (int)crSize.Height;
//if (scale > 0)
//{
// float floatScale = (float)scale / 100;
// width = (int)((float)SourceImage.Width * floatScale);
// float zoomScale = (float)width / (float)WaterImage.Width;
// height = (int)(zoomScale * (float)WaterImage.Height);
// if (height > (float)SourceImage.Height * floatScale)
// {
// zoomScale = (float)SourceImage.Height * floatScale / (float)WaterImage.Height;
// width = (int)(zoomScale * (float)WaterImage.Width);
// height = (int)((float)SourceImage.Height * floatScale);
// }
// if (width <= 0 && height < 0)
// {
// width = SourceImage.Width;
// height = SourceImage.Height;
// }
//}
#endregion
#region 设置位置
int IntX = 0;
int IntY = 0;
if (border < 0)
border = 8;
if (postion == 1)
{
IntX = border;
IntY = border;
}
else if (postion == 2)
{
IntX = (SourceImage.Width - width) / 2;
IntY = border;
}
else if (postion == 3)
{
IntX = (SourceImage.Width - width) - border;
IntY = border;
}
else if (postion == 4)
{
IntX = border;
IntY = (SourceImage.Height - height) / 2;
}
else if (postion == 5)
{
IntX = (SourceImage.Width - width) / 2;
IntY = (SourceImage.Height - height) / 2;
}
else if (postion == 6)
{
IntX = (SourceImage.Width - width) - border;
IntY = (SourceImage.Height - height) / 2;
}
else if (postion == 7)
{
IntX = border;
IntY = (SourceImage.Height - height) - border;
}
else if (postion == 8)
{
IntX = (SourceImage.Width - width) / 2;
IntY = (SourceImage.Height - height) - border;
}
else if (postion == 9)
{
IntX = (SourceImage.Width - width) - border;
IntY = (SourceImage.Height - height) - border;
}
else
{
IntX = (SourceImage.Width - width) - 10;
IntY = (SourceImage.Height - height) - 10;
}
#endregion
float[][] nArray ={ new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, opacity, 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix matrix = new ColorMatrix(nArray);
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
//GImage.CompositingQuality = CompositingQuality.HighQuality;
//GImage.DrawImage(WaterImage, new Rectangle(IntX, IntY, width, height), 0, 0, WaterImage.Width, WaterImage.Height, GraphicsUnit.Pixel, attributes);
GImage.CompositingQuality = CompositingQuality.HighQuality;
StringFormat drawFormat = new StringFormat();
drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;
GImage.DrawString(waterString, drawFont, new SolidBrush(Color.Black), new Rectangle(IntX, IntY, width, height));
ImageFormat ImgFormat = GetImageFormat(Path.GetExtension(sourceFile));
SourceImage.Dispose();
OutPut.Save(targetFile, ImgFormat);
GImage.Dispose();
OutPut.Dispose();
//WaterImage.Dispose();
if (deleteSource)
File.Delete(sourceFile);
}