Hello @Chau Dang Ba ,
TextureBrush does not directly make the image blurry. I used TextureBrush to draw an image in a blank project and it did not lose clarity. It is recommended that you save pImageStream to file multiple times in your code to find which code causes the image to be blurry.
#include <windows.h>
#include <gdiplus.h>
#include <xpsobjectmodel.h>
#include <wincodec.h>
using namespace Gdiplus;
#pragma comment (lib,"Gdiplus.lib")
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
Graphics graphics(hdc);
// Load the image
Image image(L"Test.png");
// Create a TextureBrush
TextureBrush textureBrush(&image, WrapModeTile);
// Set high-quality interpolation mode
graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic);
// Set anti-aliasing
graphics.SetSmoothingMode(SmoothingModeAntiAlias);
// Define the rectangle to paint
Rect rect(0, 0, 2000, 1800);
// Paint the image using TextureBrush
graphics.FillRectangle(&textureBrush, rect);
EndPaint(hwnd, &ps);
return 0;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pCmdLine, INT iCmdShow) {
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
// Register the window class
const wchar_t CLASS_NAME[] = L"GDI+ TextureBrush Example";
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
// Create the window
HWND hwnd = CreateWindowEx(0, CLASS_NAME, L"GDI+ TextureBrush Example", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 2000, 1800, NULL, NULL, hInstance, NULL);
if (hwnd == NULL) {
return 0;
}
ShowWindow(hwnd, iCmdShow);
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
GdiplusShutdown(gdiplusToken);
return 0;
}