| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 
 | bool __fastcall DrawBitmapToPrinterCanvas(Graphics::TBitmap *pBitmap, TCanvas *pCanvas, TRect rect)
{
    int w = pBitmap->Width;
    int h = pBitmap->Height;
    HDC h_dc = pBitmap->Canvas->Handle;
    HDC h_mem_dc = ::CreateCompatibleDC(h_dc);
    HBITMAP h_mem_bmp = ::CreateCompatibleBitmap(h_dc, w, h);
    HBITMAP h_old_bmp = ::SelectObject(h_mem_dc, h_mem_bmp);
    int result = GDI_ERROR;
 
    //copy the image on the memory dc
    ::BitBlt(h_mem_dc, 0, 0, w, h, h_dc, 0, 0, SRCCOPY);
 
    //delete the mem dc
    ::SelectObject(h_mem_dc, h_old_bmp);
    ::DeleteDC(h_mem_dc);
 
    //get memory for a BITMAPIFO structure
    HANDLE h_bmp_info = ::GlobalAlloc(GHND, sizeof(BITMAPINFO) + (sizeof(RGBQUAD) * 256));
    BITMAPINFO* bmp_info = static_cast<BITMAPINFO*> (::GlobalLock(h_bmp_info));
    //set up the structure
    memset(bmp_info, NULL, sizeof(BITMAPINFO) + (sizeof(RGBQUAD) * 255));
    bmp_info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmp_info->bmiHeader.biPlanes = 1;
    bmp_info->bmiHeader.biBitCount = 16; //ou 32
    bmp_info->bmiHeader.biWidth = w;
    bmp_info->bmiHeader.biHeight = h;
    bmp_info->bmiHeader.biCompression = BI_RGB;
 
    //find out how much memory for the bits
    if (::GetDIBits(h_dc, h_mem_bmp, 0, h, NULL, bmp_info, DIB_RGB_COLORS))
        {
        //Allocate memory for the bits
        HANDLE h_bits = ::GlobalAlloc(GHND, bmp_info->bmiHeader.biSizeImage);
        void *bits = ::GlobalLock(h_bits);
        //this time get the bits
        if (::GetDIBits(h_dc, h_mem_bmp, 0, h, bits, bmp_info, DIB_RGB_COLORS) != 0)
            {
            //send the bits to the printer
            result = StretchDIBits(pCanvas->Handle, rect.Left, rect.Top, rect.Width(), rect.Height(),
                          0, 0, w, h,
                          bits, bmp_info,
                          DIB_RGB_COLORS, SRCCOPY);
            }
        ::GlobalUnlock(bits);
        ::GlobalFree(h_bits);
        }
    //clean up
    ::DeleteObject(h_mem_bmp);
    ::GlobalUnlock(bmp_info);
    ::GlobalFree(h_bmp_info);
    return (result != GDI_ERROR);
} |