scale an bitmap and display using layered window c++ code
- User
scale an bitmap and display using layered window c++ code
- Assistant
Here is an example code for scaling a bitmap and displaying it using layered window in C++:
#include <Windows.h> #include #include <gdiplus.h>
#pragma comment (lib, "Gdiplus.lib")
using namespace Gdiplus;
// Global variables HDC hdcMem = NULL; HBITMAP hBitmap = NULL; HDC hdcSrc = NULL; HDC hdcDst = NULL; BITMAP bm = { 0 };
void UpdateLayeredWindow(HWND hwnd, HDC hdcDst) { BLENDFUNCTION blend = { 0 }; blend.BlendOp = AC_SRC_OVER; blend.SourceConstantAlpha = 255; blend.AlphaFormat = AC_SRC_ALPHA;
POINT ptDst = { 0, 0 }; SIZE sizeDst = { bm.bmWidth, bm.bmHeight }; POINT ptSrc = { 0, 0 }; COLORREF crKey = RGB(0, 0, 0); UpdateLayeredWindow(hwnd, hdcDst, &ptDst, &sizeDst, hdcSrc, &ptSrc, crKey, &blend, ULW_ALPHA);}
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_CREATE: hdcMem = CreateCompatibleDC(NULL); hBitmap = (HBITMAP)LoadImage(NULL, L"test.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); if (hBitmap == NULL) { MessageBox(hwnd, L"Error loading bitmap", L"Error", MB_OK); PostQuitMessage(0); } SelectObject(hdcMem, hBitmap); GetObject(hBitmap, sizeof(bm), &bm); SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, bm.bmWidth, bm.bmHeight, SWP_SHOWWINDOW); break;
case WM_PAINT: hdcDst = BeginPaint(hwnd, NULL); BitBlt(hdcDst, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY); EndPaint(hwnd, NULL); break; case WM_TIMER: // Scale bitmap hdcSrc = CreateCompatibleDC(NULL); HBITMAP hOldSrc = (HBITMAP)SelectObject(hdcSrc, hBitmap); int newWidth = bm.bmWidth * 2; int newHeight = bm.bmHeight * 2; Bitmap bmp(newWidth, newHeight); Graphics g(&bmp); g.DrawImage(&bmp, 0, 0, bm.bmWidth, bm.bmHeight); BitmapData bmpData; RECT rc = { 0, 0, bm.bmWidth, bm.bmHeight }; bmp.LockBits(&rc, ImageLockModeRead, PixelFormat32bppARGB, &bmpData); hdcDst = GetDC(hwnd); BITMAPINFO bmi = { 0 }; bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmi.bmiHeader.biWidth = bmpData.Width; bmi.bmiHeader.biHeight = bmpData.Height; bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biBitCount = 32; bmi.bmiHeader.biCompression = BI_RGB; hBitmap = CreateDIBSection(hdcDst, &bmi, DIB_RGB_COLORS, NULL, NULL, 0); if (hBitmap == NULL) { MessageBox(hwnd, L"Error creating bitmap", L"Error", MB_OK); PostQuitMessage(0); } hdcMem = CreateCompatibleDC(hdcDst); HBITMAP hOldMem = (HBITMAP)SelectObject(hdcMem, hBitmap); UINT32* pSrc = (UINT32*)bmpData.Scan0; UINT32* pDst = (UINT32*)malloc(bmpData.Width * bmpData.Height * sizeof(UINT32)); for (int i = 0; i < bmpData.Height; i++) { for (int j = 0; j < bmpData.Width; j++) { int indexSrc = i * bmpData.Stride / 4 + j; int indexDst = i * bmpData.Stride / 2 + j * 2; pDst[indexDst] = pSrc[indexSrc]; pDst[indexDst + 1] = pSrc[indexSrc]; pDst[indexDst + bmpData.Width] = pSrc[indexSrc]; pDst[indexDst + bmpData.Width + 1] = pSrc[indexSrc]; } } CopyMemory(bmpData.Scan0, pDst, bmpData.Width * bmpData.Height * sizeof(UINT32)); SelectObject(hdcSrc, hOldSrc); SelectObject(hdcMem, hOldMem); free(pDst); UpdateLayeredWindow(hwnd, hdcDst); DeleteDC(hdcSrc); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, uMsg, wParam, lParam); } return 0;}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { // Initialize GDI+ ULONG_PTR gdiplusToken; GdiplusStartupInput gdiplusStartupInput; GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
// Register window class WNDCLASSEX wcex = { 0 }; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_VREDRAW | CS_HREDRAW; wcex.lpfnWndProc = WndProc; wcex.hInstance = hInstance; wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wcex.lpszClassName = L"MyClass"; if (!RegisterClassEx(&wcex)) { MessageBox(NULL, L"Error registering class", L"Error", MB_OK); return 0; } // Create window HWND hwnd = CreateWindowEx(WS_EX_LAYERED, L"MyClass", L"MyWindow", WS_POPUP, 0, 0, 0, 0, NULL, NULL, hInstance, NULL); if (hwnd == NULL) { MessageBox(NULL, L"Error creating window", L"Error", MB_OK); return 0; } // Set transparency SetLayeredWindowAttributes(hwnd, RGB(0, 0, 0), 255, LWA_COLORKEY | LWA_ALPHA); // Start timer SetTimer(hwnd, 1, 1000, NULL); // Message loop MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } // Clean up GdiplusShutdown(gdiplusToken); DeleteDC(hdcMem); DeleteDC(hdcSrc); DeleteObject(hBitmap); ReleaseDC(hwnd, hdcDst); return (int)msg.wParam;}
- User
scale an bitmap and display using layered window and gdi stretchblt c++ code
- Assistant
Here is an example code for scaling a bitmap and displaying it using layered window and GDI StretchBlt in C++:
#include <Windows.h> #include #include <gdiplus.h>
#pragma comment (lib, "Gdiplus.lib")
using namespace Gdiplus;
// Global variables HDC hdcMem = NULL; HBITMAP hBitmap = NULL; HDC hdcDst = NULL; BITMAP bm = { 0 };
void UpdateLayeredWindow(HWND hwnd, HDC hdcDst) { BLENDFUNCTION blend = { 0 }; blend.BlendOp = AC_SRC_OVER; blend.SourceConstantAlpha = 255; blend.AlphaFormat = AC_SRC_ALPHA;
POINT ptDst = { 0, 0 }; SIZE sizeDst = { bm.bmWidth, bm.bmHeight }; POINT ptSrc = { 0, 0 }; COLORREF crKey = RGB(0, 0, 0); UpdateLayeredWindow(hwnd, hdcDst, &ptDst, &sizeDst, hdcMem, &ptSrc, crKey, &blend, ULW_COLORKEY | ULW_ALPHA);}
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_CREATE: hdcMem = CreateCompatibleDC(NULL); hBitmap = (HBITMAP)LoadImage(NULL, L"test.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); if (hBitmap == NULL) { MessageBox(hwnd, L"Error loading bitmap", L"Error", MB_OK); PostQuitMessage(0); } SelectObject(hdcMem, hBitmap); GetObject(hBitmap, sizeof(bm), &bm); SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, bm.bmWidth, bm.bmHeight, SWP_SHOWWINDOW); break;
case WM_PAINT: hdcDst = BeginPaint(hwnd, NULL); StretchBlt(hdcDst, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY); EndPaint(hwnd, NULL); break; case WM_TIMER: // Scale bitmap HBITMAP hOldBitmap = (HBITMAP)SelectObject(hdcMem, hBitmap); int newWidth = bm.bmWidth * 2; int newHeight = bm.bmHeight * 2; HDC hdcSrc = CreateCompatibleDC(NULL); HBITMAP hBitmapScale = CreateCompatibleBitmap(hdcSrc, newWidth, newHeight); HBITMAP hOldBitmapScale = (HBITMAP)SelectObject(hdcSrc, hBitmapScale); SetStretchBltMode(hdcSrc, HALFTONE); StretchBlt(hdcSrc, 0, 0, newWidth, newHeight, hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY); SelectObject(hdcSrc, hOldBitmapScale); DeleteDC(hdcSrc); SelectObject(hdcMem, hOldBitmap); DeleteObject(hBitmap); hBitmap = hBitmapScale; GetObject(hBitmap, sizeof(bm), &bm); hdcDst = GetDC(hwnd); UpdateLayeredWindow(hwnd, hdcDst); ReleaseDC(hwnd, hdcDst); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, uMsg, wParam, lParam); } return 0;}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { // Initialize GDI+ ULONG_PTR gdiplusToken; GdiplusStartupInput gdiplusStartupInput; GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
// Register window class WNDCLASSEX wcex = { 0 }; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_VREDRAW | CS_HREDRAW; wcex.lpfnWndProc = WndProc; wcex.hInstance = hInstance; wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wcex.lpszClassName = L"MyClass"; if (!RegisterClassEx(&wcex)) { MessageBox(NULL, L"Error registering class", L"Error", MB_OK); return 0; } // Create window HWND hwnd = CreateWindowEx(WS_EX_LAYERED, L"MyClass", L"MyWindow", WS_POPUP, 0, 0, 0, 0, NULL, NULL, hInstance, NULL); if (hwnd == NULL) { MessageBox(NULL, L"Error creating window", L"Error", MB_OK); return 0; } // Set transparency SetLayeredWindowAttributes(hwnd, RGB(0, 0, 0), 255, LWA_COLORKEY | LWA_ALPHA); // Start timer SetTimer(hwnd, 1, 1000, NULL); // Message loop MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } // Clean up GdiplusShutdown(gdiplusToken); DeleteDC(hdcMem); DeleteObject(hBitmap); ReleaseDC(hwnd, hdcDst); return (int)msg.wParam;}