Marshal and BitmapInfo questions

A

active

The structue below contains:

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=256)>

which might work for some examples but in general the size needs to be set
at run time (to

InBitmap.Palette.Entries.Length) before the structure is passed to a windows
API function.



How to handle the structure array?

How to fill in the structure?

I've found some API functions that might do it after more study. Is that the
way to go or do I simply fill in the data?

Thanks in advance for any help





Public Structure BitmapInfo

Public bmiHeader As BitmapInfoHeader

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=256)> Public bmiColors() As
RGBQuad

End Structure

Public Structure BitmapInfoHeader

Public biSize As Integer

Public biWidth As Integer

Public biHeight As Integer

Public biPlanes As Short

Public biBitCount As Short

Public biCompression As Integer

Public biSizeImage As Integer

Public biXPelsPerMeter As Integer

Public biYPelsPerMeter As Integer

Public biClrUsed As Integer

Public biClrImportant As Integer

End Structure

Public Structure RGBQuad

Public rgbBlue As Byte

Public rgbGreen As Byte

Public rgbRed As Byte

Public rgbReserved As Byte

End Structure
 
A

active

Added some background
The structue below contains:

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=256)>

which might work for some examples but in general the size needs to be set
at run time (to InBitmap.Palette.Entries.Length) before the structure is
passed to a windows API function.

This is where the BitmapInfo structure is used:

Public Declare Auto Function CreateDIBSection Lib "gdi32" (ByVal hdc As
IntPtr, ByRef pbmi As BitmapInfo, ByVal iUsage As System.UInt32, ByRef
ppvBits As IntPtr, ByVal hSection As Int32, ByVal dwOffset As System.UInt32)
As IntPtr



How to handle the structure array?

How to fill in the structure?

I've found some API functions that might do it after more study. Is that
the way to go or do I simply fill in the data?


Should I use
<DllImport("gdi32.dll", EntryPoint:="GetDIBits")> _

Private Shared Function GetDIBits(ByVal aHDC As IntPtr, _

ByVal hBitmap As IntPtr, ByVal nStartScan As Integer, _

ByVal nNumScans As Integer, ByRef lpBits As Byte, _

ByRef lpBI As BITMAPINFO, ByVal wUsage As Integer) As Integer

End Function



to fill in the structure?




Thanks in advance for any help





Public Structure BitmapInfo

Public bmiHeader As BitmapInfoHeader

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=256)> Public bmiColors()
As RGBQuad

End Structure
 
M

Mattias Sjögren

How to handle the structure array?

The CLR doesn't support variable length structures, so you can't
declare BitmapInfo like that.

The usual solution (if you don't want to switch to C++) is to
dynamically allocate a chunk of unmanaged memory and work with that as
if it was an instance of the structure instead. It involves a lot of
manual copying to and from the buffer using the Marshal class and its
PtrToStructure and StructureToPtr methods.



Mattias
 
A

active

So instead of
ByRef pbmi As BitmapInfo
the argument would be
ByRef pbmi As IntPtr

ByRef or ByVal?

I can do the PtrToStructure and StructureToPtr,
But how to define the structure?

Would the below work?

I'd like to be able to do

dim z as RGBQuad=bi.bmiColor(22)
Public Structure BitmapInfo

Public bmiHeader As BitmapInfoHeader

Public bmiColors() As RGBQuad

End Structure





Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top