N年前寫的在圖片上打浮水印的程式:
/// <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") // 在中間
{
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);
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 );
}