直接贴
/// <summary>
/// 图片加水印
/// </summary>
/// <param name="fileName">源图片文件</param>
/// <param name="text">水印文字</param>
/// <param name="newfile">新图片文件名</param>
public static string PicWaterMark(string SrcFileName,string text,string NewFile)
{
return PicWaterMark(SrcFileName,text,NewFile,20.0f, "宋体", 100f, 100f ) ;
}
/// <summary>
/// 图片加水印
/// </summary>
/// <param name="SrcFileName">源图片文件</param>
/// <param name="text">水印文字</param>
/// <param name="NewFile">新图片文件名</param>
/// <param name="fontSize">字体大小</param>
/// <param name="fontName">字体名称</param>
/// <param name="top">离顶部距离</param>
/// <param name="left">离左边距离</param>
/// <returns>返回失败的错误信息1.返回空错误信息表示成功,2.返回非空,即为相应错误信息</returns>
public static string PicWaterMark(string SrcFileName,string text,string NewFile,float fontSize,string fontName,float top,float left)
{
return PicWaterMark( SrcFileName, text, NewFile, fontSize, fontName, top, left,false);
}
/// <summary>
/// 图片加水印
/// </summary>
/// <param name="SrcFileName">源图片文件</param>
/// <param name="text">水印文字</param>
/// <param name="NewFile">新图片文件名</param>
/// <param name="fontSize">字体大小</param>
/// <param name="fontName">字体名称</param>
/// <param name="top">离顶部距离</param>
/// <param name="left">离左边距离</param>
/// <param name="saveNew">新的图片保存方式,如果用此方式,则图片经过编码处理,显示更加清晰,但是size很大;true为新的方式,false为不经过处理方式,图片比较小</param>
/// <returns>返回失败的错误信息1.返回空错误信息表示成功,2.返回非空,即为相应错误信息</returns>
public static string PicWaterMark(string SrcFileName,string text,string NewFile,float fontSize,string fontName,float top,float left,bool saveNew)
{
if(!File.Exists(SrcFileName))
{
throw new FileNotFoundException("The file don't exist!");
}
System.Drawing.Image imgSrc=null;
Bitmap bmpSrc =null;
Graphics g=null;
try
{
//读取源图片文件创建了一个Image 对象
imgSrc = System.Drawing.Image.FromFile(SrcFileName);
int imgSrcWidth =imgSrc.Width;
int imgSrcHeight=imgSrc.Height;
//建立一个Format24bppRgb格式 颜色数据的Bitmap对象
bmpSrc = new Bitmap(imgSrcWidth,imgSrcHeight,
PixelFormat.Format24bppRgb);
//从源文件创建的image中创建一个graphics对象
g = Graphics.FromImage(bmpSrc);
//设置g的呈现质量
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
//按原始大小绘制imgSrc 到Graphics 对象的(x=0,y=0)位置。
g.DrawImage(imgSrc,
new System.Drawing.Rectangle(0,0,imgSrcWidth,imgSrcHeight),
0,0,imgSrcWidth,imgSrcHeight,
GraphicsUnit.Pixel);
//定义字体
System.Drawing.Font textFont = new System.Drawing.Font(fontName,fontSize);
System.Drawing.Color textColor=System.Drawing.Color.Red;
//定义画刷
SolidBrush textBrush = new SolidBrush(textColor);
StringFormat StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Center;
float textWidth = text.Length*fontSize; //文本的长度
//下面定义一个矩形区域,以后在这个矩形里画上白底黑字
float rectX = top;
float rectY = left;
float rectWidth = text.Length*(fontSize+8);
float rectHeight = fontSize+8;
//声明矩形域
RectangleF textArea = new RectangleF(rectX,rectY,rectWidth,rectHeight);
Brush emptyBrush = new SolidBrush(System.Drawing.Color.Empty); //白笔刷,画背景用
g.FillRectangle(emptyBrush,rectX,rectY,rectWidth,rectHeight);
g.DrawString(text,textFont,textBrush,textArea, StrFormat);
//销毁两个Graphic对象,然后把Image 保存到文件系统。,ImageFormat.Jpeg
if (saveNew==true)
SaveImg(bmpSrc,NewFile);
else
bmpSrc.Save(NewFile);
//bmpSrc.Save(NewFile,ImageFormat.Jpeg );
ErrorString="";
}
catch(Exception ex)
{
ErrorString=ex.Message.Trim();
}
finally
{
if (bmpSrc!=null)
bmpSrc.Dispose();
if (imgSrc!=null)
imgSrc.Dispose();
if (g!=null)
g.Dispose();
}
return ErrorString;
}
#region "----------image"
public static void SaveImg(Bitmap bitmap,string NewFileName)
{
long level=60;
ImageCodecInfo myImageCodecInfo;
System.Drawing.Imaging.Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;
myImageCodecInfo =System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[0];//GetEncoderInfo( "image/jpeg ");
// Create an Encoder object based on the GUID
// for the Quality parameter category.
myEncoder = System.Drawing.Imaging.Encoder.Quality;
// Create an EncoderParameters object.
// An EncoderParameters object has an array of EncoderParameter
// objects. In this case, there is only one
// EncoderParameter object in the array.
myEncoderParameters = new EncoderParameters(1);
// Save the bitmap as a JPEG file with 给定的 quality level
myEncoderParameter = new EncoderParameter(myEncoder, level);
myEncoderParameters.Param[0] = myEncoderParameter;
bitmap.Save(NewFileName, myImageCodecInfo, myEncoderParameters);
}