参数说明
filebox:上传表单中的表单名
savepath:保存路径
thumbnailwidth:略缩图宽
thumbnailheight:略缩图高
filepath:文件保存路径(输出参数)
filename:文件名称(输出参数)
lasterror:(输出错误)
public static bool uploadimages(system.web.ui.htmlcontrols.htmlinputfile filebox,string savepath,int32 thumbnailwidth,int32 thumbnailheight,out string filepath,out string filename,out string lasterror)
{
datetime dattime=system.datetime.now;
string strtemp;
string strfilename = filebox.postedfile.filename;
bool blnresult;
lasterror="";
system.drawing.image ximage;
system.drawing.bitmap xbitmap;
int photoheight,photowidth;
string basepath=system.web.httpcontext.current.server.mappath("../");
rectangle newphoto;
system.drawing.imaging.imageformat xobject;
string extension="";
filename="";
filepath="";
try
{
if(strfilename=="")
{
lasterror="请点击浏览选择要上传的文件!";
return false;
}
if(convert.todecimal(filebox.postedfile.contentlength)<= 2000000 &&convert.todecimal(filebox.postedfile.contentlength)>1500)
{
//查看扩展名是否合法
extension=path.getextension(strfilename);
extension=extension.tolower();
if(extension==".gif" || extension==".jpg" || extension==".jpeg" || extension==".bmp")
{
switch(extension)
{
case ".gif":
xobject=system.drawing.imaging.imageformat.gif;
break;
case ".bmp":
xobject=system.drawing.imaging.imageformat.bmp;
break;
default:
xobject=system.drawing.imaging.imageformat.jpeg;
break;
}
}
else
{
lasterror="上传的文件不是有效的图片文件,请上传格式为:.gif.jpg.bmp的文件!";
return false;
}
//-----------------------------
//生成保存的文件路径及文件名
//-----------------------------
strtemp = dattime.toshortdatestring().replace( "-", "");
filename = dattime.tolongtimestring().replace(":","")+ extension;
filepath = savepath + strtemp + "/";
friendlib.function.createdirectory(basepath + filepath);
//保存大图
ximage=system.drawing.bitmap.fromstream(filebox.postedfile.inputstream);
ximage.save(basepath+filepath+filename);
ximage.dispose();
//-----------------------------
//开始生成缩略图
//-----------------------------
xbitmap=new bitmap(basepath+filepath+filename);//------------------
photoheight=xbitmap.height;
photowidth=xbitmap.width;
// //判断图片的形状
if(convert.todecimal(photoheight)/convert.todecimal(photowidth)>convert.todecimal(thumbnailheight)/convert.todecimal(thumbnailwidth))//高有余
{
photoheight=convert.toint16((convert.todecimal(thumbnailheight)/convert.todecimal(thumbnailwidth))*convert.todecimal(photowidth));
newphoto=new rectangle(0,0,photowidth,photoheight);
}
else if(convert.todecimal(photoheight)/convert.todecimal(photowidth)<convert.todecimal(thumbnailheight)/convert.todecimal(thumbnailwidth))
{
photowidth=convert.toint16((convert.todecimal(thumbnailwidth)/convert.todecimal(thumbnailheight))*convert.todecimal(photoheight));
newphoto=new
rectangle(convert.toint16((convert.todecimal(xbitmap.width)-convert.todecimal(photowidth))/2),0,photowidth,photoheight);
}
else
{
newphoto= newrectangle(0,0,photowidth,photoheight); < br> }
system.drawing.image mybitmap;
mybitmap=xbitmap.clone(newphoto,system.drawing.imaging.pixelformat.dontcare);
system.drawing.image.getthumbnailimageabort mycallback = new
system.drawing.image.getthumbnailimageabort(thumbnailcallback);
system.drawing.image mythumbnail =
mybitmap.getthumbnailimage(thumbnailwidth,thumbnailheight, mycallback,
intptr.zero); mythumbnail.save(basepath + filepath +"s"+filename,xobject);
mythumbnail.dispose(); mybitmap.dispose(); xbitmap.dispose(); return true; }
else
{
blnresult=false; lasterror="不能上传大于2m及小于1.5k的图片!";
}
}< br> catch (exception ex) {
throw new system.exception((ex.message + ("/r/n" + ex.stacktrace)));
}
return blnresult;
}
private static bool thumbnailcallback() { return false; }
新闻热点
疑难解答