Re: Gdi+ Bitmap/Image Clone() function



Mark,

Here is a fragment from a MS news group regarding this subject:
......................
Hi,


How to release IStream after creating object Image by means of constructor
Image(IStream*,...) or by means of function
Image::FromStream(IStream*,...), and why these two functions do not
release the stream and should I store the stream all the time untill Image
is use.


You need post only once per topic - I will ignore you other duplicate
message.

If a Bitmap or Image is created from a stream or file or memory chunk,
that that stream or file or memory is "locked" by GDI+ for the life of
the Bitmap or Image. In the case of stream or memory, you must ensure
the stream or memory stay's "alive" for the life of the Image or Bitmap.


If you want to be able to release it, then you must create a new Bitmap,
and draw or memcpy() the original into the new one. Then the old Bitmap
can be disposed and the stream released.


Thanks,
- John
Microsoft Developer Support
This posting is provided "AS IS" with no warranties, and confers no rights.
Visit http://www.microsoft.com/security for current information on security.

......................

What do you think? It seems to imply that the underling file or stream must
be there for the durration of the image unless a new bitmap is created and
the old one is copied to the new one?!

Is the poster wrong?

Here is the full link:

http://groups.google.com/group/microsoft.public.win32.programmer.gdi/browse_thread/thread/4967097db1469a27/1365bd32d9eaeb9a?lnk=st&q=gdi%2B+load+image+from+file#1365bd32d9eaeb9a


G.

"Mark Salsbery [MVP]" <MarkSalsbery[MVP]@newsgroup.nospam> wrote in message
news:5744EDEA-63D6-4C1B-8150-4456C3C7E47B@xxxxxxxxxxxxxxxx


"GT" <tihenea@xxxxxxxxxxx> wrote in message
news:uZ8NIK42IHA.2104@xxxxxxxxxxxxxxxxxxxxxxx

"Mark Salsbery [MVP]" <MarkSalsbery[MVP]@newsgroup.nospam> wrote in
message news:F74A6D7E-B233-4101-9239-F697319677F3@xxxxxxxxxxxxxxxx
"GT" <tihenea@xxxxxxxxxxx> wrote in message
news:e06RINr2IHA.4920@xxxxxxxxxxxxxxxxxxxxxxx
Mark,

Thanks for the replay. From what you say I can just do this:

1. Create the stream.
2. Move my buffer data into the stream;
3. Do Bitmap* myBmp = new Bitmap(IStream);
4. Now I can safely delete the IStream I created at point 1 and the
bitmap is still valid.
5. Then just use the bitmap whenever I need it (do this in a
function and return the bitmap to be used elsewhere).


Yes. AFAIK the Bitmap is not bound to the stream after it's created.
It just uses the stream to read the bitmap - the stream is not raw pixel
data, it needs to be a supported image file stream (which can be in
memory or on any other stream type).

Again, it's simple to test if you're unsure - just try it! :)
I understand... I thought of loading from an IStream to be similar from
loading from a file. And when you create the bitmap from file you need to
hold on to the file for the duration of the Bitmap object.


Even from a file, it's the same. The image data is read from the file and
converted to whatever the internal Bitmap format is.
The file can then be closed. I've never seen any image loader that
requires the stream/file to remain open.

Mark

--
Mark Salsbery
Microsoft MVP - Visual C++


I will test this :)

Thanks,
G.







.



Relevant Pages

  • Re: Gdi+ Bitmap/Image Clone() function
    ... constructor Image(IStream*,...) or by means of function Image::FromStream, and why these two functions do not release the stream and should I store the stream all the time untill Image is use. ... If a Bitmap or Image is created from a stream or file or memory chunk, ... the stream or memory stay's "alive" for the life of the Image or Bitmap. ...
    (microsoft.public.win32.programmer.gdi)
  • Re: Gdi+ Bitmap/Image Clone() function
    ... Once I closed the bitmap, I was able to delete the file. ... constructor Image(IStream*,...) or by means of function Image::FromStream, and why these two functions do not release the stream and should I store the stream all the time untill Image is use. ... If a Bitmap or Image is created from a stream or file or memory chunk, ... the stream or memory stay's "alive" for the life of the Image or Bitmap. ...
    (microsoft.public.win32.programmer.gdi)
  • Re: Gdi+ Bitmap/Image Clone() function
    ... release the stream and should I store the stream all the time untill ... If a Bitmap or Image is created from a stream or file or memory chunk, ... the stream or memory stay's "alive" for the life of the Image or Bitmap. ...
    (microsoft.public.win32.programmer.gdi)
  • Re: Gdi+ Bitmap/Image Clone() function
    ... Create the stream. ... Now I can safely delete the IStream I created at point 1 and the bitmap is still valid. ... It just uses the stream to read the bitmap - the stream is not raw pixel data, it needs to be a supported image file stream (which can be in memory or on any other stream type). ... Once the objec is created, I want to be able to delete the memory buffer used to create it but, the bitmap object should stay around longer then the memory used to create it. ...
    (microsoft.public.win32.programmer.gdi)
  • Re: Is this the correct way to send a Bitmap over sockets?
    ... // stream is NetworkStream ... Here you don't have to worry about the buffer, ... What if my Bitmap is larger than 1 MB? ...
    (microsoft.public.dotnet.languages.csharp)

Loading