为什么在尝试加载图像我的窗体抛出OutOfMemory例外? [英] Why does my form throw an OutOfMemory exception while trying to load image?

查看:175
本文介绍了为什么在尝试加载图像我的窗体抛出OutOfMemory例外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个保存用户信息与图片到数据库的应用程序。管理员可以访问那些已经通过不同的形式查看保存的信息。 ,点击列表框项目将显示从数据库中检索到的图像细节。

I have an application that saves User Information with Image into a data base. Admin can access the information those are already saved through a different form view. Clicking on the List Box item will display the details with Image retrieved from the database.

UserViewDetails.cs:

private void lbEmp_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        if (lbEmp.SelectedIndex != -1)
        {
            em.Emp_ID = Convert.ToInt32(lbEmp.SelectedValue);
            em.SelectById();
            if (!em.EmptyPhoto)
                pbEmp.BackgroundImage = em.Picture;
            else
                pbEmp.BackgroundImage = null;

            txtEmpName.Text = em.Emp_Name;
            txtImagePath.Text = em.ImgPath;
            cmbEmpType.SelectedText = em.EmployeeType;
            cmbCountry.SelectedValue = em.CountryID;
            cmbCity.SelectedValue = em.CityID;
        }
    }
    catch (Exception) { }
}

这种形式从父窗体 Form1中名为:

This form is called from the parent form Form1:

Form1.cs中:

try
{
    var vi = new Admin.frmViewEmployeeInfo();
    vi.ShowDialog();
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}



在这里,一个内存不足异常被捕获。发生什么事?相同的代码不扔在我的另一个应用程序的任何异常。

Here, an "out of memory" exception is caught. What is happening? The same code doesn't throw any exception in another application of mine.

推荐答案

一个OutOfMemoryException是很常见的,当您使用位图类。位图可能需要大量的内存。惹上麻烦的一个标准方法是懒散有关调用其Dispose()方法。在代码中没有使用的Dispose()的东西,你会得到远很容易地在.NET中,终结将在后面进行清理。但是,往往不是因为他们采取的大量使用位图很好地工作的非托管内存来存储像素数据,但是很少管理内存。

An OutOfMemoryException is pretty common when you use the Bitmap class. Bitmaps can require a lot of memory. One standard way to get in trouble is being sloppy about calling its Dispose() method. Not using Dispose() in your code is something you'll get away easily in .NET, finalizers will clean up after you. But that tends to not work well with bitmaps because they take a lot of unmanaged memory to store the pixel data but very little managed memory.

有至少一个的Dispose()调用在你的代码丢失,你不处理旧的背景图像。修复:

There is at least one Dispose() call missing in your code, you are not disposing the old background image. Fix:

em.SelectById();
if (pbEmp.BackgroundImage != null) pbEmp.BackgroundImage.Dispose();    // <== here
if (!em.EmptyPhoto)
    pbEmp.BackgroundImage = em.Picture;
else
    pbEmp.BackgroundImage = null;

和可能在其他地方,我们无法看到em.Picture是如何管理的。

And possibly in other places, we can't see how em.Picture is managed.

另外,和更难诊断,是GDI +是提高精确异常相当差。您也可以从坏图像数据的文件得到OOM。你会发现在是令人遗憾的行为,这个答案的一个原因。

Also, and much harder to diagnose, is that GDI+ is pretty poor at raising accurate exceptions. You can also get OOM from a file with bad image data. You'll find a reason for that regrettable behavior in this answer.

这篇关于为什么在尝试加载图像我的窗体抛出OutOfMemory例外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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