C#:DIB字节数组到位图 [英] C# : DIB byte array to bitmap
问题描述
我正在使用软件的COM接口,特别是OMICRON MPD 600。我想使用提供的方法截取屏幕截图,但是它会在字节数组中填充DIB时返回图像。
如何使用此字节数组将此屏幕截图保存为一个图像。我更喜欢DIB到Bitmap转换,但实际上任何允许我将其保存为图像的东西都是可以接受的。
我有一个VBA代码,可以使用以下方法完成此任务:
步骤1:CopyMemory
步骤2:GetDC
步骤3:CreateCompatibleDC
步骤4:CreateCompatibleBitmap
步骤5:SelectObject
步骤6:SetDIBits
步骤7:OleCreatePictureIndirect
但是当我尝试使用C#这样做我第一步陷入困境,我从字节数组中提取BitmapInfoHeader。我能够提取他的值,但这些值似乎不正确。
我尝试过:
int size = System.Runtime.InteropServices.Marshal.SizeOf(typeof(BITMAPINFOHEADER));
Debug.Print(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));
高于C#代码给我:
字节数组长度:897860
biWidth之后:-256
biHeight之后:511
biSize之后:256
biSizeImage之后:3005743104
biBitCount之后:0
当工作的VBA代码产生:< br $>
字节数组长度:897832
biBitCount:32
biClrImportant:0
biClrUsed:0
biCompression:0
biHeight:448
biPlanes:1
biSize:40
biSizeImage:0
biWidth:501
biXPelsPerMeter:3110
Code Project上有一篇文章将为您解答这个问题: DIB to System.Bitmap [ ^ ]
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
There is an article right here on Code Project that will answer this for you: DIB to System.Bitmap[^]
这篇关于C#:DIB字节数组到位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!