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

Brandon Boone 31 Reputation points
2022-05-25T05:08:42.043+00:00

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.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,670 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Muhammad Ahmod 1 Reputation point
    2022-05-25T06:23:43.877+00:00

    @Brandon Boone you lockbits but i dont see you unlock bits?

    maybe clean up, in a finally statement?

    finally
    {
    b.UnlockBits(bData);
    }

    0 comments No comments

  2. Brandon Boone 31 Reputation points
    2022-05-25T15:35:12.797+00:00

    All,

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

    0 comments No comments

  3. 腾飞 刘 1 Reputation point
    2022-05-26T03:51:14.633+00:00

    Mabye one pixel four bytes?

    0 comments No comments