1

I have looked for GetDIBits all over and only seem to find discussion in every other application other than Excel 2016.

I keep getting back 0 for all of the pixel values. I don't know if using Image1.Picture.Handle is the proper statement for the hdc, and I don't know if Image1.Picture is the proper statement for the hbitmap.

I can't even get a result to show up in an image box using the SetDIBits function.

Most of the content on the web uses PictureBox, and Autodraw, and stuff that doesn't seem to be in Excel.

Can anyone please help me solve this problem. It would be greatly appreciated. I would post all the Declarations but I didn't want to be booted of the site my question was too long. Thanks in advance.

Private Sub CommandButton2_Click() 'Userform command button
Dim X As Long 'X coordinates for the pixels
Dim Y As Long 'Y coordinates for the pixels
Dim sw As BITMAP
Dim bmapinfo As BITMAPINFO 'Information about the bitmap
Dim xtPixels() As RGBPixel 'Array to place pixel data into
Dim oPic As IPictureDisp 'Declaration of picture used in this program
Set oPic = Image1.Picture 'making the picture an object
'All of the data below gives information about the picture
bmapinfo.bmiHeader.biSize = 40
bmapinfo.bmiHeader.biWidth = oPic.Width
bmapinfo.bmiHeader.biHeight = oPic.Height
bmapinfo.bmiHeader.biPlanes = 1
bmapinfo.bmiHeader.biBitCount = 24
'bmapinfo.bmiHeader.biCompression = BI_RGB
bmapinfo.bmiHeader.biXPelsPerMeter = ((((bmapinfo.bmiHeader.biWidth * bmapinfo.bmiHeader.biBitCount) + _
            31) \ 32) * 4)
        bmapinfo.bmiHeader.biYPelsPerMeter = bmapinfo.bmiHeader.biXPelsPerMeter - (((bmapinfo.bmiHeader.biWidth _
            * bmapinfo.bmiHeader.biBitCount) + 7) \ 8)
        bmapinfo.bmiHeader.biSizeImage = bmapinfo.bmiHeader.biXPelsPerMeter * Abs(bmapinfo.bmiHeader.biHeight)
  '  GetObjectAPI voPicture.Handle, LenB(tBmp), tBmp
  '  nBitCount = tBmp.bmWidth * tBmp.bmBitsPixel * tBmp.bmHeight \ 4
    ReDim xtPixels(1 To bmapinfo.bmiHeader.biWidth, 1 To bmapinfo.bmiHeader.biHeight)
'All of the data above gives information about the picture
        GetDIBits Image1.Picture.Handle, sw.bmBitsPixel, _
0, bmapinfo.bmiHeader.biHeight, xtPixels(1, 1), bmapinfo, _
DIB_RGB_COLORS


For Y = 1 To UBound(xtPixels, 1) - 1
    For X = 1 To UBound(xtPixels, 2) - 1
        'With xtPixels(Y, X)
        xtPixels(Y, X).Blue = xtPixels(Y, X).Blue
        xtPixels(Y, X).Green = xtPixels(Y, X).Green
        xtPixels(Y, X).Red = xtPixels(Y, X).Red
    Next X, Y
SetDIBits oPic.Handle, oPic.Handle, _
        0, bmapinfo.bmiHeader.biHeight, xtPixels(1, 1), _
        bmapinfo, DIB_RGB_COLORS
    'Set Image2.Picture = oPic.Render

End Sub
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • At very least you should post your declaration for `GetDIBits`. Does it return 0, `ERROR_INVALID_PARAMETER`, or something else? – Comintern Sep 16 '16 at 16:35
  • Yes. Every single array input shows 0. When I use GetBitmapBits, I get the pixel values but not with GetDIBits. I can show you anything you need in order to assess my situation, just ask me. I am confused with the hdc, the hbitmap, and the bitmapinfo mostly. I will try to post the declarations right now, just give me a second please. Thank you. – ElectroMagneTech Sep 16 '16 at 16:44
  • I'm not asking what is in the returned array. I'm asking what the return value of the `GetDIBits` function is. – Comintern Sep 16 '16 at 16:45
  • So: GetDIBits Image1.Picture.Handle, sw.bmBitsPixel, _ 0, bmapinfo.bmiHeader.biHeight, xtPixels(1, 1), bmapinfo, _ DIB_RGB_COLORS Image1.Picture.Handle = 822414627 sw.bmBitsPixel = 0 bmapinfo.bmiHeader.biHeight = 6165 DIB_RGB_COLORS = 0 – ElectroMagneTech Sep 16 '16 at 16:52
  • If I were to equate GetDIBits to a variable like lret, I get zero or nothing. – ElectroMagneTech Sep 16 '16 at 16:55
  • Add `Debug.Print` before `GetDIBits...` It will show up in the Immediate Window. Also, by the declaration, I'm referring to the line that starts with `Public Declare Function GetDIBits`. – Comintern Sep 16 '16 at 16:58
  • Debug.Print GetDIBits; My module is telling me this argument is not optional. – ElectroMagneTech Sep 16 '16 at 17:04
  • Private Declare Function GetDIBits Lib "gdi32.dll" ( _ ByVal hdc As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, _ ByVal nNumScans As Long, ByRef lpBits As Any, _ ByRef lpBI As BITMAPINFO, ByVal wUsage As Long) As Long – ElectroMagneTech Sep 16 '16 at 17:06
  • Private Declare Function SetDIBits Lib "gdi32.dll" ( _ ByVal hdc As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, _ ByVal nNumScans As Long, ByRef lpBits As Any, _ ByRef lpBI As BITMAPINFO, ByVal wUsage As Long) As Long – ElectroMagneTech Sep 16 '16 at 17:07
  • `Debug.Print GetDIBits(Image1.Picture.Handle, sw.bmBitsPixel, 0, bmapinfo.bmiHeader.biHeight, xtPixels(1, 1), bmapinfo, DIB_RGB_COLORS)` – Comintern Sep 16 '16 at 18:26
  • Yes. I am getting a zero value when that line is executed. I see it in the immediate window. What could it possibly be if the bmiheader if full of information? – ElectroMagneTech Sep 16 '16 at 18:44
  • I'd check 2 things. Verify your math on the sizing, and check to see if you are actually getting a valid and compatible hdc from `Image1.Picture.Handle`. Unfortunately there isn't really a good way to find out *why* `GetDIBits` fails other than poking at it. – Comintern Sep 16 '16 at 18:51
  • When you say sizing are you talking about bmapinfo.bmiHeader.biSize = 40? When you say valid and compatible hdc are you saying that Image1.Picture.Handle = 822414627 is not necessarily a valid figure? – ElectroMagneTech Sep 16 '16 at 19:13

0 Answers0