图片添加图片水印
当图片为PNG格式的透明图片时,会添加透明水印;非PNG格式的图片时添加不透明水印.
ImgMask
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
namespace IPImage
{
public class ImgMask
{
/// <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 );
}
}
}
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
namespace IPImage
{
public class ImgMask
{
/// <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 );
}
}
}
透明图片效果:
附源码:下载
posted on 2009-05-24 13:37 huanjian9999 阅读(422) 评论(2) 编辑 收藏 举报