在ASP.NET中实现在图片上打水印的程序代码:
/// <summary>
/// 在图片上加入图片版权信息
/// </summary>
/// <param name=\"strFileName\">输入图片(绝对路径)</param>
/// <param name=\"strOutfileName\">输出图片(绝对路径)</param>
/// <param name=\"strCopyRightFile\">水印图片(绝对路径)</param>
/// <param name=\"StrPlace\">水印位置</param>
/// <param name=\"bolFileName\">是否删除输入图片</param>
public static void CreateCopyRightImage(string strFileName, string strOutfileName, string strCopyRightFile, string StrPlace, bool bolFileName)
{
System.Drawing.Image MyImage = System.Drawing.Image.FromFile( strFileName );
System.Drawing.Image CopyMyImage = System.Drawing.Image.FromFile( strCopyRightFile );
System.Drawing.Bitmap OutPut = new System.Drawing.Bitmap( MyImage );
System.Drawing.Graphics GImage = System.Drawing.Graphics.FromImage( OutPut );
int IntX = 0;
int IntY = 0;
if (StrPlace == \"1\") // 在左上
{
IntX = 0;
IntY = 0;
}
else if (StrPlace == \"2\") // 在正上
{
IntX = (MyImage.Width-CopyMyImage.Width)/2;
IntY = 0;
}
else if (StrPlace == \"3\") // 在右上
{
IntX = MyImage.Width-CopyMyImage.Width;
IntY = 0;
}
else if (StrPlace == \"4\") // 在正左
{
IntX = 0;
IntY = (MyImage.Height-CopyMyImage.Height)/2;
}
else if (StrPlace == \"5\") // 在中间
{ [Page]
IntX = (MyImage.Width-CopyMyImage.Width)/2;
IntY = (MyImage.Height-CopyMyImage.Height)/2;
}
else if (StrPlace == \"6\") // 在正右
{
IntX = MyImage.Width-CopyMyImage.Width;
IntY = (MyImage.Height-CopyMyImage.Height)/2;
}
else if (StrPlace == \"7\") // 在左下
{
IntX = 0;
IntY = MyImage.Height-CopyMyImage.Height;
}
else if (StrPlace == \"8\") // 在正下
{
IntX = (MyImage.Width-CopyMyImage.Width)/2;
IntY = MyImage.Height-CopyMyImage.Height;
}
else if (StrPlace == \"9\") // 在右下
{
IntX = MyImage.Width-CopyMyImage.Width;
IntY = MyImage.Height-CopyMyImage.Height;
}
else
{
IntX = MyImage.Width-CopyMyImage.Width-10;
IntY = MyImage.Height-CopyMyImage.Height-10;
}
// 画出水印的位置
GImage.DrawImage(CopyMyImage, IntX, IntY, CopyMyImage.Width, CopyMyImage.Height);
string strExtend = strFileName.Substring(strFileName.LastIndexOf(\".\") + 1).ToLower();
switch ( strExtend )
{
case \"bmp\" :
OutPut.Save(strOutfileName, System.Drawing.Imaging.ImageFormat.Bmp);
break;
case \"jpg\" :
OutPut.Save(strOutfileName, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case \"gif\" :
OutPut.Save(strOutfileName, System.Drawing.Imaging.ImageFormat.Gif);
break;
case \"icon\" :
OutPut.Save(strOutfileName, System.Drawing.Imaging.ImageFormat.Icon); [Page]
break;
case \"png\" :
OutPut.Save(strOutfileName, System.Drawing.Imaging.ImageFormat.Png);
break;
case \"tif\" :
OutPut.Save(strOutfileName, System.Drawing.Imaging.ImageFormat.Tiff);
break;
default :
OutPut.Save(strOutfileName, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
}
GImage.Dispose();
OutPut.Dispose();
MyImage.Dispose();
CopyMyImage.Dispose();
if( bolFileName )
File.Delete( strFileName );
}