0

I'm developing a Winforms application. This code contains it to save an image to a SQL Server table. But sometimes a Out of memory exception happens when I'm returning the binary data from the table to display the image.

This is my code to convert image before save.

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();
}

and this is my code to retrieve the image from the database

// 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();

Can someone please give me an advice how to find the error?

9
  • Bitmap inherits from Image which is Disposable, you need to dispose of your bitmap. Commented Jul 10, 2014 at 8:44
  • @Sayse and the FileStream Commented Jul 10, 2014 at 8:46
  • this code contains it to save an image to sql server table. WHOA, STOP RIGHT THERE. Never save images to a database!! Commented Jul 10, 2014 at 8:46
  • Where (which line) exactly the exception happens? Commented Jul 10, 2014 at 8:46
  • Are you doing anything with m_barrImg? Also assuming FRM_ImageViewer opens the file, combined with the Bitmap created for pictureBox1, you'll have three copies of the same image in memory. What exactly are you converting? Commented Jul 10, 2014 at 9:00

3 Answers 3

2

Bitmap inherits from Image which is Disposable, you need to dispose of your bitmap.

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

As DGibbs notes, you need to do the same for FileStream

using(var fs = new FileStream(strFn, FileMode.Open, FileAccess.Read,
                              FileShare.Read))
    int iBytesRead = fs.Read(m_barrImg, 0,Convert.ToInt32(m_lImageFileLength));
Sign up to request clarification or add additional context in comments.

3 Comments

While I agree, does this answer the question? OP says he gets the error when reading the image form the database.
@DavidG - Anything that is a memory leak will be contributing, again there is a file stream in the second snippet which should receive the same treatment
I wouldn't say it's a memory leak (I would assume the GC would tidy that stuff up eventually) though it is memory inefficient. My concern is what is happening inside the FRM_ImageViewer constructor.
0

I'm not all that familiar with winforms so bear with me but this looks incorrect to me.

You create a strfn:

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

And then initialize an image viewer with... a date/time string?

FRM_ImageViewer imgvwr = new FRM_ImageViewer(strfn);

Seems like barrImg is what should be in the ctor here?

Also, FileStream implements the IDisposable interface so should be ideally wrapped in a using block:

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();
}

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.