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

Shyam Butani 380 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 development | Windows App SDK
Windows development | Windows API - Win32
{count} votes

1 answer

Sort by: Most helpful
  1. Xiaopo Yang - MSFT 12,731 Reputation points Microsoft External Staff
    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.