0
var bitmap win.BITMAP
win.GetObject(win.HGDIOBJ(hBitmap), unsafe.Sizeof(bitmap), unsafe.Pointer(&bitmap))
var bmpInfo win.BITMAPINFO
bmpInfo.BmiHeader.BiSize = uint32(unsafe.Sizeof(Info.BmiHeader))
....................Width = width
....................Height = height
....................Planes = 1
....................BitCount = 24
....................Compression = win.BI_RGB
var hdc win.HDC
hdc = win.GetDC(0)
win.GetDIBits(hdc, bitmap, 0, uint32(bitmap.BmHeight), bitmapInfo, nil, 0)
pBits := make([]byte, imageSize)
win.GetDIBits(hdc, bitmap, 0, uint32(bitmap.BmHeight), bitmapInfo, (*byte)(unsafe.Pointer(&pBits)), 0)

use package github.com/lxn/win

First of all, we have done all the work before getting the GetDIBits function.

After that, I finally try to get the image data using the GetDIBits function, but it keeps returning 0.

I changed the data type of pBits to 'unsafe.Pointer' or '* byte' and tried it but it returned nil or 0.

How can I get the image data from the GetDIBits function?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
SangGeol
  • 65
  • 8
  • Guessing you are using ```github.com/lxn/win``` (please specify packages used to avoid guesswork). In that case try passing ```&pBits[0]``` into the function (see [this question](https://stackoverflow.com/questions/16375997/from-byte-to-char) for more info). – Brits Feb 19 '20 at 02:25
  • @Brits The package is specified in the text. And I tried it the way you told me, so the value is 0. – SangGeol Feb 19 '20 at 02:51
  • 1
    Thanks for updating the question; unfortunately any further suggestions would be guesswork without further info. Are you able to add a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)? – Brits Feb 19 '20 at 03:16
  • I agree with Brits, you may first provide a example for our test.And if you are interested, here is a silmilar C++ [case](https://stackoverflow.com/questions/23313903/using-getdibits-to-load-a-bitmap) for your reference. – Strive Sun Feb 19 '20 at 07:55
  • @Brits Updated contents!! – SangGeol Feb 19 '20 at 08:45
  • @SangGeol The parameters of GetDIBits seem incorrect, please see my answer. – Strive Sun Feb 19 '20 at 09:59

1 Answers1

0

I don't know much about Go language, but when you use GetDIBits, the position of parameters is incorrect.

lpvBits

A pointer to a buffer to receive the bitmap data. If this parameter is NULL, the function passes the dimensions and format of the bitmap to the BITMAPINFO structure pointed to by the lpbi parameter.

lpbmi

A pointer to a BITMAPINFO structure that specifies the desired format for the DIB data.

You can modify it like this:

win.GetDIBits(hdc, bitmap, 0, uint32(bitmap.BmHeight), nil, bitmapInfo, 0)
pBits := make([]byte, imageSize)

win.GetDIBits(hdc, bitmap, 0, uint32(bitmap.BmHeight), (*byte)(unsafe.Pointer(&pBits)), bitmapInfo, 0)

Refer: Using GetDIBits to load a bitmap

Strive Sun
  • 5,988
  • 1
  • 9
  • 26
  • I'm a bit confused by this answer. The signature of the function is ```func GetDIBits(hdc HDC, hbmp HBITMAP, uStartScan uint32, cScanLines uint32, lpvBits *byte, lpbi *BITMAPINFO, uUsage uint32) int32``` so ```uUsage``` (becomes ```usage``` in the winapi call) is an integer (which can be 0 but not null) and the [microsoft docs](https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-getdibits) say that [```DIB_RGB_COLORS```](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-emf/a5e722e3-891a-4a67-be1a-ed5a48a7fda1) = ```0x00``` so that looks correct? – Brits Feb 19 '20 at 07:45