using system.io;
using system.drawing.imaging;
private void button1_serverclick(object sender, system.eventargs e)
{
graphics g=null;
system.drawing.image upimage=null;
system.drawing.image thumimg=null;
system.drawing.image simage=null;
bitmap outputfile=null;
try
{
string extension = path.getextension(file1.postedfile.filename).toupper();
string filename = datetime.now.tostring("yyyymmddhhmmss");
string smallpath = server.mappath(".")+"/smallimg/";
string bigpath = server.mappath(".")+"/bigimg/";
int width,height,newwidth,newheight;
system.drawing.image.getthumbnailimageabort callb =new system.drawing.image.getthumbnailimageabort(thumbnailcallback);
if(!directory.exists(smallpath))
directory.createdirectory(smallpath);
if(!directory.exists(bigpath))
directory.createdirectory(bigpath);
stream upimgfile = file1.postedfile.inputstream;
string simagefile = server.mappath("a8logo.jpg"); //要加水印的文件
simage=system.drawing.image.fromfile(simagefile);
upimage= system.drawing.image.fromstream(upimgfile); //上传的图片
width = upimage.width;
height = upimage.height;
if(width>height)
{
newwidth=200;
newheight =(int)((double)height/(double)width * (double)newwidth);
}
else
{
newheight=200;
newwidth=(int)((double)width/(double)height * (double)newheight);
}
thumimg = upimage.getthumbnailimage(newwidth,newheight,callb,intptr.zero);
outputfile=new bitmap(upimage);
g=graphics.fromimage(outputfile);
g.drawimage(simage,new rectangle(upimage.width-simage.width,upimage.height-simage.height,upimage.width,upimage.height),0,0,upimage.width,upimage.height,graphicsunit.pixel);
string newpath = bigpath + filename + extension; //原始图路径
string thumpath = smallpath + filename + extension; //缩略图路径
outputfile.save(newpath);
thumimg.save(thumpath);
outputfile.dispose();
}
catch(exception ex)
{
throw ex;
}
finally
{
if(g!=null)
g.dispose();
if(thumimg!=null)
thumimg.dispose();
if(upimage!=null)
upimage.dispose();
if(simage!=null)
simage.dispose();
}
}
public bool thumbnailcallback()
{
return false;
}
新闻热点
疑难解答
图片精选