6 Replies - 7654 Views - Last Post: 15 October 2010 - 11:52 AM Rate Topic: -----

#1 Sparukus  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 190
  • Joined: 05-April 10

Out of memory error c#

Posted 15 October 2010 - 09:01 AM

Hi I keep getting an out of memory problem when I select and empty bitmap image.

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (pictureBox1.Image != null)
            //   try
            //   {
                    pictureBox1.Image = System.Drawing.Image.FromFile("C:/Users/Garrith-Super-PC/Desktop/New folder/" + listBox1.SelectedItem.ToString());
                    
            //}
            
           //catch (NullReferenceException)
           //{

           //    MessageBox.Show("The image box is empty!");
           //}

        }


I tryed to handle it myself as you can see but that didnt work. Any help would be great thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Out of memory error c#

#2 Sparukus  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 190
  • Joined: 05-April 10

Re: Out of memory error c#

Posted 15 October 2010 - 09:27 AM

No one able to help out? :(
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,663
  • Joined: 23-August 08

Re: Out of memory error c#

Posted 15 October 2010 - 10:02 AM

You posted in C++/CLI, not C#. I'll move it.
Was This Post Helpful? 1
  • +
  • -

#4 Sparukus  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 190
  • Joined: 05-April 10

Re: Out of memory error c#

Posted 15 October 2010 - 10:05 AM

ahh thanks man, didnt realise it was not set on c# hmm.

This post has been edited by Sparukus: 15 October 2010 - 10:05 AM

Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Out of memory error c#

Posted 15 October 2010 - 10:27 AM

Just how big is this image?

Also, I see you used a try/catch, but catching a NullReferenceException isn't the same as catching a OutOfMemoryException.
Was This Post Helpful? 1
  • +
  • -

#6 Sparukus  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 190
  • Joined: 05-April 10

Re: Out of memory error c#

Posted 15 October 2010 - 10:34 AM

View PostinsertAlias, on 15 October 2010 - 09:27 AM, said:

Just how big is this image?

Also, I see you used a try/catch, but catching a NullReferenceException isn't the same as catching a OutOfMemoryException.


Hey its an empty image. so not big lol I have it set so my listbox shows all the files in a specific folder when I click these images it puts them in my picture box but... when ever I hit an empty bitmap image i get the out of memory error. I shall try the outofmem exception in my try catch.

Thanks.

woop got it thanks alot buddy!

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

                try
                {
                    pictureBox1.Image = System.Drawing.Image.FromFile("C:/Users/Garrith-Super-PC/Desktop/New folder/" + listBox1.SelectedItem.ToString());

                }

                catch (OutOfMemoryException)
                {

                    MessageBox.Show("The image box is empty!");
                }

        }


works well now! :)
Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Out of memory error c#

Posted 15 October 2010 - 11:52 AM

Looking up the Image.FromFile method, you would see that the OutOfMemoryException is thrown when:

The file does not have a valid image format.
-or-
GDI+ does not support the pixel format of the file.

I would say that when nothing is selected in the listbox, your path is "C:/Users/Garrith-Super-PC/Desktop/New folder/". As you can see, this isn't a valid image.

And truthfully, you shouldn't use a try/catch for this particular instance. You can leave the try/catch for the times when the exception is actually thrown for a reason. You should do this:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
        if (listBox1.SelectedIndex == -1)
        {
             MessageBox.Show("Please select an image");
             return;
        }

        try
        {
            pictureBox1.Image = System.Drawing.Image.FromFile("C:/Users/Garrith-Super-PC/Desktop/New folder/" + listBox1.SelectedItem.ToString());

        }

        catch (OutOfMemoryException)
        {

            MessageBox.Show("The image box is empty!");
        }

}



Try/catch's are expensive to processing. If you can do a simple IF statement to handle when an exception will occur, that is normally a much better option.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1