Get bytes from HBITMAP

后端 未结 5 1845
逝去的感伤
逝去的感伤 2021-01-13 13:01

How can I get image bytes from hbitmap if I am given an HBITMAP pointer, and my application is console application. I tryed using GetDIBits which require such parameter as

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-13 13:16

    Since you're using LoadImage to get the HBITMAP, then it is indeed a DIB (Device-Independent Bitmap) (they call it a DIBsection). However, you don't have the color information.

    This MSDN HOWTO shows you how to select the DIBsection into a memory DC. They then go on to use GetDIBColorTable to get the palette. However, I believe from there, with that DC you can use GetDIBits to get the RGB bitmap information as you were trying to do.

    Here's the general gist of it:

    // Create a memory DC and select the DIBSection into it
    hMemDC = CreateCompatibleDC( NULL );
    (HBITMAP)SelectObject( hMemDC, hBitmap );
    
    GetDIBits(hMemDC, hBitmap, ...);
    

    You'll note in their code that SelectObject returns a handle to the what was in the DC. They then restore that before calling DeleteDC. I'm not sure its entirely necessary, but they do it. I left it out here for clarity.

提交回复
热议问题