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

    How to use Get/SetDIBits() like Get/SetBitmapBits()?

    For now I'm just trying to copy one bitmap created with LoadImage() to another bitmap created with CreateCompatibleBitmap(). Problem is, the second bitmap ends up with all black pixels. Below is my code so far. I'm not sure about bi.biSizeImage, but I saw it in a few examples I found on MSDN.

    Code:
    hdc = CreateCompatibleDC(NULL);
    bmp1 = (HBITMAP)LoadImage(NULL, L"bmp.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    bmp2 = CreateCompatibleBitmap(hdc, 256, 16);
    
    BITMAP bmp;
    GetObject(bmp1, sizeof(BITMAP), &bmp);
    
    BITMAPINFOHEADER bi;
    bi.biSize = sizeof(BITMAPINFOHEADER);
    bi.biWidth = bmp.bmWidth;
    bi.biHeight = bmp.bmHeight;
    bi.biPlanes = 1;
    bi.biBitCount = 24;
    bi.biCompression = BI_RGB;
    bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) >> 3) * bi.biHeight;
    
    char* bits = new char[bi.biSizeImage];
    int ret1 = GetDIBits(hdc, bmp1, 0, bi.biHeight, bits, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
    int ret2 = SetDIBits(hdc, bmp2, 0, bi.biHeight, bits, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
    After that I select bmp2 into hdc and BitBlt it to a window but it's just a pure black bitmap. I was able to copy using GetBitmapBits() and SetBitmapBits() but I don't want to use those if they're deprecated.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: How to use Get/SetDIBits() like Get/SetBitmapBits()?

    Did you try to add LR_CREATEDIBSECTION flag to LoadImage?
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2005
    Posts
    2,160

    Re: How to use Get/SetDIBits() like Get/SetBitmapBits()?

    Actually, I believe it is your first line. CreateCompatibleDC(NULL) will produce a monochrome DC until some object is selected into it. Instead, try CreateCompatibleDC(GetDC(hWnd)) where hWnd is a valid window handle.

    See:
    http://support.microsoft.com/kb/139165

  4. #4
    Join Date
    Dec 2010
    Posts
    14

    Re: How to use Get/SetDIBits() like Get/SetBitmapBits()?

    I tried your guys' advice but still the same outcome

    I found that the bits array contains only 0's and negative values. So I changed it to type unsigned char* and now it contains the correct pixel values. Still getting a black image in bmp2 however.

  5. #5
    Join Date
    Dec 2010
    Posts
    14

    Resolved Re: How to use Get/SetDIBits() like Get/SetBitmapBits()?

    I think you were right, hoxsiew. The problem was that I used hdc in the call to CreateCompatibleBitmap, thus making bmp2 a monochrome bitmap. I changed it to CreateCompatibleBitmap(GetDC(0), 256, 16) -- problem solved.

    Thanks!
    Last edited by falcon60; December 20th, 2010 at 11:57 AM.

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