How to remove thin black border while printing bitmap in Win32 app

Shyam Butani 20 Reputation points
2024-04-09T12:49:16.9433333+00:00

Hello,

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.

// 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)
// StartDoc (...)
// StartPage (...)

// Print bitmap using printer

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

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

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

Thanks.

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
727 questions
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,426 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Xiaopo Yang - MSFT 11,496 Reputation points Microsoft Vendor
    2024-04-10T02:03:21.4366667+00:00

    According to lpBits of CreateBitmap, you may need to initialize the color data. And you should try CreateCompatibleBitmap.

    0 comments No comments