创建缩略图
来源:互联网 发布:cloudzoom.js 编辑:程序博客网 时间:2024/06/05 20:29
前段时间,做了一个WPF项目,其中需要做到了图像凭证保存及预览的功能,所以从网上找了些制作缩略图的方法,但是使用时发现会出现严重的内存泄露,往往查看几张大图后就内存不足,特地在已有方法上做了一定修改,解决了内存泄露的问题。
private void CreateThumbnail1(string[] files, double width, double height) { try { imageList = null; imageList = new ObservableCollection<ImageList>(); byte[] bytes; BitmapImage source = null; BitmapImage thumbnailSource = null; System.Drawing.Bitmap sourceBitmap = null; System.Drawing.Bitmap thumbnailBitmap = null; MemoryStream ms = null; for (int i = 0; i < files.Length; i++) { if (!string.IsNullOrEmpty(files[i])) { string fileName = files[i].Substring(files[i].LastIndexOf("\\") + 1, (files[i].LastIndexOf(".") - files[i].LastIndexOf("\\") - 1)); string fileType = files[i].Substring(files[i].LastIndexOf(".") + 1, (files[i].Length - files[i].LastIndexOf(".") - 1)); sourceBitmap = new System.Drawing.Bitmap(files[i]); double rw = width / sourceBitmap.Width; double rh = height / sourceBitmap.Height; double aspect = (double)Math.Min(rw, rh); int w = sourceBitmap.Width; int h = sourceBitmap.Height; if (aspect < 1) { w = (int)Math.Round(sourceBitmap.Width * aspect); h = (int)Math.Round(sourceBitmap.Height * aspect); } ms = new MemoryStream(); //原始图 sourceBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); bytes = ms.GetBuffer(); source = new BitmapImage(); source.BeginInit(); source.StreamSource = new MemoryStream(bytes); source.CacheOption = BitmapCacheOption.OnLoad; source.EndInit(); //缩略图 thumbnailBitmap = new System.Drawing.Bitmap(sourceBitmap, w, h); thumbnailBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); bytes = ms.GetBuffer(); thumbnailSource = new BitmapImage(); thumbnailSource.BeginInit(); thumbnailSource.StreamSource = new MemoryStream(bytes); thumbnailSource.CacheOption = BitmapCacheOption.OnLoad; thumbnailSource.EndInit(); source.Freeze(); thumbnailSource.Freeze(); imageList.Add(new ImageList() { Path = files[i], Index = i + 1, SourceImage = source, ThumbnailImage = thumbnailSource, FileName = fileName, FileType = fileType }); ms.Dispose(); bytes = null; sourceBitmap.Dispose(); thumbnailBitmap.Dispose(); source = null; thumbnailSource = null; sourceBitmap = null; thumbnailBitmap = null; GC.Collect(); } } } catch (Exception ex) { MessageBox.Show("缩略图转换出错:" + ex.Message); } } private void CreateThumbnail(List<object[]> list) { try { imageList = null; imageList = new ObservableCollection<ImageList>(); BitmapImage bi = null; byte[] data; for (int i = 0; i < list.Count; i++) { object[] objs = list[i]; if (objs.Length == 2) { data = objs[0] as byte[]; string fullName = objs[1].ToString(); if (!string.IsNullOrEmpty(fullName)) { string fileName = System.IO.Path.GetFileName(fullName).Split('.')[0]; string fileType = System.IO.Path.GetFileName(fullName).Split('.')[1]; bi = new BitmapImage(); bi.BeginInit(); bi.StreamSource = new MemoryStream(data); bi.CacheOption = BitmapCacheOption.OnLoad; bi.EndInit(); imageList.Add(new ImageList() { Path = fullName, Index = i + 1, ThumbnailImage = bi, FileName = fileName, FileType = fileType }); bi = null; } } } } catch (Exception ex) { MessageBox.Show("缩略图转换出错:" + ex.Message); } }
阅读全文
1 0
- 创建缩略图
- 转贴:创建缩略图
- ASP.NET创建缩略图
- GDI+ 创建缩略图
- ios 创建图像缩略图
- 创建图片的缩略图
- php 创建图片缩略图
- ios 创建图像缩略图
- C# 创建缩略图
- php 创建缩略图函数
- php创建缩略图
- PHP创建缩略图
- 为图片创建缩略图
- PHP创建缩略图
- java 后台创建缩略图
- 创建图片的缩略图
- PHP创建图片缩略图函数
- Drawble高效创建缩略图方法
- Hadoop相关知识整理
- java-数组和arrays类的常用方法
- HDU 5631 Rikka with Graph
- EditText-RecyclerView
- PaddlePaddle学习笔记:识别数字
- 创建缩略图
- 解决Web项目因访问URL与Tomcat服务器文件夹相同导致404页面的问题
- 类和对象
- hdu-2056-Rectangles
- Dubbox 构建Restful服务
- python3常用快捷键——将在学习中不断更新
- jdk 源码分析(6)java BitSet结构
- HDU4648 Magic Pen 6 签到
- [转载自知乎——有哪些令人拍案叫绝的算法?] [侵必付] 关于位运算