473,387 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Marshal and BitmapInfo questions

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
Aug 9 '07 #1
5 3757
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
>
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


Aug 9 '07 #2
>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Aug 9 '07 #3
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
"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:O$**************@TK2MSFTNGP02.phx.gbl...
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Aug 9 '07 #4
>So instead of
>ByRef pbmi As BitmapInfo
the argument would be
ByRef pbmi As IntPtr

ByRef or ByVal?
ByVal

>I can do the PtrToStructure and StructureToPtr,
But how to define the structure?
Youprobably don't need it at all. You just work with its members -
BitmapInfoHeader and the RGBQuad array.

Here's an example of the general idea

http://www.dotnetinterop.com/faq/?q=...leLengthStruct
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Aug 9 '07 #5
thanks a lot
"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
So instead of
ByRef pbmi As BitmapInfo
the argument would be
ByRef pbmi As IntPtr

ByRef or ByVal?

ByVal

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

Youprobably don't need it at all. You just work with its members -
BitmapInfoHeader and the RGBQuad array.

Here's an example of the general idea

http://www.dotnetinterop.com/faq/?q=...leLengthStruct
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Aug 15 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Angel | last post by:
Hi again, I'm trying to call functions from a proprietary DLL but it's turned out to be more difficult than I thought. I have this W32.DLL which was written in C by USPS. They don't provide the...
13
by: Just Me | last post by:
The following almost works. The problem is Marshal.PtrToStringAuto seems to terminate at the first null so I don't get the full string. Any suggestions on how to fix this? Or how to improve the...
6
by: william.thorpe.b | last post by:
I have recently switched from VS2003 to VS2005 and at the same time from V1 to V2 of the .NET Compact Framework. The target is a Windows CE 5.0 device and an ARMV4I processor. ...
2
by: Pierre Rouleau | last post by:
Hi all, When using Python 2.4.x on a Win32 box, marshal.loads(marshal.dumps(1e66666)) returns 1.0 instead of infinity as it should and does under Python 2.5 (also running on Win32 ). This...
5
by: Anurag | last post by:
I have been chasing a problem in my code since hours and it bolis down to this import marshal marshal.dumps(str(123)) != marshal.dumps(str("123")) Can someone please tell me why? when...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
0
by: Charming12 | last post by:
Hi All, I have a strange problem and due to my inefficiency with IntPtr i am unable to figure it out. I have an structure something like: public struct Detail { public int age; public...
0
by: xrxst32 | last post by:
Hello there, I have some doubts about the best practice for using COM automation, the Runtime Callable Wrapper (RCW) and Marshal.ReleaseComObject. So the question is/are: Do I need to release...
2
by: O.B. | last post by:
I have operation within a class that marshals the data into a byte array. Below are three different ways that work. Are there any downsides to using one over the the other? public virtual byte...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.