I\'m creating an application (Windows Form) that allows the user to take a screenshot based on the locations they choose (drag to select area). I wanted to add a little \"pr
If you are creating new bitmaps over and over, you might need to call GC.Collect();
which will force C# to garbage collect
you should check if curLocation.X is larger than 50, otherwise your rectangle will start in the negative area (and of course curLocation.Y)
Out of memory in C# imaging, is usually sign of wrong rect or point - a bit of red herring. I bet start
has negative X or Y when error happens or the Size.Hight + Y or Size.Width + X is bigger than Hight or width of the image.
Use the Bitmap object like this:
using (Bitmap bmpImage = new Bitmap(img))
{
// Do something with the Bitmap object
}
If the zoom box goes off the edge of the desktop area, then when you try to crop, you are asking the system to make a new image that includes pixels outside of the video memory area. Make sure to limit your zoom box so that none of its extents is less than 0 or greater than the screen edges.
MSDN explains that an OutOfMemoryException
means
rect
is outside of the source bitmap bounds
where rect
is the first parameter to the Bitmap.Clone
method.
So check that the cropArea
parameter is not larger than your image.
In GDI+ an OutOfMemoryException
does not really mean "out of memory"; the GDI+ error code OufOfMemory
has been overloaded to mean different things. The reasons for this are historic and a well described by Hans Passant in another answer.