本文介绍了C#:DIB字节数组到位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在使用软件的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代码产生: 字节数组长度: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: CopyMemoryStep 2: GetDCStep 3: CreateCompatibleDCStep 4: CreateCompatibleBitmapStep 5: SelectObjectStep 6: SetDIBitsStep 7: OleCreatePictureIndirectHowever 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: 897860biWidth after: -256biHeight after: 511biSize after: 256biSizeImage after: 3005743104biBitCount after: 0While a working VBA Code produces:Byte Array Length: 897832biBitCount: 32biClrImportant: 0biClrUsed: 0biCompression: 0biHeight: 448biPlanes: 1biSize: 40biSizeImage: 0biWidth: 501biXPelsPerMeter: 3110 解决方案 There is an article right here on Code Project that will answer this for you: DIB to System.Bitmap[^] 这篇关于C#:DIB字节数组到位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-23 09:55