如何解决C#中的内存不足异常

时间:2014-07-10 08:41:21

标签: c# .net winforms

我正在开发Winforms应用程序。此代码包含它以将图像保存到SQL Server表。但是,当我从表中返回二进制数据以显示图像时,有时会发生Out of memory异常。

这是我保存前转换图片的代码。

openImage.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";

if (openImage.ShowDialog() == DialogResult.OK)
{
    pictureBox1.Image = new Bitmap(openImage.FileName);         
    pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;      
    lblImgInfo.Text = openImage.FileName;
    FRM_ImageViewer imgver = new FRM_ImageViewer(openImage.FileName);
    imgver.Show();

    string strFn = openImage.FileName;
    FileInfo fiImage = new FileInfo(strFn);

    long  m_lImageFileLength =fiImage.Length;
    byte[] m_barrImg = new byte[Convert.ToInt32(m_lImageFileLength)];

    FileStream fs = new FileStream(strFn, FileMode.Open, FileAccess.Read, FileShare.Read);
    int iBytesRead = fs.Read(m_barrImg, 0,Convert.ToInt32(m_lImageFileLength));
    fs.Close();
}

这是我从数据库中检索图像的代码

// DTSelectedJobs is a DataTable  in csharp.
byte[] barrImg = (byte[])DTSelectedJobs.Rows[0].ItemArray[11];
string strfn = Convert.ToString(DateTime.Now.ToFileTime());
FileStream fs2 = new FileStream(strfn, FileMode.CreateNew, FileAccess.Write);
fs2.Write(barrImg, 0, barrImg.Length);
fs2.Flush();
fs2.Close();
FRM_ImageViewer imgvwr = new FRM_ImageViewer(strfn);
imgvwr.Show();

有人可以给我一个如何找到错误的建议吗?

3 个答案:

答案 0 :(得分:2)

Bitmap继承自Image即Disposable,您需要处理您的位图。

using(var bmp = new Bitmap(openImage.FileName))
{
    pictureBox1.Image = bmp;
}

正如DGibbs所说,你需要对FileStream

做同样的事情
using(var fs = new FileStream(strFn, FileMode.Open, FileAccess.Read,
                              FileShare.Read))
    int iBytesRead = fs.Read(m_barrImg, 0,Convert.ToInt32(m_lImageFileLength));

答案 1 :(得分:0)

我并不熟悉winforms,所以请耐心等待,但这看起来不合适。

您创建了strfn

string strfn = Convert.ToString(DateTime.Now.ToFileTime());

然后使用...日期/时间字符串初始化图像查看器?

FRM_ImageViewer imgvwr = new FRM_ImageViewer(strfn);

似乎barrImg这里应该是什么?

此外,FileStream实现了IDisposable接口,因此理想情况下应该包含在使用块中:

using(FileStream fs2 = new FileStream(strfn, FileMode.CreateNew, FileAccess.Write))
{
    fs2.Write(barrImg, 0, barrImg.Length);
    FRM_ImageViewer imgvwr = new FRM_ImageViewer(strfn);
    imgvwr.Show();
}

答案 2 :(得分:0)

It's almost an year since the question was asked but let me share my thoughts since there is not an answer yet

Out of memory exception was thrown because there is not enough memory available for your application or your application is using too much memory than available.

I would check the following to find why the application is using too much memory

1) Make sure table returns only required data. Because size of an image is large. If your database returns a large result then, the result set may use several MB's of memory.

Assuming you are trying to view an image saved in data base, if you are displaying n images then query for the specific records.

2) Make sure you are disposing all the disposable objects like Image, Bitmap,Stream, FileStream, Pen, etc., because it may cause memory leak in your application

3) Use a memory profiler to check for memory leaks in the application.