Hi, @Willard
Welcome to Microsoft Q&A!
I remember graphics.DrawImage
used 5 parameters. Your usage is wrong.
When "graphics unidentified" occurred, you should add namespace and library.
using namespace Gdiplus;
#pragma comment(lib,"gdiplus.lib")
Also, I made a code sample to show how to use GDI+ to show images.
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment(lib,"gdiplus.lib")
ULONG_PTR gdiplusStartupToken;
Gdiplus::GdiplusStartupInput gdiInput;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
// Register the window class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASSEX wincl = { };
wincl.hInstance = hInstance;
wincl.lpszClassName = CLASS_NAME;
wincl.lpfnWndProc = WindowProc;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof(WNDCLASSEX);
wincl.hIcon = NULL;
wincl.hIconSm = NULL;
wincl.hCursor = NULL;
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
RegisterClassEx(&wincl);
// Create the window.
HWND hwnd = CreateWindowEx(
WS_EX_LAYERED ,
(LPCWSTR)CLASS_NAME,
L"PNGDialog Example Application",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
500,
500,
0,
NULL,
hInstance,
NULL
);
RECT wndRect;
SIZE wndSize;
HDC hdc;
POINT ptSrc;
BLENDFUNCTION blendFunction;
HDC screenDC;
HDC memDC;
HBITMAP memBitmap;
GetWindowRect(hwnd, &wndRect);
wndSize = { wndRect.right - wndRect.left,wndRect.bottom - wndRect.top };
hdc = GetDC(hwnd);
memDC = CreateCompatibleDC(hdc);
memBitmap = ::CreateCompatibleBitmap(hdc, wndSize.cx, wndSize.cy);
::SelectObject(memDC, memBitmap);
Gdiplus::Image image(L"001.bmp");
Gdiplus::Graphics graphics(memDC);
graphics.DrawImage(&image, 0, 0, wndSize.cx, wndSize.cy);
// Run the message loop.
screenDC = GetDC(NULL);
ptSrc = { 0,0 };
UpdateLayeredWindow(hwnd, screenDC, &ptSrc, &wndSize, memDC, &ptSrc, 0, &blendFunction, 4);
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
Gdiplus::GdiplusStartup(&gdiplusStartupToken, &gdiInput, NULL);
switch (uMsg)
{
case WM_DESTROY:
Gdiplus::GdiplusShutdown(gdiplusStartupToken);
PostQuitMessage(0);
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
SIZE wndSize;
HBITMAP memBitmap;
HDC memDC;
HDC hdc = BeginPaint(hwnd, &ps);
// All painting occurs here, between BeginPaint and EndPaint.
FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
EndPaint(hwnd, &ps);
}
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
Thank you.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.