0

I change the Automatic Image Stitching with Accord.NET application to stitch multiple files.
I put a for loop at the btnDoItAll code, the loop will depend on how many images was to stitch.
I have joined a cropping code at the end of the loop. you can see the here.
The an error showed up which said OutOfMemoryException was unhandled - Out of memory in this part.

croppedBitmap = croppedBitmap.Clone(new Rectangle(MinWidth, 0, (int)croppedBitmap.Width - MinWidth - MaxWidth, 1323), System.Drawing.Imaging.PixelFormat.DontCare);

hope you can help me again.

The output from debug was these:  
Bitmap-width: 877
Bitmap-height: 1325
Width: -1
MinWidth: 877
A first chance exception of type 'System.OutOfMemoryException' occurred in System.Drawing.dll  
Community
  • 1
  • 1
Ozarraga_AB
  • 939
  • 5
  • 15
  • 24
  • possible duplicate of [Is there a reason Image.FromFile throws an OutOfMemoryException for an invalid image format?](http://stackoverflow.com/questions/2610416/is-there-a-reason-image-fromfile-throws-an-outofmemoryexception-for-an-invalid-i) – BrokenGlass Feb 12 '12 at 18:41

1 Answers1

1

I just had a similar problem a few hours ago check your boundaries for Clone. If you move outside of your bitmap it will throw this exception.

So be careful that this is the case.

1: MinWidth + (int)croppedBitmap.Width - MinWidth - MaxWidth <= croppedBitmap.Width
2: 0 + 1323 <= croppedBitmap.Height

if my guess is correct 1 & 2 are not fulfilled

EDIT: Add this in front of Clone and post the result in your post

Debug.WriteLine("Bitmap-width: "+croppedBitmap.Width);
Debug.WriteLine("Bitmap-height: "+croppedBitmap.Height);
Debug.WriteLine("Width: "+(croppedBitmap.Width - MinWidth - MaxWidth));
Debug.WriteLine("MinWidth: "+MinWidth);

EDIT2: 1. Your width is < 0 (in this case -1) which shouldn't be the case 2. Even if it would be > 0 it would result in an error since 877 + x is > croppedBitmap.Width which is not allowed

So what I was saying from the very beginning is that you have to make sure that your width and height have to be larger 0 AND that the sum of width + MinWidth and height + 0 from your rectangle mustn't exceed the boundaries of your image.

Right now your rectangle looks like this:

new Rectangle(877, 0, -1, 1323) // Rectangle(posx, posy, width, height)

So as you can see the width is negative which is not what you want it has to be larger 0. So if you would do this now:

new Rectangle(877, 0, 1, 1323)

It would still be wrong since your rectangle would be from from 877 to 878 (x coordinate) which can't be since your image is only 877 pixels wide. This means MinWidth and (int)croppedBitmap.Width - MinWidth - MaxWidth are wrong. You have to make sure that your values don't cause these kind of problems.

It's not a problem with the Copy method it's more a problem of the passed parameters. You have to check them before passing them!

  • Sorry, i am new to this stuff. I didn't quite get it, what should i suppose to do? – Ozarraga_AB Feb 12 '12 at 18:48
  • I've add it in front of `croppedBitmap = croppedBitmap.Clone(`, there was an error on debbuging which saids `The name 'Debug' does not exist in the current context` – Ozarraga_AB Feb 12 '12 at 19:14
  • Because you need using System.Diagnostics; –  Feb 12 '12 at 19:15
  • Wrong parameters often cause OOM exceptions with `System.Drawing`. So ensure the chosen rectangle is valid. – CodesInChaos Feb 12 '12 at 19:40
  • @Layne the cropping application alone does run successfully. but when combining it to the other application results into this. what do you think the problem is? – Ozarraga_AB Feb 12 '12 at 19:51