Convert HGlobal into a Stream

  • Thread starter Thread starter Cartoper
  • Start date Start date
C

Cartoper

I have an image stored in a HGlobal and would like to load it into a
System::Image via the System::Image::FromStream(). How do I convert
the HGlobal into a stream?
 
Ok, how do I convert the 'IStream *' to 'System::IO::Stream __gc *' so
I can use it in the .Net world?

You can't convert the unmanaged COM IStream to a managed System:IO::Stream.

If you want a Net solution use a MemoryStream.

Marshal the unmanaged memory represented by your HGlobal to a managed buffer
of type byte.
Create a MemoryStream object from this buffer and pass the MemoryStream
object to Image::FromStream.
 
Michael said:
You can't convert the unmanaged COM IStream to a managed
System:IO::Stream.
If you want a Net solution use a MemoryStream.

Marshal the unmanaged memory represented by your HGlobal to a managed
buffer of type byte.
Create a MemoryStream object from this buffer and pass the
MemoryStream object to Image::FromStream.

Or if copying the memory backed by the HGLOBAL is unacceptable, write your
own implementation of System::IO::Stream that reads (and writes?) the
memory. It's not a lot of code to write.

-cd
 
Or if copying the memory backed by the HGLOBAL is unacceptable, write your
own implementation of System::IO::Stream that reads (and writes?) the
memory. It's not a lot of code to write.

Or just use the UnmanagedMemoryStream class (assuming .NET 2.0 or
later).


Mattias
 
Mattias said:
Or just use the UnmanagedMemoryStream class (assuming .NET 2.0 or
later).

Even better! (I knew it was in there somewhere - just couldn't remember the
name!)

-cd
 
But I am using .Net 1.1 :(

The MemoryStream class is available with Net 1.1 to Net 3.5.

Marshaling the unmanaged HGlobal memory to a managed byte array is not
difficult.

Even better! (I knew it was in there somewhere - just couldn't remember
the
name!)

But I am using .Net 1.1 :(
 
Back
Top