question

BrandonBoone-0673 avatar image
0 Votes"
BrandonBoone-0673 asked 10165396 published

C# "Exception of type 'System.OutOfMemoryException' was thrown."

So I am getting this Exception: "Exception of type 'System.OutOfMemoryException' was thrown." but only after I open like 3 different pictures are the same time. This seem weird to me. I have opened way more than that with Adobe Illustrator. anyhow, I have a try catch and after It fails, I try to openned a new picture and it will work well for another 3 or 4 pictures. I have not idea why.

Here is my Code:

  static private  double[,] ImageToByteArray(Bitmap _image, int height, int width)
     {
         using (Bitmap b = new Bitmap(_image))
         { 
             BitmapData bData = b.LockBits(new Rectangle(0, 0, _image.Width, _image.Height), ImageLockMode.ReadWrite, b.PixelFormat);
            
             /* GetBitsPerPixel just does a switch on the PixelFormat and returns the number */
             byte BitsPerPixel = GetBitsPerPixel(bData.PixelFormat);

             /*the size of the image in bytes */
             int size = bData.Stride * bData.Height;

             int Stride = bData.Stride;
             /*Allocate buffer for image*/
             byte[] data = new byte[size];
             double[,] returndata = new double[height, width];
             /*This overload copies data of /size/ into /data/ from location specified (/Scan0/)*/
             Marshal.Copy(bData.Scan0, data, 0, size);
             int i = 0;

             for (int y = 0; y < height; y++)
             {
                 for (int x = 0; x < width; x++)
                 {

                   
                     double magnitude = ((0.0722D * data[i]) + (0.7152D * data[i + 1]) + (0.2126D * data[i + 2]));
                     returndata[y, x] = magnitude;
                     i += BitsPerPixel / 8;

                 }
             }

             _image.Dispose();
             data = null;

             return returndata;
         }
     }

it fails at double[,] returndata = new double[height, width]; and I do not know why

I checked Task Manager and the program got up to 2,651.8.

I should be about to open multiple pictures at a time correct?

I can with Photos.


dotnet-csharpwindows-wpf
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Did you check if size, height and width are not too large?



0 Votes 0 ·

Viorel - 1

how can it be too big, I am creating the array during run time?

0 Votes 0 ·
MuhammadAhmod avatar image
0 Votes"
MuhammadAhmod answered

@Brandonboone-0673 you lockbits but i dont see you unlock bits?

maybe clean up, in a finally statement?

finally
{
b.UnlockBits(bData);
}

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

BrandonBoone-0673 avatar image
0 Votes"
BrandonBoone-0673 answered

All,

I believe I fixed it by changing the program to a x64

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

10165396 avatar image
0 Votes"
10165396 answered 10165396 published

Mabye one pixel four bytes?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.