上传缩略图,等比例压缩
2010-08-16 15:08
113 查看
前些日子一位同学问我怎么上传缩略图,并可以指定图片的尺度而且进行等比压缩。晚上,自己没事在网上看到一些网友发的帖子,都是挺好,最后我也写了一个上传的方法。供大家参考:
代码
上述为上传缩略图的方法,涉及到读取xml文件和存储地址,不是很完整,掉了个方法,补上:
代码
public byte[] UploadFile(FileUpload f_Ifile, int ThumbnailWidth, int ThumbnailHeight, string resPathId,string otherPath) { try { //获取客户端指定上传文件的访问 HttpPostedFile upfile = f_Ifile.PostedFile; //得到上传文件的长度 int UpfileLength = upfile.ContentLength; //没有选择上传的文件 if (UpfileLength == 0) { return null; } //得到上传文件的客户端MIME类型 string contentType = upfile.ContentType; byte[] fileArray = new Byte[UpfileLength]; Stream fileStream = upfile.InputStream; fileStream.Read(fileArray, 0, UpfileLength); string filename = Path.GetFileName(upfile.FileName); string sExt = upfile.FileName.Substring(upfile.FileName.LastIndexOf(".")).ToLower(); //读取xml判断上传的文件格式及大小 string xmlPath = System.Web.HttpContext.Current.Server.MapPath("~/File.xml"); bool postfix = Tools.UpDownFile.VerdictPostfixByFile(filename, xmlPath, "FileManager", "id", "UpImgFile", "format"); bool size = Tools.UpDownFile.VerdictSizeByFile(f_Ifile, xmlPath, "FileManager", "id", "ImageSize", "size"); System.Drawing.Image xImage; //缩略图的高,宽 int PhotoHeight, PhotoWidth; //创建新的图形 Rectangle NewPhoto; //位图图像 System.Drawing.Bitmap xBitmap; //路径 //string filePath = ""; if (postfix == true && size == true) { xImage = System.Drawing.Bitmap.FromStream(upfile.InputStream); //赋值,高和宽 //this.ThumbnailHeight = xImage.Height.ToString(); //this.ThumbnailWidth = xImage.Width.ToString(); if (this.txtwidth.Text == "") { ThumbnailWidth = Convert.ToInt32(xImage.Width.ToString()); } else { ThumbnailWidth = Convert.ToInt32(this.txtwidth.Text); } if (this.txtheight.Text == "") { ThumbnailHeight = Convert.ToInt32(xImage.Height.ToString()); } else { ThumbnailHeight = Convert.ToInt32(this.txtheight.Text); } //上传资源模块的缩略图 if (resPathId != "" && otherPath=="") { ResPath rp = BLL.ResPathManager.GetResPathByResModelId(Convert.ToInt32(resPathId)); if (rp.ResPaths != "") { filepath = rp.ResPaths + @"\Pic"; } else { return null; } } //上传的不是资源文件的缩略图 else if (resPathId == "" && otherPath != "") { filepath = otherPath; if (!Tools.IOHelp.CreateDirectory(filepath)) { return null; } } else { return null; } string ranNum= Tools.FormatStr.GetUpFileName() + sExt; filepath = filepath + @"\" + ranNum; xBitmap = new System.Drawing.Bitmap(fileStream); PhotoHeight = xBitmap.Height; PhotoWidth = xBitmap.Width; if (Convert.ToDecimal(PhotoHeight) / Convert.ToDecimal(PhotoWidth) > Convert.ToDecimal(PhotoHeight) / Convert.ToDecimal(PhotoWidth)) { PhotoHeight = Convert.ToInt16((Convert.ToDecimal(ThumbnailHeight) / Convert.ToDecimal(ThumbnailWidth)) * Convert.ToDecimal(PhotoWidth)); NewPhoto = new Rectangle(Convert.ToInt16((Convert.ToDecimal(xBitmap.Width) - Convert.ToDecimal(PhotoWidth)) / 2), 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 = new Rectangle(0, 0, PhotoWidth, PhotoHeight); } 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 = xImage.GetThumbnailImage(ThumbnailWidth, ThumbnailHeight, myCallback, IntPtr.Zero); myThumbnail.Save(filepath); this.Label1.Text = ranNum; myThumbnail.Dispose(); myBitmap.Dispose(); xBitmap.Dispose(); return fileArray; } } catch (Exception ex) { return null; } return null; }
上述为上传缩略图的方法,涉及到读取xml文件和存储地址,不是很完整,掉了个方法,补上:
private static bool ThumbnailCallback() { return false; }
相关文章推荐
- 上传缩略图,等比例压缩
- 黄聪:php实现上传图片和等比例缩略图压缩图片
- php实现上传图片和等比例缩略图压缩图片
- 在ASP.NET中实现上传图片,并按比例生成缩略图
- 上传等比例压缩图片
- C#上传图片并按比例生成缩略图
- Java实现的上传并压缩图片功能【可等比例压缩或原尺寸压缩】
- php gd2 上传图片/文字水印/图片水印/等比例缩略图/实现代码
- 图片上传判断width 等比例压缩
- 安卓中关于图片从网络获取,压缩,上传,下载,缩略图,缓存的一些处理总结(一)
- ASP.NET(C#)图片上传压缩成缩略图的代码
- 安卓中关于图片从网络获取,压缩,上传,下载,缩略图,缓存的一些处理总结(四)
- ThinkPHP上传图片并压缩为缩略图
- 制作图片缩略图 等比例压缩
- 图片上传判断width 等比例压缩
- 在ASP.NET中实现上传图片,并按比例生成缩略图
- php将图片压缩成缩略图(大,中,小)上传到数据库中
- EasySwift/YXJImageCompressor 图片压缩,拍照上传压缩图片,比例压缩,data压缩,方便好用
- 在ASP.NET中实现上传图片,并按比例生成缩略图
- 安卓中关于图片从网络获取,压缩,上传,下载,缩略图,缓存的一些处理总结(二)