Need some help on memory image conversion using GDI Image class.

Need some help on memory image conversion using GDI Image class.

Post by Ken Var » Sat, 07 Dec 2002 01:35:58


I have a jpeg image loaded into a C++ memory pointer.  I would like to be
able to attach this memory pointer to an Image class and perform a
conversion to another memory pointer using the Image::Save method.

Since the Image class manipulates data through IStreams, I need to assign an
IStream to my memory pointers.  This is where I am confused.  The only way
that I can figure out how to attach an IStream to a memory pointer is via
CreateStreamOnHGlobal() function.  However, I am not sure that I can just
obtain an HGLOBAL from any memory pointer using GlobalHandle() function if
it did not originate from a GlobalAlloc().

I do not want to have to create an additional global memory block just so
that I can attach a stream to it.  I would rather use my existing memory
blocks.

So basically, I am asking if someone can please tell me the proper way to
attach an IStream to a standard C++ pointer?

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

-----------------------------------

 
 
 

Need some help on memory image conversion using GDI Image class.

Post by John Hornick [M » Sat, 07 Dec 2002 07:16:56


Hi,

Quote:> I have a jpeg image loaded into a C++ memory pointer.  I would like to be
> able to attach this memory pointer to an Image class and perform a
> conversion to another memory pointer using the Image::Save method.

> Since the Image class manipulates data through IStreams, I need to assign
an
> IStream to my memory pointers.  This is where I am confused.  The only way
> that I can figure out how to attach an IStream to a memory pointer is via
> CreateStreamOnHGlobal() function.  However, I am not sure that I can just
> obtain an HGLOBAL from any memory pointer using GlobalHandle() function if
> it did not originate from a GlobalAlloc().

> I do not want to have to create an additional global memory block just so
> that I can attach a stream to it.  I would rather use my existing memory
> blocks.

> So basically, I am asking if someone can please tell me the proper way to
> attach an IStream to a standard C++ pointer?

If it were me, I'd just use GlobalAlloc() to create the pointer
in the first place.

You should consider posting on a COM newsgroup since your question
is related to IStream and not to GDI+.

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.

 
 
 

Need some help on memory image conversion using GDI Image class.

Post by Chris Hi » Sat, 07 Dec 2002 12:06:53




Quote:>I do not want to have to create an additional global memory block just so
>that I can attach a stream to it.  I would rather use my existing memory
>blocks.

>So basically, I am asking if someone can please tell me the proper way to
>attach an IStream to a standard C++ pointer?

Since IStream is a COM interface you can provide your own
implementation that works with a pointer to a buffer that you manage.
This way you will have full control over the implementation.
According to the Gdiplus::Image documentation you only need to
implement the Read, Write, Seek, and Stat functions to use Image::Load
and Image::Save on that stream (make the other IStream members return
an error code indicating that they are not supported in your
implementation).

Chris

 
 
 

Need some help on memory image conversion using GDI Image class.

Post by steph » Sat, 07 Dec 2002 23:21:10




> I have a jpeg image loaded into a C++ memory pointer.  I would like to be
> able to attach this memory pointer to an Image class and perform a
> conversion to another memory pointer using the Image::Save method.

> Since the Image class manipulates data through IStreams, I need to assign
an
> IStream to my memory pointers.  This is where I am confused.  The only way
> that I can figure out how to attach an IStream to a memory pointer is via
> CreateStreamOnHGlobal() function.  However, I am not sure that I can just
> obtain an HGLOBAL from any memory pointer using GlobalHandle() function if
> it did not originate from a GlobalAlloc().

> I do not want to have to create an additional global memory block just so
> that I can attach a stream to it.  I would rather use my existing memory
> blocks.

> So basically, I am asking if someone can please tell me the proper way to
> attach an IStream to a standard C++ pointer?

> --
> -----------------------------------
> Ken Varn
> Senior Software Engineer
> Diebold Inc.

> -----------------------------------

here is how i do it (maybe not the proper way because i'm not a master in
COM object but it seems to work) :

BOOL SetImage(BYTE * pBuffer, int nSize)
{
    HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, nSize);
     if(hGlobal == NULL)
     {
       return(FALSE);
     }

    // lock the handle and copy data
     void* pData = GlobalLock(hGlobal);
     memcpy(pData, pBuffer, nSize);
     GlobalUnlock(hGlobal);

    // create stream
     IStream* pStream = NULL;
     if(CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) == S_OK)
     {
          m_pImage = Image::FromStream(pStream);
     }

     FreeResource(hGlobal); // 16Bit Windows Needs This (32Bit - Automatic
Release)

Quote:}

regards

S.

 
 
 

Need some help on memory image conversion using GDI Image class.

Post by John Hornick [M » Sat, 07 Dec 2002 23:54:10


Hi,

Quote:> here is how i do it (maybe not the proper way because i'm not a master in
> COM object but it seems to work) :

> BOOL SetImage(BYTE * pBuffer, int nSize)
> {
>     HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, nSize);
>      if(hGlobal == NULL)
>      {
>        return(FALSE);
>      }

>     // lock the handle and copy data
>      void* pData = GlobalLock(hGlobal);
>      memcpy(pData, pBuffer, nSize);
>      GlobalUnlock(hGlobal);

>     // create stream
>      IStream* pStream = NULL;
>      if(CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) == S_OK)
>      {
>           m_pImage = Image::FromStream(pStream);
>      }

>      FreeResource(hGlobal); // 16Bit Windows Needs This (32Bit - Automatic
> Release)

> }

GlobalFree(), not FreeResource(), is the companion to GlobalAlloc().

You need to keep track of pStream, and Release() it after m_pImage
is destroyed. This may also mean you need to keep hGlobal around -
I'm not sure, ask on a COM newsgroup.

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.

 
 
 

Need some help on memory image conversion using GDI Image class.

Post by Ken Var » Sun, 08 Dec 2002 00:31:24


The only problem I have with this implementation is that you end up using
extra memory allocation by having to allocate a HGLOBAL to transfer in data
that already occupies an existing memory space.  I would prefer not to
allocate another HGLOBAL for performance reasons.

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

-----------------------------------



> > I have a jpeg image loaded into a C++ memory pointer.  I would like to
be
> > able to attach this memory pointer to an Image class and perform a
> > conversion to another memory pointer using the Image::Save method.

> > Since the Image class manipulates data through IStreams, I need to
assign
> an
> > IStream to my memory pointers.  This is where I am confused.  The only
way
> > that I can figure out how to attach an IStream to a memory pointer is
via
> > CreateStreamOnHGlobal() function.  However, I am not sure that I can
just
> > obtain an HGLOBAL from any memory pointer using GlobalHandle() function
if
> > it did not originate from a GlobalAlloc().

> > I do not want to have to create an additional global memory block just
so
> > that I can attach a stream to it.  I would rather use my existing memory
> > blocks.

> > So basically, I am asking if someone can please tell me the proper way
to
> > attach an IStream to a standard C++ pointer?

> > --
> > -----------------------------------
> > Ken Varn
> > Senior Software Engineer
> > Diebold Inc.

> > -----------------------------------

> here is how i do it (maybe not the proper way because i'm not a master in
> COM object but it seems to work) :

> BOOL SetImage(BYTE * pBuffer, int nSize)
> {
>     HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, nSize);
>      if(hGlobal == NULL)
>      {
>        return(FALSE);
>      }

>     // lock the handle and copy data
>      void* pData = GlobalLock(hGlobal);
>      memcpy(pData, pBuffer, nSize);
>      GlobalUnlock(hGlobal);

>     // create stream
>      IStream* pStream = NULL;
>      if(CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) == S_OK)
>      {
>           m_pImage = Image::FromStream(pStream);
>      }

>      FreeResource(hGlobal); // 16Bit Windows Needs This (32Bit - Automatic
> Release)

> }

> regards

> S.

 
 
 

Need some help on memory image conversion using GDI Image class.

Post by steph » Sun, 08 Dec 2002 02:21:04


Quote:>-----Original Message-----
>Hi,

>> here is how i do it (maybe not the proper way because
i'm not a master in
>> COM object but it seems to work) :

>> BOOL SetImage(BYTE * pBuffer, int nSize)
>> {
>>     HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, nSize);
>>      if(hGlobal == NULL)
>>      {
>>        return(FALSE);
>>      }

>>     // lock the handle and copy data
>>      void* pData = GlobalLock(hGlobal);
>>      memcpy(pData, pBuffer, nSize);
>>      GlobalUnlock(hGlobal);

>>     // create stream
>>      IStream* pStream = NULL;
>>      if(CreateStreamOnHGlobal(hGlobal, TRUE, &pStream)
== S_OK)
>>      {
>>           m_pImage = Image::FromStream(pStream);
>>      }

>>      FreeResource(hGlobal); // 16Bit Windows Needs This
(32Bit - Automatic
>> Release)

>> }

>GlobalFree(), not FreeResource(), is the companion to
GlobalAlloc().

>You need to keep track of pStream, and Release() it after
m_pImage
>is destroyed. This may also mean you need to keep hGlobal
around -
>I'm not sure, ask on a COM newsgroup.

I keep track on both, but i think keep track of HGlobal is
not necessary because of the TRUE parameter in
CreateStreamOnHglobal which means free memory when stream
is released...

but as you said it's not the purpose of this group...

bus as a last question, do you have an other method to
load an image from a memory buffer (piece of code ;-))

thanks

sd.

 
 
 

1. Store some of a window image in a image class in GDI+

I am writting a program that is skinned,

I am currently writting the code that will handle buttons, I am using my own
button code, so I am not using windows's window class's for creating
buttons, lists etc.
I am working on a button that will be drawn using a png file (so it will
have transparancy), this will allow the button to be semi transparent in
some areas so the application windows background image will be shown through
some parts of the button.

Currently the image behind the button is not saved, so eachtime a semi
transparent area of the image is drawn (eg. when the button is clicked, or
mouse over) then the transparancy adds up and ends up solid.

So what I want to do is, in the button constructor to save the window image
behind the button, in a image class, so I can draw this and then draw the
semi transparent button after. So the background of the button will be reset
and then the button will be drawn.

How do I save the background image, I've looked at the GDI+ documentation
and cannot work out a way of doing it.

Thank you for any help,
Grant

2. (no subject)

3. Suppressing LISTVIEW Item Images When Using subItem Images

4. Placing Executable Files

5. copy image form GDI to own memory buffer with BYTE *

6. Multiple copies of objects in "Templates" folder

7. MFC: Help needed copying images from memory DC to display DC

8. Site check please -- only take a minute.

9. copy image form GDI to own memory buffer with BYTE *

10. Image resizing/conversion to memory/file

11. Guru's: need Image Class design help!

12. How do I rotate a 2-D bitmap image using GDI or DirectX ?

13. memory leak using GDI+ Image ?