Maybe you can test if it works directly with StretchDIBits (without changing intensity in this test) =>
#include <windows.h>
#include <tchar.h>
#include "gdiplus.h"
using namespace Gdiplus;
#pragma comment(lib, "gdiplus.lib")
#include <Urlmon.h> // URLDownloadToCacheFile
#pragma comment (lib, "Urlmon")
#include <shlwapi.h> // SHCreateStreamOnFile
#pragma comment (lib, "shlwapi")
#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
HINSTANCE hInst;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int nWidth = 280, nHeight = 200;
#define IDC_STATIC 10
#define IDC_BUTTON 11
//HBITMAP Create32BPPDIBSection(HDC hDC, int iWidth, int iHeight);
HRESULT CreateNBitHBITMAP(HDC hDC, const SIZE* psize, void** ppvBits, HBITMAP* phBitmap, WORD nBitCount);
BOOL PrintBitmap(HBITMAP hBitmap, int nOrientation);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow)
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
hInst = hInstance;
WNDCLASSEX wcex =
{
sizeof(WNDCLASSEX), CS_HREDRAW | CS_VREDRAW, WndProc, 0, 0, hInst, LoadIcon(NULL, IDI_APPLICATION),
LoadCursor(NULL, IDC_ARROW), (HBRUSH)(COLOR_WINDOW + 1), NULL, TEXT("WindowClass"), NULL,
};
if (!RegisterClassEx(&wcex))
return MessageBox(NULL, TEXT("Cannot register class !"), TEXT("Error"), MB_ICONERROR | MB_OK);
int nX = (GetSystemMetrics(SM_CXSCREEN) - nWidth) / 2, nY = (GetSystemMetrics(SM_CYSCREEN) - nHeight) / 2;
HWND hWnd = CreateWindowEx(0, wcex.lpszClassName, TEXT("Test"), WS_OVERLAPPEDWINDOW, nX, nY, nWidth, nHeight, NULL, NULL, hInst, NULL);
if (!hWnd)
return MessageBox(NULL, TEXT("Cannot create window !"), TEXT("Error"), MB_ICONERROR | MB_OK);
ShowWindow(hWnd, SW_SHOWNORMAL);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hWndButton = NULL, hWndStatic = NULL;
int wmId, wmEvent;
switch (message)
{
case WM_CREATE:
{
// hWndStatic = CreateWindowEx(0, TEXT("Static"), TEXT(""), WS_CHILD | WS_VISIBLE | SS_BITMAP, 10, 10, 200, 200, hWnd, (HMENU)IDC_STATIC, hInst, NULL);
hWndButton = CreateWindowEx(0, L"Button", L"Click", WS_CHILD | WS_VISIBLE | BS_PUSHLIKE, 100, 60, 60, 32, hWnd, (HMENU)IDC_BUTTON, hInst, NULL);
return 0;
}
break;
case WM_COMMAND:
{
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
switch (wmId)
{
case IDC_BUTTON:
{
if (wmEvent == BN_CLICKED)
{
WCHAR wsURL[MAX_PATH] = TEXT("https://image.ibb.co/buLv2e/Little_Girl3.jpg");
WCHAR wsFilename[MAX_PATH];
HRESULT hr = URLDownloadToCacheFile(NULL, wsURL, wsFilename, ARRAYSIZE(wsFilename), 0x0, NULL);
if (SUCCEEDED(hr))
{
IStream* pStream = NULL;
hr = SHCreateStreamOnFile(wsFilename, STGM_READ | STGM_SHARE_DENY_WRITE, &pStream);
if (SUCCEEDED(hr))
{
HBITMAP hBitmap = NULL;
Gdiplus::Bitmap* pBitmap = new Gdiplus::Bitmap(pStream);
if (pBitmap)
{
pBitmap->GetHBITMAP(Gdiplus::Color(255, 255, 255), &hBitmap);
PrintBitmap(hBitmap, DMORIENT_LANDSCAPE);
}
}
}
}
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hDC = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
BOOL PrintBitmap(HBITMAP hBitmap, int nOrientation)
{
HBITMAP hbm = NULL, hBitmapOld;
HDC hDCPrinter = NULL;
HDC hDCMemory, hDCMem;
HDC hDCScreen;
DOCINFO di;
DIBSECTION ds;
BITMAP bmp;
GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bmp);
PRINTDLG pd;
ZeroMemory(&pd, sizeof(PRINTDLG));
pd.lStructSize = sizeof(PRINTDLG);
pd.Flags = PD_RETURNDEFAULT | PD_RETURNDC;
PrintDlg(&pd);
hDCPrinter = pd.hDC;
if (!(GetDeviceCaps(pd.hDC, RASTERCAPS) & RC_STRETCHDIB))
{
MessageBox(NULL, TEXT("StretchDIBits not supported"), TEXT("Error"), MB_OK | MB_ICONSTOP);
return FALSE;
}
DEVMODE* dm = (DEVMODE*)GlobalLock(pd.hDevMode);
dm->dmOrientation = nOrientation;
ResetDC(pd.hDC, dm);
GlobalUnlock(pd.hDevMode);
if (!hDCPrinter)
return FALSE;
hDCScreen = GetDC(NULL);
hDCMem = CreateCompatibleDC(hDCScreen);
hBitmapOld = (HBITMAP)SelectObject(hDCMem, hBitmap);
//hbm = Create32BPPDIBSection(hDCMem, bmp.bmWidth, bmp.bmHeight);
BYTE* pbBitmap;
SIZE sz;
sz.cx = bmp.bmWidth;
sz.cy = bmp.bmHeight;
HRESULT hr = CreateNBitHBITMAP(NULL, &sz, (void**)&pbBitmap, &hbm, 32);
if (!hbm)
{
DeleteDC(hDCPrinter);
ReleaseDC(NULL, hDCScreen);
SelectObject(hDCMem, hBitmapOld);
DeleteDC(hDCMem);
MessageBox(NULL, TEXT("Error Creating DIB Section"), TEXT("Error"), MB_OK |MB_ICONSTOP);
return FALSE;
}
hDCMemory = CreateCompatibleDC(hDCScreen);
SelectObject(hDCMemory, hbm);
BitBlt(hDCMemory, 0, 0, bmp.bmWidth, bmp.bmHeight, hDCMem, 0, 0,SRCCOPY);
ZeroMemory(&di, sizeof(di));
di.cbSize = sizeof(di);
di.lpszDocName = TEXT("Bitmap Printing");
if (StartDoc(hDCPrinter, &di))
{
if (StartPage(hDCPrinter))
{
int nPrinterWidth = GetDeviceCaps(hDCPrinter, HORZRES);
int nPrinterHeight = GetDeviceCaps(hDCPrinter, VERTRES);
int nBitmapWidth = bmp.bmWidth * nPrinterWidth / GetSystemMetrics(SM_CXSCREEN);
int nBitmapHeight = nBitmapWidth * bmp.bmHeight / bmp.bmWidth;
GetObject(hbm, sizeof(DIBSECTION), &ds);
StretchDIBits(hDCPrinter,
0, 0, nBitmapWidth, nBitmapHeight,
0, 0, bmp.bmWidth, bmp.bmHeight,
ds.dsBm.bmBits,
(LPBITMAPINFO)&ds.dsBmih,
DIB_RGB_COLORS,
SRCCOPY);
EndPage(hDCPrinter);
}
EndDoc(hDCPrinter);
}
DeleteDC(hDCPrinter);
DeleteDC(hDCMemory);
SelectObject(hDCMem, hBitmapOld);
DeleteDC(hDCMem);
ReleaseDC(NULL, hDCScreen);
DeleteObject(hbm);
return TRUE;
}
// Adapted from MS SDK
HRESULT CreateNBitHBITMAP(HDC hDC, const SIZE* psize, void** ppvBits, HBITMAP* phBitmap, WORD nBitCount)
{
*phBitmap = NULL;
BITMAPINFO bmi = { 0 };
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = psize->cx;
bmi.bmiHeader.biHeight = psize->cy;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = nBitCount;
bmi.bmiHeader.biCompression = BI_RGB;
HDC hDCUsed = hDC ? hDC : GetDC(NULL);
if (hDCUsed)
{
*phBitmap = CreateDIBSection(hDCUsed, &bmi, DIB_RGB_COLORS, ppvBits, NULL, 0);
if (hDC != hDCUsed)
{
ReleaseDC(NULL, hDCUsed);
}
}
return (NULL == *phBitmap) ? E_OUTOFMEMORY : S_OK;
}