CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: print bmp

  1. #1
    Join Date
    May 1999
    Posts
    40

    print bmp

    hi.

    How can I print bmp?

    Thanks for any hints.


  2. #2
    Join Date
    May 1999
    Posts
    5

    Re: print bmp

    Her is my print function, taken out of my print class.

    Hope this helps.

    Lars Dirks

    BOOL GPrint::Bitmap(int iID, double dXPos, double dYPos, double dXSize, double dYSize, double xScale)
    {
    BITMAP bm;
    CBitmap bitmap;
    CBitmap* pOldBitmap = NULL;
    CRect rcDest;
    CDC sourceDC;

    if (!bitmap.LoadBitmap(iID))
    return FALSE;

    bitmap.GetObject(sizeof(BITMAP), &bm);

    if (!sourceDC.CreateCompatibleDC(m_pDC))
    return FALSE; // not enough memory

    pOldBitmap = sourceDC.SelectObject(&bitmap);

    bitmap.GetObject(sizeof(bm), &bm);

    rcDest = CRect(m_iPixLeftMargin + (int)(dXPos / 25.4 * m_iLogPixelsX),
    m_iPixTopMargin + (int)(dYPos / 25.4 * m_iLogPixelsY),
    m_iPixLeftMargin + (int)((dXPos + dXSize) / 25.4 * m_iLogPixelsX),
    m_iPixTopMargin + (int)((dYPos + dYSize) / 25.4 * m_iLogPixelsY));

    m_pDC->StretchBlt(m_iPixLeftMargin + (int)(dXPos / 25.4 * m_iLogPixelsX),
    m_iPixTopMargin + (int)(dYPos / 25.4 * m_iLogPixelsY),
    rcDest.Width(),
    rcDest.Height(),
    &sourceDC,
    0,
    0,
    bm.bmWidth,
    bm.bmHeight,
    SRCCOPY);

    sourceDC.SelectObject(pOldBitmap);

    return TRUE;
    }



  3. #3
    Join Date
    May 1999
    Posts
    40

    Re: print bmp

    Thank for your help
    I will try your function.


  4. #4
    Join Date
    May 1999
    Posts
    40

    Re: print bmp

    I try it . But the Text can print out.Only the bmp can not.
    My print is HP LaserJet 6P.
    What is wrong ? Thanks for your answer.


  5. #5
    Join Date
    May 1999
    Posts
    5

    Re: print bmp

    Try to convert bitmap to 2 color (black/white)
    i can't renember if it can print in color

    LD


  6. #6
    Join Date
    May 1999
    Posts
    40

    Re: print bmp

    But If I want print color bmp.What can I do?
    Buy a color printer ?

    Tahnks.


  7. #7
    Join Date
    May 1999
    Posts
    5

    Re: print bmp

    That won't help, you have to make som
    conversion, look at Q64520 to get more
    info.

    If you come out with something good, please
    send source to me. (for print bitmap)

    Lars Dirks


  8. #8
    Join Date
    May 1999
    Posts
    40

    Re: print bmp

    I find how to solve it.
    we can convert bmp to dib format.
    And display dib.It works.


  9. #9
    Join Date
    May 1999
    Posts
    5

    Re: print bmp

    Great, will you please email me the source
    that you use to convert and print a bitmap ?

    Lars Dirks
    [email protected]


  10. #10
    Join Date
    May 1999
    Posts
    40

    Re: print bmp

    // WIDTHBYTES performs DWORD-aligning of DIB scanlines. The "bits"
    // parameter is the bit count for the scanline (biWidth * biBitCount),
    // and this macro returns the number of DWORD-aligned bytes needed
    // to hold those bits.

    #define WIDTHBYTES(bits) (((bits) + 31) / 32 * 4)

    /* DIB macros */
    #define IS_WIN30_DIB(lpbi) ((*(LPDWORD)(lpbi)) == sizeof(BITMAPINFOHEADER))
    #define RECTWIDTH(lpRect) ((lpRect)->right - (lpRect)->left)
    #define RECTHEIGHT(lpRect) ((lpRect)->bottom - (lpRect)->top)




    if(pDC->IsPrinting( ))
    {
    RECT drect,srect;
    drect.left=0;
    drect.top=0;
    drect.right=X;
    drect.bottom=Y;

    srect.left=0;
    srect.top=0;
    srect.right=sizeTotal.cx;
    srect.bottom=sizeTotal.cy;

    PaintDIB(pDC->GetSafeHdc(),&drect,BitmapToDIB*m_bitmap,NULL),&srect,NULL);
    }


    HANDLE CNdiaView::BitmapToDIB(HBITMAP hBitmap, HPALETTE hPal)
    {
    BITMAP bm; // bitmap structure
    BITMAPINFOHEADER bi; // bitmap header
    LPBITMAPINFOHEADER lpbi; // pointer to BITMAPINFOHEADER
    DWORD dwLen; // size of memory block
    HANDLE hDIB, h; // handle to DIB, temp handle
    HDC hDC; // handle to DC
    WORD biBits; // bits per pixel

    // check if bitmap handle is valid

    if (!hBitmap)
    return NULL;

    // fill in BITMAP structure, return NULL if it didn't work

    if (!GetObject(hBitmap, sizeof(bm), (LPSTR)&bm))
    return NULL;

    // if no palette is specified, use default palette

    if (hPal == NULL)
    hPal = (HPALETTE)GetStockObject(DEFAULT_PALETTE);

    // calculate bits per pixel

    biBits = bm.bmPlanes * bm.bmBitsPixel;

    // make sure bits per pixel is valid

    if (biBits <= 1)
    biBits = 1;
    else if (biBits <= 4)
    biBits = 4;
    else if (biBits <= 8)
    biBits = 8;
    else // if greater than 8-bit, force to 24-bit
    biBits = 24;

    // initialize BITMAPINFOHEADER

    bi.biSize = sizeof(BITMAPINFOHEADER);
    bi.biWidth = bm.bmWidth;
    bi.biHeight = bm.bmHeight;
    bi.biPlanes = 1;
    bi.biBitCount = biBits;
    bi.biCompression = BI_RGB;
    bi.biSizeImage = 0;
    bi.biXPelsPerMeter = 0;
    bi.biYPelsPerMeter = 0;
    bi.biClrUsed = 0;
    bi.biClrImportant = 0;

    // calculate size of memory block required to store BITMAPINFO

    dwLen = bi.biSize + PpaletteSize((LPSTR)&bi);

    // get a DC

    hDC = ::GetDC(NULL);

    // select and realize our palette

    hPal = SelectPalette(hDC, hPal, FALSE);
    RealizePalette(hDC);

    // alloc memory block to store our bitmap

    hDIB = GlobalAlloc(GHND, dwLen);

    // if we couldn't get memory block

    if (!hDIB)
    {
    // clean up and return NULL

    SelectPalette(hDC, hPal, TRUE);
    RealizePalette(hDC);
    ::ReleaseDC(NULL, hDC);
    return NULL;
    }

    // lock memory and get pointer to it

    lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIB);

    /// use our bitmap info. to fill BITMAPINFOHEADER

    *lpbi = bi;

    // call GetDIBits with a NULL lpBits param, so it will calculate the
    // biSizeImage field for us

    GetDIBits(hDC, hBitmap, 0, (UINT)bi.biHeight, NULL, (LPBITMAPINFO)lpbi,
    DIB_RGB_COLORS);

    // get the info. returned by GetDIBits and unlock memory block

    bi = *lpbi;
    GlobalUnlock(hDIB);

    // if the driver did not fill in the biSizeImage field, make one up
    if (bi.biSizeImage == 0)
    bi.biSizeImage = WIDTHBYTES((DWORD)bm.bmWidth * biBits) * bm.bmHeight;

    // realloc the buffer big enough to hold all the bits

    dwLen = bi.biSize + PpaletteSize((LPSTR)&bi) + bi.biSizeImage;

    if (h = GlobalReAlloc(hDIB, dwLen, 0))
    hDIB = h;
    else
    {
    // clean up and return NULL

    GlobalFree(hDIB);
    hDIB = NULL;
    SelectPalette(hDC, hPal, TRUE);
    RealizePalette(hDC);
    ::ReleaseDC(NULL, hDC);
    return NULL;
    }

    // lock memory block and get pointer to it */

    lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIB);

    // call GetDIBits with a NON-NULL lpBits param, and actualy get the
    // bits this time

    if (GetDIBits(hDC, hBitmap, 0, (UINT)bi.biHeight, (LPSTR)lpbi +
    (WORD)lpbi->biSize + PpaletteSize((LPSTR)lpbi), (LPBITMAPINFO)lpbi,
    DIB_RGB_COLORS) == 0)
    {
    // clean up and return NULL

    GlobalUnlock(hDIB);
    hDIB = NULL;
    SelectPalette(hDC, hPal, TRUE);
    RealizePalette(hDC);
    ::ReleaseDC(NULL, hDC);
    return NULL;
    }

    bi = *lpbi;

    // clean up
    GlobalUnlock(hDIB);
    SelectPalette(hDC, hPal, TRUE);
    RealizePalette(hDC);
    ::ReleaseDC(NULL, hDC);

    // return handle to the DIB
    return hDIB;
    }

    WORD CNdiaView::PpaletteSize(LPSTR lpDIB)
    {
    // calculate the size required by the palette
    if (IS_WIN30_DIB (lpDIB))
    return (DIBNumColors(lpDIB) * sizeof(RGBQUAD));
    else
    return (DIBNumColors(lpDIB) * sizeof(RGBTRIPLE));
    }

    WORD CNdiaView:IBNumColors(LPSTR lpDIB)
    {
    WORD wBitCount; // DIB bit count

    // If this is a Windows-style DIB, the number of colors in the
    // color table can be less than the number of bits per pixel
    // allows for (i.e. lpbi->biClrUsed can be set to some value).
    // If this is the case, return the appropriate value.


    if (IS_WIN30_DIB(lpDIB))
    {
    DWORD dwClrUsed;

    dwClrUsed = ((LPBITMAPINFOHEADER)lpDIB)->biClrUsed;
    if (dwClrUsed)

    return (WORD)dwClrUsed;
    }

    if (IS_WIN30_DIB(lpDIB))
    wBitCount = ((LPBITMAPINFOHEADER)lpDIB)->biBitCount;
    else
    wBitCount = ((LPBITMAPCOREHEADER)lpDIB)->bcBitCount;

    // return number of colors based on bits per pixel

    switch (wBitCount)
    {
    case 1:
    return 2;

    case 4:
    return 16;

    case 8:
    return 256;

    default:
    return 0;
    }
    }

    BOOL WINAPI CNdiaView::PaintDIB(HDC hDC,
    LPRECT lpDCRect,
    HANDLE hDIB,
    LPRECT lpDIBRect,
    CPalette* pPal)
    {
    LPSTR lpDIBHdr; // Pointer to BITMAPINFOHEADER
    LPSTR lpDIBBits; // Pointer to DIB bits
    BOOL bSuccess=FALSE; // Success/fail flag
    HPALETTE hPal=NULL; // Our DIB's palette
    HPALETTE hOldPal=NULL; // Previous palette

    /* Check for valid DIB handle */
    if (hDIB == NULL)
    return FALSE;

    /* Lock down the DIB, and get a pointer to the beginning of the bit
    * buffer
    */
    lpDIBHdr = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
    lpDIBBits = FindDIBBits(lpDIBHdr);

    // Get the DIB's palette, then select it into DC
    if (pPal != NULL)
    {
    hPal = (HPALETTE) pPal->m_hObject;

    // Select as background since we have
    // already realized in forground if needed
    hOldPal = ::SelectPalette(hDC, hPal, TRUE);
    }

    /* Make sure to use the stretching mode best for color pictures */
    ::SetStretchBltMode(hDC, COLORONCOLOR);

    /* Determine whether to call StretchDIBits() or SetDIBitsToDevice() */
    if ((RECTWIDTH(lpDCRect) == RECTWIDTH(lpDIBRect)) &&
    (RECTHEIGHT(lpDCRect) == RECTHEIGHT(lpDIBRect)))
    bSuccess = ::SetDIBitsToDevice(hDC, // hDC
    lpDCRect->left, // DestX
    lpDCRect->top, // DestY
    RECTWIDTH(lpDCRect), // nDestWidth
    RECTHEIGHT(lpDCRect), // nDestHeight
    lpDIBRect->left, // SrcX
    (int)DIBHeight(lpDIBHdr) -
    lpDIBRect->top -
    RECTHEIGHT(lpDIBRect), // SrcY
    0, // nStartScan
    (WORD)DIBHeight(lpDIBHdr), // nNumScans
    lpDIBBits, // lpBits
    (LPBITMAPINFO)lpDIBHdr, // lpBitsInfo
    DIB_RGB_COLORS); // wUsage
    else
    bSuccess = ::StretchDIBits(hDC, // hDC
    lpDCRect->left, // DestX
    lpDCRect->top, // DestY
    RECTWIDTH(lpDCRect), // nDestWidth
    RECTHEIGHT(lpDCRect), // nDestHeight
    lpDIBRect->left, // SrcX
    lpDIBRect->top, // SrcY
    RECTWIDTH(lpDIBRect), // wSrcWidth
    RECTHEIGHT(lpDIBRect), // wSrcHeight
    lpDIBBits, // lpBits
    (LPBITMAPINFO)lpDIBHdr, // lpBitsInfo
    DIB_RGB_COLORS, // wUsage
    SRCCOPY); // dwROP

    ::GlobalUnlock((HGLOBAL) hDIB);

    /* Reselect old palette */
    if (hOldPal != NULL)
    {
    ::SelectPalette(hDC, hOldPal, TRUE);
    }

    return bSuccess;
    }

    LPSTR WINAPI CNdiaView::FindDIBBits(LPSTR lpbi)
    {
    return (lpbi + *(LPDWORD)lpbi + PpaletteSize(lpbi));
    }

    DWORD WINAPI CNdiaView:IBHeight(LPSTR lpDIB)
    {
    LPBITMAPINFOHEADER lpbmi; // pointer to a Win 3.0-style DIB
    LPBITMAPCOREHEADER lpbmc; // pointer to an other-style DIB

    /* point to the header (whether old or Win 3.0 */

    lpbmi = (LPBITMAPINFOHEADER)lpDIB;
    lpbmc = (LPBITMAPCOREHEADER)lpDIB;

    /* return the DIB height if it is a Win 3.0 DIB */
    if (IS_WIN30_DIB(lpDIB))
    return lpbmi->biHeight;
    else /* it is an other-style DIB, so return its height */
    return (DWORD)lpbmc->bcHeight;
    }


    most code I find from codeguru



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