winapiprintingbitmapgdi

How to remove thin black border while printing bitmap with GDI?


I'm working on C++/WinRT application, and trying to print bitmap using printer. But I'm getting thin black border on the printed page.

I'm doing similar to below code snippet.

Please help me to understand the reason behind this and how to remove this border.

// Printer HDC

HDC     hdc_printer = CreateDCW (NULL, L"My Printer Name", NULL, NULL);

// Getting height and width from the printer

int     width       = GetDeviceCaps (hdc_printer, HORZRES);
int     height      = GetDeviceCaps (hdc_printer, VERTRES);

// Creating bitmap

HBITMAP bitmap      = CreateBitmap (width, height, 1, 32, NULL);

// Draw bitmap

// SelectObject (hdc_bitmap, bitmap)

hbrush = CreateSolidBrush (RGB (255, 255, 255));        // White color page
SelectObject (hdc_bitmap, hbrush);
Rectangle    (hdc_bitmap, 0, 0, width, height);

// StartDoc (...)
// StartPage (...)

// Print bitmap using printer

BitBlt (hdc_printer, 0, 0, width, height, hdc_bitmap, 0, 0, SRCCOPY);

// EndPage (...)
// EndDoc (...)

Solution

  • Rectangle fills a rectangle with the selected brush and outlines it with the selected pen. The default pen is a 1-pixel wide black pen.

    You can get the null pen from GetStockObject and select it into the DC before the call to Rectangle, which disables the outlining.

    SelectObject(hdc_bitmap, GetStockObject(NULL_PEN))
    

    Or you can use FillRect.

    There are other ways as well, but either of the two suggestions above should serve you well.