ITeye博客

.net如何上传图片,并生成缩略图效果

oottpp · 2012-01-17 00:29 · 技术杂绘 · 非技术 · Markdown

.net如何上传图片,并生成缩略图效果 2011年04月18日    很晚了。就不和大家废话了。搞了一晚才搞定。和大家分享一下啊:      请看代码:   protected void btnUpLoad_Click(object sender, EventArgs e)   {   //判断是否有文件提交   if (HttpContext.Current.Request.Files.Count != 0)   {   string path = HttpContext.Current.Request.ApplicationPath;   HttpPostedFile postFile = HttpContext.Current.Request.Files;   string filename = DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();   SavePostedImage(postFile, path + "/UploadImg/", filename, 100, 100, System.Drawing.Imaging.ImageFormat.Jpeg, 0.8);   filename += "-isBig";   SavePostedImage(postFile, path + "/UploadImg/", filename, 502, 670, System.Drawing.Imaging.ImageFormat.Jpeg, 0.8);   postFile.InputStream.Close();   }   }   ///   /// 上传图片 集成cmyk→rgb   ///   /// 上载文件的访问   /// 保存目录   /// 新名字   /// 限高   /// 限宽   /// 图片格式要求   /// 压缩比(double:0--1)   /// 上传后的文件名   public string SavePostedImage(HttpPostedFile postedFile, string filePath, string fileName, int maxHeight, int maxWidth, System.Drawing.Imaging.ImageFormat imgFormat, double rate)   {   try   {   //判断压缩格式   if (imgFormat == null)   {   if (postedFile.FileName.ToLower().EndsWith("jpg"))   {   imgFormat = ImageFormat.Jpeg;   }   else if (postedFile.FileName.ToLower().EndsWith("gif"))   {   imgFormat = ImageFormat.Gif;   }   else if (postedFile.FileName.ToLower().EndsWith("bmp"))   {   imgFormat = ImageFormat.Bmp;   }   else if (postedFile.FileName.ToLower().EndsWith("png"))   {   imgFormat = ImageFormat.Png;   }   else if (postedFile.FileName.ToLower().EndsWith("tiff"))   {   imgFormat = ImageFormat.Tiff;   }   else if (postedFile.FileName.ToLower().EndsWith("icon"))   {   imgFormat = ImageFormat.Icon;   }   else   {   return "";   }   }   Bitmap bmp = new Bitmap(postedFile.InputStream);   bmp = IsCMYK(bmp) ? ConvertCMYK(bmp, rate) : bmp;   if ((bmp.HorizontalResolution > 72) || (bmp.VerticalResolution > 72))   {   bmp.SetResolution(72, 72);   }   Bitmap saveBmp;   //判断大小,进行压缩   if (bmp.Height > maxHeight)   {   if ((maxHeight == 0) || (maxWidth == 0))   {   saveBmp = new Bitmap(bmp);   }   else   {   Double heightRatio = Convert.ToDouble(maxHeight) / Convert.ToDouble(bmp.Height);   Double widthRatio = Convert.ToDouble(maxWidth) / Convert.ToDouble(bmp.Width);   Double scaleRatio;   scaleRatio = heightRatio > widthRatio ? widthRatio : heightRatio;   saveBmp = new Bitmap(bmp, Convert.ToInt32(bmp.Width * scaleRatio), Convert.ToInt32(bmp.Height * scaleRatio));   while (Convert.ToInt32(saveBmp.Height * scaleRatio) > maxHeight)   {   heightRatio = Convert.ToDouble(maxHeight) / Convert.ToDouble(bmp.Height);   widthRatio = Convert.ToDouble(maxWidth) / Convert.ToDouble(bmp.Width);   scaleRatio = heightRatio > widthRatio ? widthRatio : heightRatio;   saveBmp = new Bitmap(bmp, Convert.ToInt32(bmp.Width * scaleRatio), Convert.ToInt32(bmp.Height * scaleRatio));   }   }   }   else   {   saveBmp = new Bitmap(bmp);   }   DateTime DateFile = DateTime.Now;   string NewFileName = DateFile.Year.ToString() + DateFile.Month.ToString()+DateFile.Day.ToString() + DateFile.Hour.ToString() + DateFile.Minute.ToString();   fileName = fileName.LastIndexOf('.') > 0 ? fileName.Substring(0, fileName.IndexOf('.')) : fileName.Replace(".", "");   fileName = fileName != "" ? NewFileName + fileName : NewFileName;   string path = HttpContext.Current.Server.MapPath(filePath);   //如果不存在该文件夹   if (!Directory.Exists(path))   Directory.CreateDirectory(path);   fileName += "." + imgFormat.ToString();   saveBmp.Save(path + "\" + fileName, imgFormat);   bmp.Dispose();   saveBmp.Dispose();   }   catch (System.Exception)   {   fileName = "";   }   return fileName;   }   private bool IsCMYK(System.Drawing.Image img)   {   string FlagVals = GetImageFlags(img);   if ((FlagVals.IndexOf("Ycck") > -1) || (FlagVals.IndexOf("Cmyk") > -1))   { return true; }   else   { return false; }   }   private string GetImageFlags(System.Drawing.Image img)   {   ImageFlags FlagVals = (ImageFlags)Enum.Parse(typeof(ImageFlags), img.Flags.ToString());   return FlagVals.ToString();   }   private Bitmap ConvertCMYK(Bitmap bmp, double rate)   {   rate = rate > 1.0 ? 1.0 : rate;   rate = rate RGB了   g.DrawImage(bmp, rect);   Bitmap returnBmp = new Bitmap(tmpBmp);   g.Dispose();   tmpBmp.Dispose();   bmp.Dispose();   return returnBmp;   }       搞定收工。洗澡睡觉!

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。