This project has moved. For the latest updates, please go here.

OutOfMemoryException

Oct 15, 2013 at 11:32 AM
Hey,
i have a Question about the library.
(Magick.NET-6.8.7.001-Q16-x86-net40-client: I copied the two files to my release folder and add a reference to my project)

I use the following code to display a image in a PictureBox.
pictureBox1.Image = new MagickImage(memoryStream).ToBitmap();
If i try to flip this image with the following code, i get a "OutOfMemoryException".
private Image flipImage(Image image, Boolean yAxis)
 {
        Image flippedImage = new Bitmap(image.Width, image.Height);

        using (Graphics g = Graphics.FromImage(flippedImage))
        {
                Matrix mat = null;

                if (yAxis)
                {
                    mat = new Matrix(1, 0, 0, -1, 0, 0);
                    mat.Translate(0, flippedImage.Height, MatrixOrder.Append);
                }
                else
                {
                    mat = new Matrix(-1, 0, 0, 1, 0, 0);
                    mat.Translate(flippedImage.Width, 0, MatrixOrder.Append);
                }

                //Draw
                g.Transform = mat;
                g.DrawImage(image, 0, 0);      <-------- Exception

                //clean up
                mat.Dispose();
            }
            return flippedImage;
        }
If i used this code with the same picture,and flip the image, i get no Exception.
pictureBox1.Image = Bitmap.FromStream(memoryStream);
Any advices?
Coordinator
Oct 15, 2013 at 12:49 PM
Can you post an example image that works with Bitmap.FromStream(memoryStream); but crashes with new MagickImage(memoryStream).ToBitmap()?
Oct 15, 2013 at 1:15 PM
Edited Oct 15, 2013 at 1:15 PM
Sure, i have included a png and a dds file.

I get the same error, when i load one of these files from a path and flip the image.
pictureBox1.Image = new MagickImage(open_Dlg.FileName).ToBitmap();  <----- Exception
pictureBox1.Image = Bitmap.FromFile(open_Dlg.FileName);                      <----- works
Link images
Coordinator
Oct 15, 2013 at 2:31 PM
Can you create an example that does not use the picture box? Your example does not explain how the flipImage method is called. Maybe you are forgetting to Dispose the image you use when you call the flipImage method?
Oct 15, 2013 at 2:45 PM
I hope i understand what you want.

With this code i get the exception.
Image originalImage = new MagickImage(openFile_Dlg.FileName).ToBitmap();
Image img = flipImage(originalImage, true);
This code works:
Image originalImage1 = Bitmap.FromFile(openFile_Dlg.FileName);
Image img1 = flipImage(originalImage1, true);
Coordinator
Oct 15, 2013 at 9:33 PM
Edited Oct 15, 2013 at 9:34 PM
I can reproduce the problem. I am loading the Bitmap from a MemoryStream. After creating the Bitmap I now Dispose the MemoryStream. But according to the documentation (http://msdn.microsoft.com/en-us/library/93z9ee4x.aspx) I should not do this. I will create a work item to fix this and also use the constructor of Bitmap that accepts a MemoryStream instead of using the Image.FromStream method.
Coordinator
Oct 15, 2013 at 9:36 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.