CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2004
    Posts
    235

    How to get a pointer to starting bits of HBITMAP?

    I have created a DC using CreateCompatibleDC and attached an HBITMAP created using CreateCompatibleBitmap(). Everything works fine with windows api's . I have tryed using GetObject(HBITMAP, ..., &BITMAP), The BITMAP.bmBits is alway's NULL .


    Code:
    temp = GetObject(hbit, 0, NULL);
    bmp = (BITMAP *)malloc(temp);
    GetObject(hbit, temp, bmp);
    lptr = (long *)bmp->bmBits;
    if(lptr == NULL){MessageBox(hwnd, "it is NULL", "nothing", 0);}
    Anyone know how to get a pointer to the starting bits of a HBITMAP?

  2. #2
    Join Date
    Nov 2002
    Location
    Israel
    Posts
    182
    look this function GetBitmapBits. For Windows 2000 it works.
    Good luck

  3. #3
    Join Date
    Apr 2004
    Posts
    3
    I suggest:

    hdc1 = CreateCompatibleDC(NULL);
    hbmp1 = CreateBitmap(200,100,1,32,nil); //1 pixel = 32 bit, 200*100 pixel

    SelectObject(hdc1,hbmp1);
    hpen1 = CreatePen(PS_SOLID,1,255);
    SelectObject(hdc1,hpen1);
    MoveToEx(hdc1,0,0,NULL);
    LineTo(hdc1,200,100);
    //draw a red line

    GetBitmapBits(hbmp1,200*100*4,lptr);
    //or SetBitmapBits( ... )

    The CreateCompatibleBitmap() does not work well on my computer (it created monochrome bitmap).

  4. #4
    Join Date
    Mar 2004
    Posts
    235
    From MSDN library
    GetBitmapBits

    cbBuffer
    Specifies the number of bytes to copy from the bitmap into the buffer.
    I also thought that GetBitmapBits would work but then I was wondering why Can't I change color of pixels . It turns out that GetBitmapBits copies the bits lol. And I have to use SetBitmapBits to put the modifyed bits back.

    The CreateCompatibleBitmap() does not work well on my computer (it created monochrome bitmap)
    That also happened to me. What I did is create a window and did something similer:
    Code:
    HDC h;
    HDC hnew;
    HBITMAP hbmp;
    h = GetDC(windowhwnd);
    hnew = CreateCompatibleDC(h);
    hbmp = CreateCompatibleBitmap(h, width, height)
    SelectObject(hnew, hbmp);
    Tada. Now we have a bitmap with default color bits .

    CreateDibSection seems to be what I need . But there are some parameters that I don't understand ...
    Last edited by answer; April 20th, 2004 at 03:29 PM.

  5. #5
    Join Date
    Nov 2002
    Location
    Israel
    Posts
    182
    Of course, it's correct. And here is the error:

    HDC hDC = ::GetDC(hWnd);
    HDC hMemDC = ::CreateCompatibleDC(hDC);
    HBITMAP hBitmap = ::CreateCompatibleBitmap(hMemDC, 32, 32);

    I cannot explain it. I think it's becase we deal with handles.
    Good luck

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured