Re: Gdi+ Bitmap/Image Clone() function



"GT" <tihenea@xxxxxxxxxxx> wrote in message news:eVM9P#U3IHA.1196@xxxxxxxxxxxxxxxxxxxxxxx
Mark,

I created a small console app and used this code:

Gdiplus::Bitmap* locBmp = Gdiplus::Bitmap::FromFile( L"C:\\temp\\found001.JPG" );



Interesting. I can't imagine why it holds the file open - I wish they'd document that :)

Thanks for the update!

Mark

--
Mark Salsbery
Microsoft MVP - Visual C++



I run this under the debugger and stopped after creating the image and making sure that it is ok. Then I went to Windows Explorer and I tried to delete the file. I got an error and the file was NOT deleted. Once I closed the bitmap, I was able to delete the file.

George.


"Mark Salsbery [MVP]" <MarkSalsbery[MVP]@newsgroup.nospam> wrote in message news:1486665E-B147-4229-945B-8AEBB3D9C9A0@xxxxxxxxxxxxxxxx


"GT" <tihenea@xxxxxxxxxxx> wrote in message news:eGhLtxL3IHA.4476@xxxxxxxxxxxxxxxxxxxxxxx
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?


Apparently, at least I believe so. Have you tested it yet?
I still disagree until you show me code that proves me wrong :)

Here's my test code:

IStream *pisMWFile = NULL;
if (SUCCEEDED (SHCreateStreamOnFile(_T("E:\\Source\\ImageTIF\\bulldog.tif"), STGM_READ | STGM_SHARE_EXCLUSIVE, &pisMWFile)) )
{
Bitmap bm(pisMWFile, FALSE);
pisMWFile->Release();
Graphics gr(*this);
gr.DrawImage(&bm, 0, 0, 640, 480);
}

It really doesn't make sense to me that the stream would need to stay around for the life of the Bitmap object.
It makes even less sense that a file would have to stay around.

Mark

--
Mark Salsbery
Microsoft MVP - Visual C++



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






.



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
    ... release the stream and should I store the stream all the time untill Image ... If a Bitmap or Image is created from a stream or file or memory chunk, ... I thought of loading from an IStream to be similar from ...
    (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: 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)
  • 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)

Loading