C#上传图片 并按尺寸新生成一张略缩图

上传图片并生成略缩图        
   
   
  参数说明  
  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;   }   
      
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值