Re: 'Out of Memory' exception when trying to Clone a Bitmap

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Michael,

This sounds like an interesting alternative. Do you know hoe I would go
about this in .Net 1.1
I'm assuming the DrawImage method you are talking about is the GDI+ method
and not the .Net method.
Is there anything in the .Net framework, maybe in the Bitmap class, that
will do similar?

Sorry for my ignorance but I know little about ther GDI world. All I'm
looking to do is simply copy and crop a bitmap image. I was hoping the .Net
Bitmap.Clone method would perform this for me.

Thanks
--
Richard


"Michael Phillips, Jr." wrote:

There appears to be a memory allocation problem.

Gdiplus will only allocate memory when the image is actually accessed.

Since you are using a third party sdk, it seems that when gdiplus attempts
to access the image to intialize and allocate internal memory, there is a
failure.

No problem! You can safely create a new image with the bounds that you need
and use DrawImage to copy that portion of the original image to your new
image.

That is what I do in my own code to crop an image.

You must not use an indexed pixel format for the source and destination
image, as gdiplus cannot create a Graphics object from indexed images.

For indexed pixel formatted images, you must roll your own cropping by using
LockBits to access the internal bits of the image.


"RichardB" <RichardB@xxxxxxxxxxxxxxxxx> wrote in message
news:A0E2BF1C-FEE5-4D04-9216-EE805F136865@xxxxxxxxxxxxxxxx
Michael,

Thank you for the reply.
Firstly I have tried using regular and floating point bounds, both give
the
same problem.
Secondly the bounds are within the bounds of the first bitmap.
If I clone using the EXACT bounds of the original bitmap it works fine.
If I clone using anything smaller then it fails. I have tried using the
original bounds and just subtracting 1 from the height or width. It fails.

Could it be to do with something about the bitmap in question?
If I use my own bitmap, for example....
Bitmap MyBitmap = new Bitmap("filename.jpg");
.. then it works fine using smaller bounds for the clone.

If I use the one passed to me from the SDK then I can only copy it using
the
exact bounds. I need to copy and select a small part of the original.

Any ideas?

Thanks
--
Richard


"Michael Phillips, Jr." wrote:

Per the documentation, you only get that error when the bounding
rectangle
is outside of the source bitmap's bounds!

Did you check the bounds of the bitmap first. Additionally, you are
using a
bounding rectangle described with floating point bounds. Try a regular
Rectangle.

"RichardB" <RichardB@xxxxxxxxxxxxxxxxx> wrote in message
news:FC603D6E-0E98-437E-AF15-432BD96F1ABA@xxxxxxxxxxxxxxxx
I'm getting an 'Out of Memory' exception when I try to clone a Bitmap
object
in .Net 1.1

If I load a new bitmap (from a file for example) and clone it, it works
fine.

However I am getting a bitmap passed to me from an external 3rd party
SDK.
It's a C++ SDK with a .Net interop dll, so it should work fine.
If I clone the image as is (a straight full clone) then it's fine.
If however I clone the image and specify a reduced area to clone (as
per
the
.net Bitmap.Clone method) then it fails with 'Out of Memory error.

For example:
RectangleF NewRect = new RectangleF(0,0,10,10);
Bitmap Bitmap2 = Bitmap1.Clone(NewRect, Bitmap1.PixelFormat);

This fails on the second line.

Does anyone have any ideas?

Thank you
--
Richard






.



Relevant Pages

  • Re: Out of Memory exception when trying to Clone a Bitmap
    ... looking to do is simply copy and crop a bitmap image. ... If I clone using the EXACT bounds of the original bitmap it works fine. ... If I clone using anything smaller then it fails. ... bounding rectangle described with floating point bounds. ...
    (microsoft.public.win32.programmer.gdi)
  • Re: Out of Memory exception when trying to Clone a Bitmap
    ... Gdiplus will only allocate memory when the image is actually accessed. ... Since you are using a third party sdk, it seems that when gdiplus attempts ... If I clone using the EXACT bounds of the original bitmap it works fine. ...
    (microsoft.public.win32.programmer.gdi)
  • Re: Out of Memory exception when trying to Clone a Bitmap
    ... Secondly the bounds are within the bounds of the first bitmap. ... If I clone using the EXACT bounds of the original bitmap it works fine. ... If I clone using anything smaller then it fails. ...
    (microsoft.public.win32.programmer.gdi)
  • Re: Gdi+ Bitmap/Image Clone() function
    ... So one can use a technique were you wrap a memory buffer containg a bitmap into an IStream, create the bitmap, clone it and then dispose the IStream and the memory used. ...
    (microsoft.public.win32.programmer.gdi)
  • Re: image Clone problem when using LockBits
    ... Clone of an inmage is not a "deep copy". ... Read my Blog at http://bobpowelldotnet.blogspot.com "Norvin Laudon" wrote in message ... > I am using LockBits to perform some pixel processing on some Bitmap objects. ... > A Bitmap arrives as an argument to an event, ...
    (microsoft.public.dotnet.framework.drawing)