前段时间,做了一个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);
}
}