[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.interop

Converting GDI+ Bitmap from unmanaged code to managed code

herc

9/11/2007 3:53:00 PM

I have a very simple (I think) question: How do I convert a unmanaged
GDI+ bitmap into a managed GDI+ Image? The code is C++ CLI (.Net
2.0). There is another option, the Bitmap is being generated by
calling it's function UnlockBits() that takes a unmanaged
Gdiplus::BitmapData structure. Is it easier/safer to convert the
unmanaged Gdiplus::BitmapData structure and pass that into a managed
Bitmap object's UnlockBits() method?

3 Answers

Michael Phillips, Jr.

9/11/2007 4:07:00 PM

0

>I have a very simple (I think) question: How do I convert a unmanaged
> GDI+ bitmap into a managed GDI+ Image? The code is C++ CLI (.Net
> 2.0). There is another option, the Bitmap is being generated by
> calling it's function UnlockBits() that takes a unmanaged
> Gdiplus::BitmapData structure. Is it easier/safer to convert the
> unmanaged Gdiplus::BitmapData structure and pass that into a managed
> Bitmap object's UnlockBits() method?

There are many ways to Marshal a gdiplus bitmap between managed and
unmanaged code.

Here are a few methods:
1) You may use GetHBITMAP() to pass a handle to a bitmap.
2) You may use a stream to represent the bitmap.
3) You may use a global memory pointer to a packed DIB(i.e.,
BITMAPINFOHEADER, ColorTable, bits).
4) You may use a IPictureDisp interface.

Choice #1 is the easiest to implement.


herc

9/11/2007 4:20:00 PM

0

On Sep 11, 12:06 pm, "Michael Phillips, Jr."
<mphillip...@nospam.jun0.c0m> wrote:

> There are many ways to Marshal a gdiplus bitmap between managed and
> unmanaged code.
>
> Here are a few methods:
> 1) You may use GetHBITMAP() to pass a handle to a bitmap.
> 2) You may use a stream to represent the bitmap.
> 3) You may use a global memory pointer to a packed DIB(i.e.,
> BITMAPINFOHEADER, ColorTable, bits).
> 4) You may use a IPictureDisp interface.
>
> Choice #1 is the easiest to implement.

I major concern is not ease, but speed. The unmanaged code is
manipulating the data in the unmanaged BitmapData.Scan0 to generate
the image for display. When using the LockBits() call, the
ImageLockModeUserInputBuf flag is being set. Is it possible to simply
cast the unmanaged BitmapData to a managed BitmapData?

Michael Phillips, Jr.

9/11/2007 4:48:00 PM

0

> I major concern is not ease, but speed. The unmanaged code is
> manipulating the data in the unmanaged BitmapData.Scan0 to generate
> the image for display. When using the LockBits() call, the
> ImageLockModeUserInputBuf flag is being set. Is it possible to simply
> cast the unmanaged BitmapData to a managed BitmapData?

No. LockBits provides a copy of the bitmap's memory for the rectangular
region passed.

When the lock is released the memory copy is committed to the image.

You can use GCHandle.Alloc to pin the memory for the bitmap when it is
created or
you can use CreateDIBSection to create your bitmap.

The pointer to the DIB's memory can be passed back and forth between managed
and unmanaged memory.

There is no protection offered. Reading and writing occur at your peril.

"Cartoper" <cartoper@gmail.com> wrote in message
news:1189527579.653624.255620@d55g2000hsg.googlegroups.com...
> On Sep 11, 12:06 pm, "Michael Phillips, Jr."
> <mphillip...@nospam.jun0.c0m> wrote:
>
>> There are many ways to Marshal a gdiplus bitmap between managed and
>> unmanaged code.
>>
>> Here are a few methods:
>> 1) You may use GetHBITMAP() to pass a handle to a bitmap.
>> 2) You may use a stream to represent the bitmap.
>> 3) You may use a global memory pointer to a packed DIB(i.e.,
>> BITMAPINFOHEADER, ColorTable, bits).
>> 4) You may use a IPictureDisp interface.
>>
>> Choice #1 is the easiest to implement.
>
> I major concern is not ease, but speed. The unmanaged code is
> manipulating the data in the unmanaged BitmapData.Scan0 to generate
> the image for display. When using the LockBits() call, the
> ImageLockModeUserInputBuf flag is being set. Is it possible to simply
> cast the unmanaged BitmapData to a managed BitmapData?
>