主页 > 编程资料 > C# >
发布时间:2015-09-26 作者:网络 阅读:250次

参数说明
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){
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; }

关键字词: