1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 生成jpg的缩略图并添加水印

生成jpg的缩略图并添加水印

时间:2019-03-04 15:29:25

相关推荐

生成jpg的缩略图并添加水印

改进版本v1.1。解决了生成较大缩略图时质量下降问题。现在的版本正确的叫法应改是缩放图了。哈哈

usingSystem;

usingSystem.Drawing;

usingSystem.Drawing.Imaging;

namespaceOnest.Cyclone.Business.util

{

///<summary>

///缩略图

///</summary>

publicclassMiniature

{

privateMiniature()

{

}

publicstaticvoidConvert(System.IO.Streamsrc,stringnewImagePath,intWidth,intHeight,stringCopyright)

{

System.Drawing.Imageoldimage=Image.FromStream(src,true,true);

intwidth;//缩略图的宽度

intheight;//缩略图的高度

if(oldimage.Width>oldimage.Height)//横向,颠倒一下尺寸设置

{

width=Height;//缩略图的宽度

height=Width;//缩略图的高度

}

else//纵向

{

//oldImagePath-原图地址newImagePath生成缩略图地址

width=Width;//缩略图的宽度

height=Height;//缩略图的高度

}

intlevel=100;//缩略图的质量1-100的范围

//System.Drawing.Imageoldimage=System.Drawing.Image.FromFile(oldImagePath);

//System.Drawing.ImagethumbnailImage=oldimage.GetThumbnailImage(width,height,newSystem.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback),IntPtr.Zero);

//Bitmapbm=newBitmap(thumbnailImage);

//newmethod

Bitmapbm=newBitmap(width,height);

Graphicsg=Graphics.FromImage(bm);

g.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;

g.DrawImage(oldimage,newRectangle(0,0,bm.Width,bm.Height),0,0,

oldimage.Width,oldimage.Height,GraphicsUnit.Pixel);

Graphics.FromImage(oldimage).DrawImage(bm,0,0,width,height);

//处理JPG质量的函数

ImageCodecInfo[]codecs=ImageCodecInfo.GetImageEncoders();

ImageCodecInfoici=null;

foreach(ImageCodecInfocodecincodecs)

{

if(codec.MimeType=="image/jpeg")

ici=codec;

}

EncoderParametersep=newEncoderParameters();

ep.Param[0]=newEncoderParameter(Encoder.Quality,(long)level);

#region制作水印

GraphicsgrPhoto=Graphics.FromImage(bm);

//用指定Sizes绘制图片时,测量用指定的字串宽度

//取可能的最大宽度

int[]sizes=newint[]{64,48,32,16,8,6,4};

FontcrFont=null;

SizeFcrSize=newSizeF();

for(inti=0;i<7;i++)

{

crFont=newFont("Verdana",sizes[i],

FontStyle.Bold);

crSize=grPhoto.MeasureString(Copyright,

crFont);

if((ushort)crSize.Width<(ushort)width)

break;

}

//指定做图点

intyPixlesFromBottom=(int)(height*.05);

floatyPosFromBottom=((height-

yPixlesFromBottom)-(crSize.Height/2));

floatxCenterOfImg=(width/2);

StringFormatStrFormat=newStringFormat();

StrFormat.Alignment=StringAlignment.Center;

//绘制copyright

SolidBrushsemiTransBrush2=newSolidBrush(Color.FromArgb(100,0,0,0));

grPhoto.DrawString(Copyright,crFont,semiTransBrush2,newPointF(xCenterOfImg+1,yPosFromBottom+1),StrFormat);

SolidBrushsemiTransBrush=newSolidBrush(Color.FromArgb(100,255,255,255));

grPhoto.DrawString(Copyright,crFont,semiTransBrush,newPointF(xCenterOfImg,yPosFromBottom),StrFormat);

#endregion

bm.Save(newImagePath,ici,ep);

try

{

//释放资源

ep.Dispose();

bm.Dispose();

oldimage.Dispose();

g.Dispose();

codecs=null;

StrFormat.Dispose();

semiTransBrush2.Dispose();

grPhoto.Dispose();

semiTransBrush.Dispose();

sizes=null;

}

catch{}

}

publicstaticvoidConvert(stringoldImagePath,stringnewImagePath,intWidth,intHeight,stringCopyright)

{

System.IO.StreamReadersr=newSystem.IO.StreamReader(oldImagePath);

Convert(sr.BaseStream,newImagePath,Width,Height,Copyright);

}

privatestaticboolThumbnailCallback(){returntrue;}

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。