CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    14

    Zafir Anjum & DDB to DIB

    Any idea why when I use the DDBtoDib routine, and then I do a StretchDIBits to the screen or printer all of the colors get distorted? I tried setting the stretch mode and that did not help either? I was capturing a part of the screen to a bitmap, and from there to a DIB, and then stretchDIBit to the printer?
    The picture is fine, the color awful. Help? Why does this happen?

    Sharon Gottlieb


  2. #2
    Join Date
    Oct 1999
    Location
    WA
    Posts
    2,393

    Re: Zafir Anjum & DDB to DIB

    Show the code here, including how do you call the StretchDIBits and what's your screen color depth ?

    Why don't use just create a DIB section, copy from screen to a memory DC with the DIB section selected ?

    *** Happe Chinese New Year. Try something Asia this month, Visit igs.joyjoy.net ***

  3. #3
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    14

    Re: Zafir Anjum & DDB to DIB

    Thank you for your reply . I am sending you the source that creates the dibs, with a test to the screen and where my actual printing occurs.
    Both strectdibbits display the picture correctly with the wrong colors.
    Any help on this would be greatly appreciated.

    Does the DIBSection idea work from a screen to a printer too? Any ideas?


    HANDLE
    CCalenderDlg:DBtoDIB(LPBITMAPINFOHEADER bminfo)
    {
    CBitmap hbmScreen;
    CBitmap* bmp;
    BITMAP bm;
    HPALETTE hPal;
    HANDLE hDIB;

    CRect rect;
    GetWindowRect(rect);

    CDC hdcScreen, hdcCompatible;
    hdcScreen.CreateDC("DISPLAY", NULL, NULL, NULL);
    hdcCompatible.CreateCompatibleDC(&hdcScreen);

    BOOL bret = hbmScreen.CreateCompatibleBitmap(&hdcScreen, rect.Width(), rect.Height());
    if (!bret)
    return NULL;
    hbmScreen.GetObject(sizeof(bm), (LPSTR)&bm);

    BITMAPINFOHEADER bi;
    ZeroMemory(&bi, sizeof(bi));
    bi.biSize = sizeof(BITMAPINFOHEADER);
    bi.biHeight = bm.bmHeight;
    bi.biWidth = bm.bmWidth;
    bi.biPlanes = 1;
    bi.biCompression = BI_RGB;
    bi.biBitCount = bm.bmPlanes * bm.bmBitsPixel;

    hPal = GetSystemPalette();
    if (hPal) {
    //apply palette to source and destination
    SelectPalette(hdcScreen, hPal, FALSE);
    RealizePalette(hdcScreen);
    SelectPalette(hdcCompatible, hPal, FALSE);
    RealizePalette(hdcCompatible);
    }

    if (!(bmp = (CBitmap*)hdcCompatible.SelectObject(hbmScreen)))
    return NULL;


    BitBlt(hdcCompatible.m_hDC, 0, 0, rect.Width(), rect.Height(), hdcScreen.m_hDC, rect.left, rect.top, SRCCOPY);

    hbmScreen.GetObject(sizeof(bm), (LPSTR)&bm);

    int nColors = (1 << bi.biBitCount);
    if (nColors > 256)
    nColors = 0;
    DWORD dwLen = bi.biSize + nColors * sizeof(RGBQUAD);

    hDIB = GlobalAlloc(GMEM_FIXED, dwLen);
    if (!hDIB) {
    SelectPalette(hdcScreen, hPal, FALSE);
    return NULL;
    }

    LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIB);
    *lpbi = bi;

    GetDIBits(hdcScreen, (HBITMAP)hbmScreen.GetSafeHandle(), 0L, (DWORD)bi.biHeight,
    (LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);

    GlobalUnlock(hDIB);

    bi = *lpbi;
    if (bi.biSizeImage == 0)
    bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) >> 3)
    *bi.biHeight;

    dwLen += bi.biSizeImage;
    HANDLE handle;
    if (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
    hDIB = handle;
    else {
    GlobalFree(hDIB);
    SelectPalette(hdcScreen, hPal, FALSE);
    return NULL;
    }

    lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIB);
    BOOL bGotBits = GetDIBits(hdcScreen, (HBITMAP)hbmScreen.GetSafeHandle(),
    0L, (DWORD)bi.biHeight,
    lpbi,//(LPBYTE)lpbi + (bi.biSize + nColors * sizeof(RGBQUAD)),
    (LPBITMAPINFO)lpbi,
    (DWORD)DIB_RGB_COLORS);
    GlobalUnlock(hDIB);

    if (!bGotBits) {
    GlobalFree(hDIB);
    SelectPalette(hdcScreen, hPal, FALSE);
    return NULL;
    }

    //****test that it will print correctly, by checking to the screen

    SelectPalette(hdcScreen, hPal, FALSE);
    RealizePalette(hdcScreen);
    //SetStretchBltMode(hdcScreen, COLORONCOLOR);

    int ret = StretchDIBits(hdcScreen, 0, 0,
    (WORD)bi.biWidth, (WORD)bi.biHeight,
    //GetDeviceCaps(hdcScreen,HORZRES)/2, GetDeviceCaps(hdcScreen, VERTRES)/2,
    //0, 0, (WORD)bi->biWidth, (WORD)bi->biHeight,
    0, 0, (WORD)bi.biWidth, (WORD)bi.biHeight,
    hDIB, (BITMAPINFO*)&bi, DIB_RGB_COLORS, SRCCOPY);

    //***end of screen test, picture is good, color is terrible

    if (hPal)
    DeleteObject(hPal);


    *bminfo = bi;
    return hDIB;
    }

    void
    CCalenderDlg::OnPrint()
    {
    BITMAPINFOHEADER lpInfo;
    HANDLE lpBits;

    if ((lpBits = DDBtoDIB(&lpInfo)) == NULL)
    return;

    HDC hdcPrn;
    CPrintDialog *printDlg = new CPrintDialog(FALSE, PD_ALLPAGES | PD_RETURNDC, NULL);
    printDlg->m_pd.nMinPage = printDlg->m_pd.nMaxPage = 1;
    printDlg->m_pd.nFromPage = printDlg->m_pd.nToPage = 1;
    printDlg->DoModal();

    hdcPrn = printDlg->GetPrinterDC();
    if (hdcPrn != NULL) {
    CDC* pDC = new CDC;
    pDC->Attach(hdcPrn);
    pDC->StartDoc("");
    pDC->StartPage();

    //*******Prints the picture content fine, color is terrible
    int ret = StretchDIBits(hdcPrn, 0, 0,
    GetDeviceCaps(hdcPrn, HORZRES), GetDeviceCaps(hdcPrn, VERTRES),
    0, 0, (WORD)lpInfo.biWidth, (WORD)lpInfo.biHeight,
    lpBits, (BITMAPINFO*)&lpInfo, DIB_RGB_COLORS, SRCCOPY);


    GlobalFree(lpBits);

    pDC->EndPage();
    pDC->EndDoc();
    pDC->Detach();
    }
    delete printDlg;

    GlobalFree(lpBits);
    }







  4. #4
    Join Date
    Oct 1999
    Location
    WA
    Posts
    2,393

    Re: Zafir Anjum & DDB to DIB

    It's a messing program, I'm sorry to say.

    Here are the problems I can see:

    1) hdcCompatible is generated, but not actually used otherwise.

    2) 1 << (bi.biBitCount) may be wrong for 32-bpp image.

    3) bi is only a BITMAPINFOHEADER, not a full BITMAPINFO. Use it to display image is wrong for images need color table.

    4) hDIB is a global handle to the packed DIB, lpbi is its locked pointer. You can't use it to call GetDIBits and StretchDIBits as the pointer to the pixels, they points to the BITMAPINFO structure.

    Again, use a DIB section, it's much easier. Works both for screen and printer.

    *** Happe Chinese New Year. Try something Asia this month, Visit igs.joyjoy.net ***

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