asp.net

位置:IT落伍者 >> asp.net >> 浏览文章

asp.net生成缩略图及给原始图加水印


发布日期:2018年08月03日
 
asp.net生成缩略图及给原始图加水印
生成缩略图及给原始图加水印代码如下

usingSystemIO;

usingSystemDrawingImaging;

privatevoidButton_ServerClick(objectsenderSystemEventArgse)

{

Graphicsg=null;

SystemDrawingImageupimage=null;

SystemDrawingImagethumimg=null;

SystemDrawingImagesimage=null;

Bitmapoutputfile=null;

try

{

stringextension=PathGetExtension(FilePostedFileFileName)ToUpper();

stringfilename=DateTimeNowToString(yyyyMMddhhmmss);

stringsmallpath=ServerMapPath()+/smallimg/;

stringbigpath=ServerMapPath()+/bigimg/;

intwidthheightnewwidthnewheight;

SystemDrawingImageGetThumbnailImageAbortcallb=newSystemDrawingImageGetThumbnailImageAbort(ThumbnailCallback);

if(!DirectoryExists(smallpath))

DirectoryCreateDirectory(smallpath);

if(!DirectoryExists(bigpath))

DirectoryCreateDirectory(bigpath);

Streamupimgfile=FilePostedFileInputStream;

stringsimagefile=ServerMapPath(alogojpg);//要加水印的文件

simage=SystemDrawingImageFromFile(simagefile);

upimage=SystemDrawingImageFromStream(upimgfile);//上传的图片

width=upimageWidth;

height=upimageHeight;

if(width>height)

{

newwidth=;

newheight=(int)((double)height/(double)width*(double)newwidth);

}

else

{

newheight=;

newwidth=(int)((double)width/(double)height*(double)newheight);

}

thumimg=upimageGetThumbnailImage(newwidthnewheightcallbIntPtrZero);

outputfile=newBitmap(upimage);

g=GraphicsFromImage(outputfile);

gDrawImage(simagenewRectangle(upimageWidthsimageWidthupimageHeightsimageHeightupimageWidthupimageHeight)upimageWidthupimageHeightGraphicsUnitPixel);

stringnewpath=bigpath+filename+extension;//原始图路径

stringthumpath=smallpath+filename+extension;//缩略图路径

outputfileSave(newpath);

thumimgSave(thumpath);

outputfileDispose();

}

catch(Exceptionex)

{

throwex;

}

finally

{

if(g!=null)

gDispose();

if(thumimg!=null)

thumimgDispose();

if(upimage!=null)

upimageDispose();

if(simage!=null)

simageDispose();

}

}

publicboolThumbnailCallback()

{

returnfalse;

}

上一篇:ASP.NET图象处理详解(2)

下一篇:利用JS在asp.net中实现左导航页的隐藏