Click here to Skip to main content
16,003,880 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working with COM Interface of a software, OMICRON MPD 600 to be particular. I would like to take a screenshot using the provided methods however it returns the image as DIB populated inside byte array.

How can use this byte array to save this screenshot as an image. I would prefer DIB to Bitmap conversion but actually anything that would allow me to save it as an image is acceptable.

I have a VBA Code that accomplished this task using:

Step 1: CopyMemory 
Step 2: GetDC
Step 3: CreateCompatibleDC
Step 4: CreateCompatibleBitmap
Step 5: SelectObject
Step 6: SetDIBits
Step 7: OleCreatePictureIndirect


However When I tried doing this using C# I get stuck first step where I extract BitmapInfoHeader from the byte array. I am able to extract he values but these values seem incorrect.

What I have tried:

int size = System.Runtime.InteropServices.Marshal.SizeOf(typeof(BITMAPINFOHEADER));
Debug.Print("Size of BitMapInfoHeader: " + size + "");
GCHandle pinned = GCHandle.Alloc(l, GCHandleType.Pinned);
IntPtr lPtr = pinned.AddrOfPinnedObject();
Debug.Print("Byte Array Lenght: " + l.Length + "");          
BITMAPINFOHEADER bim = (BITMAPINFOHEADER)Marshal.PtrToStructure(lPtr, typeof(BITMAPINFOHEADER));


Above C# code Gives me:

Byte Array Lenght: 897860
biWidth after: -256
biHeight after: 511
biSize after: 256
biSizeImage after: 3005743104
biBitCount after: 0


While a working VBA Code produces:

Byte Array Length: 897832
biBitCount: 32
biClrImportant: 0
biClrUsed: 0
biCompression: 0
biHeight: 448
biPlanes: 1
biSize: 40
biSizeImage: 0
biWidth: 501
biXPelsPerMeter: 3110
Posted
Updated 13-Sep-17 13:02pm

1 solution

There is an article right here on Code Project that will answer this for you: DIB to System.Bitmap[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900