C#利用原图和水印图的重叠简单实现水印的方法
本文实例讲述了C#利用原图和水印图的重叠简单实现水印的方法。分享给大家供大家参考,具体如下:
图片操作类
///<summary>
///获取一个图片按等比例缩小后的大小。
///</summary>
///<paramname="maxWidth">需要缩小到的宽度</param>
///<paramname="maxHeight">需要缩小到的高度</param>
///<paramname="imageOriginalWidth">图片的原始宽度</param>
///<paramname="imageOriginalHeight">图片的原始高度</param>
///<returns>返回图片按等比例缩小后的实际大小</returns>
publicstaticSizeGetNewSize(intmaxWidth,intmaxHeight,intimageOriginalWidth,intimageOriginalHeight)
{
doublew=0.0;
doubleh=0.0;
doublesw=Convert.ToDouble(imageOriginalWidth);
doublesh=Convert.ToDouble(imageOriginalHeight);
doublemw=Convert.ToDouble(maxWidth);
doublemh=Convert.ToDouble(maxHeight);
if(sw<mw&&sh<mh)
{
w=sw;
h=sh;
}
elseif((sw/sh)>(mw/mh))
{
w=maxWidth;
h=(w*sh)/sw;
}
else
{
h=maxHeight;
w=(h*sw)/sh;
}
returnnewSize(Convert.ToInt32(w),Convert.ToInt32(h));
}
///<summary>
///对给定的一个图片(Image对象)生成一个指定大小的缩略图。
///</summary>
///<paramname="originalImage">原始图片</param>
///<paramname="thumMaxWidth">缩略图的宽度</param>
///<paramname="thumMaxHeight">缩略图的高度</param>
///<returns>返回缩略图的Image对象</returns>
publicstaticSystem.Drawing.ImageGetThumbNailImage(System.Drawing.ImageoriginalImage,intthumMaxWidth,intthumMaxHeight)
{
SizethumRealSize=Size.Empty;
System.Drawing.ImagenewImage=originalImage;
Graphicsgraphics=null;
try
{
thumRealSize=GetNewSize(thumMaxWidth,thumMaxHeight,originalImage.Width,originalImage.Height);
newImage=newBitmap(thumRealSize.Width,thumRealSize.Height);
graphics=Graphics.FromImage(newImage);
graphics.CompositingQuality=CompositingQuality.HighQuality;
graphics.InterpolationMode=InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode=SmoothingMode.HighQuality;
graphics.Clear(Color.Transparent);
graphics.DrawImage(originalImage,newRectangle(0,0,thumRealSize.Width,thumRealSize.Height),newRectangle(0,0,originalImage.Width,originalImage.Height),GraphicsUnit.Pixel);
}
catch{}
finally
{
if(graphics!=null)
{
graphics.Dispose();
graphics=null;
}
}
returnnewImage;
}
///<summary>
///对给定的一个图片文件生成一个指定大小的缩略图。
///</summary>
///<paramname="originalImage">图片的物理文件地址</param>
///<paramname="thumMaxWidth">缩略图的宽度</param>
///<paramname="thumMaxHeight">缩略图的高度</param>
///<returns>返回缩略图的Image对象</returns>
publicstaticSystem.Drawing.ImageGetThumbNailImage(stringimageFile,intthumMaxWidth,intthumMaxHeight)
{
System.Drawing.ImageoriginalImage=null;
System.Drawing.ImagenewImage=null;
try
{
originalImage=System.Drawing.Image.FromFile(imageFile);
newImage=GetThumbNailImage(originalImage,thumMaxWidth,thumMaxHeight);
}
catch{}
finally
{
if(originalImage!=null)
{
originalImage.Dispose();
originalImage=null;
}
}
returnnewImage;
}
///<summary>
///对给定的一个图片文件生成一个指定大小的缩略图,并将缩略图保存到指定位置。
///</summary>
///<paramname="originalImageFile">图片的物理文件地址</param>
///<paramname="thumbNailImageFile">缩略图的物理文件地址</param>
///<paramname="thumMaxWidth">缩略图的宽度</param>
///<paramname="thumMaxHeight">缩略图的高度</param>
publicstaticvoidMakeThumbNail(stringoriginalImageFile,stringthumbNailImageFile,intthumMaxWidth,intthumMaxHeight)
{
System.Drawing.ImagenewImage=GetThumbNailImage(originalImageFile,thumMaxWidth,thumMaxHeight);
try
{
newImage.Save(thumbNailImageFile,ImageFormat.Jpeg);
}
catch
{}
finally
{
newImage.Dispose();
newImage=null;
}
}
///<summary>
///将一个图片的内存流调整为指定大小,并返回调整后的内存流。
///</summary>
///<paramname="originalImageStream">原始图片的内存流</param>
///<paramname="newWidth">新图片的宽度</param>
///<paramname="newHeight">新图片的高度</param>
///<returns>返回调整后的图片的内存流</returns>
publicstaticMemoryStreamResizeImage(StreamoriginalImageStream,intnewWidth,intnewHeight)
{
MemoryStreamnewImageStream=null;
System.Drawing.ImagenewImage=Globals.GetThumbNailImage(System.Drawing.Image.FromStream(originalImageStream),newWidth,newHeight);
if(newImage!=null)
{
newImageStream=newMemoryStream();
newImage.Save(newImageStream,ImageFormat.Jpeg);
}
returnnewImageStream;
}
///<summary>
///将一个内存流保存为磁盘文件。
///</summary>
///<paramname="stream">内存流</param>
///<paramname="newFile">目标磁盘文件地址</param>
publicstaticvoidSaveStreamToFile(Streamstream,stringnewFile)
{
if(stream==null||stream.Length==0||string.IsNullOrEmpty(newFile))
{
return;
}
byte[]buffer=newbyte[stream.Length];
stream.Position=0;
stream.Read(buffer,0,buffer.Length);
FileStreamfileStream=newFileStream(newFile,FileMode.OpenOrCreate,FileAccess.Write);
fileStream.Write(buffer,0,buffer.Length);
fileStream.Flush();
fileStream.Close();
fileStream.Dispose();
}
///<summary>
///对一个指定的图片加上图片水印效果。
///</summary>
///<paramname="imageFile">图片文件地址</param>
///<paramname="waterImage">水印图片(Image对象)</param>
publicstaticvoidCreateImageWaterMark(stringimageFile,System.Drawing.ImagewaterImage)
{
if(string.IsNullOrEmpty(imageFile)||!File.Exists(imageFile)||waterImage==null)
{
return;
}
System.Drawing.ImageoriginalImage=System.Drawing.Image.FromFile(imageFile);
if(originalImage.Width-10<waterImage.Width||originalImage.Height-10<waterImage.Height)
{
return;
}
Graphicsgraphics=Graphics.FromImage(originalImage);
intx=originalImage.Width-waterImage.Width-10;
inty=originalImage.Height-waterImage.Height-10;
intwidth=waterImage.Width;
intheight=waterImage.Height;
graphics.DrawImage(waterImage,newRectangle(x,y,width,height),0,0,width,height,GraphicsUnit.Pixel);
graphics.Dispose();
MemoryStreamstream=newMemoryStream();
originalImage.Save(stream,ImageFormat.Jpeg);
originalImage.Dispose();
System.Drawing.ImageimageWithWater=System.Drawing.Image.FromStream(stream);
imageWithWater.Save(imageFile);
imageWithWater.Dispose();
}
///<summary>
///对一个指定的图片加上文字水印效果。
///</summary>
///<paramname="imageFile">图片文件地址</param>
///<paramname="waterText">水印文字内容</param>
publicstaticvoidCreateTextWaterMark(stringimageFile,stringwaterText)
{
if(string.IsNullOrEmpty(imageFile)||string.IsNullOrEmpty(waterText)||!File.Exists(imageFile))
{
return;
}
System.Drawing.ImageoriginalImage=System.Drawing.Image.FromFile(imageFile);
Graphicsgraphics=Graphics.FromImage(originalImage);
graphics.SmoothingMode=SmoothingMode.HighQuality;
graphics.TextRenderingHint=TextRenderingHint.ClearTypeGridFit;
graphics.CompositingQuality=CompositingQuality.HighQuality;
graphics.InterpolationMode=InterpolationMode.HighQualityBicubic;
SolidBrushbrush=newSolidBrush(Color.FromArgb(153,255,255,255));
FontwaterTextFont=newFont("Arial",16,FontStyle.Regular);
SizeFwaterTextSize=graphics.MeasureString(waterText,waterTextFont);
floatx=(float)originalImage.Width-waterTextSize.Width-10F;
floaty=(float)originalImage.Height-waterTextSize.Height-10F;
graphics.DrawString(waterText,waterTextFont,brush,x,y);
graphics.Dispose();
brush.Dispose();
MemoryStreamstream=newMemoryStream();
originalImage.Save(stream,ImageFormat.Jpeg);
originalImage.Dispose();
System.Drawing.ImageimageWithWater=System.Drawing.Image.FromStream(stream);
imageWithWater.Save(imageFile);
imageWithWater.Dispose();
}
///<summary>
///判断上传组件是否包含内容。
///</summary>
///<paramname="fileUpload">ASP.NET2.0标准上传组件</param>
///<returns>如果数据有效,则返回True,否则返回False</returns>
publicstaticboolIsAttachmentValid(FileUploadfileUpload)
{
if(fileUpload!=null&&
fileUpload.PostedFile!=null&&
!string.IsNullOrEmpty(fileUpload.PostedFile.FileName)&&
fileUpload.PostedFile.ContentLength>0)
{
returntrue;
}
returnfalse;
}
publicclassImageHelper
{
#region"水印存放的相对路径"
publicstaticstringGetLogoPath()
{
return"/images/logo.png";///水印图路径
}
#endregion
#region"图片水印"
//<summary>
//在图片上生成图片水印,此方法不支持Gif类型的图片
//</summary>
//<paramname="Path">原服务器图片路径</param>
//<paramname="Path_syp">生成的带图片水印的图片路径</param>
//<paramname="Path_sypf">水印图片路径</param>
publicstaticvoidMarkImage(StreamInUploadImagePath,stringinLogoImagePath,stringInSavePath)
{
System.Drawing.ImageImage=System.Drawing.Image.FromStream(InUploadImagePath);
System.Drawing.Imagenewimage=Image.FromFile(Current.Server.MapPath(inLogoImagePath));
Graphicsg=Graphics.FromImage(Image);
g.DrawImage(newimage,newRectangle(Image.Width-newimage.Width,Image.Height-newimage.Height,newimage.Width,newimage.Height),0,0,newimage.Width,newimage.Height,GraphicsUnit.Pixel);
try{
Image.Save(Current.Server.MapPath(InSavePath));
}
catch(Exceptionex){
}
finally{
g.Dispose();
Image.Dispose();
newimage.Dispose();
}
}
#endregion
}
更多关于C#相关内容感兴趣的读者可查看本站专题:《C#图片操作技巧汇总》、《WinForm控件用法总结》、《C#数据结构与算法教程》、《C#常见控件用法教程》、《C#面向对象程序设计入门教程》及《C#程序设计之线程使用技巧总结》
希望本文所述对大家C#程序设计有所帮助。