加载图像时内存不足异常 [英] Out of memory exception while loading images

查看:169
本文介绍了加载图像时内存不足异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码将图像作为缩略图加载到FlowLayoutPanel控件。不幸的是我得到了一个OutOfMemory异常。

I am using the following piece of code to load images as thumbnails to a FlowLayoutPanel control. Unfortunately i get an OutOfMemory exception.

正如你已经猜到的那样,内存泄漏位于行

As you already guess the memory leak is found at line

 Pedit.Image = System.Drawing.Image.FromStream(fs)

那我怎么能优化下面的代码?

So how could i optimize the following code?

 Private Sub LoadImagesCommon(ByVal FlowPanel As FlowLayoutPanel, ByVal fi As FileInfo)
        Pedit = New DevExpress.XtraEditors.PictureEdit
        Pedit.Width = txtIconsWidth.EditValue
        Pedit.Height = Pedit.Width / (4 / 3)
        Dim fs As System.IO.FileStream
        fs = New System.IO.FileStream(fi.FullName, IO.FileMode.Open, IO.FileAccess.Read)
        Pedit.Image = System.Drawing.Image.FromStream(fs)
        fs.Close()
        fs.Dispose()
        Pedit.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Zoom

        If FlowPanel Is flowR Then
            AddHandler Pedit.MouseClick, AddressOf Pedit_MouseClick
            AddHandler Pedit.MouseEnter, AddressOf Pedit_MouseEnter
            AddHandler Pedit.MouseLeave, AddressOf Pedit_MouseLeave
        End If

        FlowPanel.Controls.Add(Pedit)
    End Sub

更新:加载大量图片时出现问题(3264x2448px在300dpi - 每个图像大约是3Mb)

Update: The problem occurs while loading a number of images (3264x2448px at 300dpi - each image is about 3Mb's)

推荐答案

您可以通过几个步骤解决这个问题:

You can solve this in a few steps:


  • 为了摆脱文件依赖性,你必须复制图像。通过真正将其绘制到新的位图,您不能只是复制它。

  • 因为你想要缩略图,而你的源位图相当大,所以将它与收缩图像结合起来。

这篇关于加载图像时内存不足异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆