生成缩略图及给原始图加水印
代码如下
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;
}