创建缩略图

       前段时间,做了一个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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值