How to draw image on a window?
Asked Answered
R

3

19

I have created a window with createwindow() api using VS2005 in C++ on Windows Vista

My requirement is to draw an image (of any format) on that window. I am not using any MFC in this application.

Reni answered 17/11, 2009 at 12:17 Comment(0)
A
35

not exactly sure what is your problem: draw a bitmap on the form, or you would like know how to work with various image formats, or both. Anyways below is an example of how you could load a bitmap and draw it on the form:

HBITMAP hBitmap = NULL;

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;

    switch (message)
    {
<...>

    case WM_CREATE:
        hBitmap = (HBITMAP)LoadImage(hInst, L"c:\\test.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
        break;
    case WM_PAINT:
        PAINTSTRUCT     ps;
        HDC             hdc;
        BITMAP          bitmap;
        HDC             hdcMem;
        HGDIOBJ         oldBitmap;

        hdc = BeginPaint(hWnd, &ps);

        hdcMem = CreateCompatibleDC(hdc);
        oldBitmap = SelectObject(hdcMem, hBitmap);

        GetObject(hBitmap, sizeof(bitmap), &bitmap);
        BitBlt(hdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hdcMem, 0, 0, SRCCOPY);

        SelectObject(hdcMem, oldBitmap);
        DeleteDC(hdcMem);

        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        DeleteObject(hBitmap);
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

LoadImage loads an icon, cursor, animated cursor, or bitmap. Details here

For working with various images formats you can use Windows Imaging Component (see IWICBitmapDecoder) or code from here Loading JPEG and GIF pictures or 3rd party tools like FreeImage or LeadTools

hope this helps, regards

Aslant answered 19/11, 2009 at 2:43 Comment(4)
Hi Serge, it solved my problem of drawing image on window. thanks for the help. but as this takes only bmp,cur and ico files, i need to work on converting png to bmp and then i will pass that bmp to this functionReni
can anybody guide me in putting png image on window without using any MFCReni
If I can save someone a few minutes of searching around, I tried using this answer but it didn't work. After a while of poking I tried to change the sizeof(bitmap) to sizeof(BITMAP) and the code worked. I'm using Visual Studio 2015 on Windows 10. Heres the full line : GetObject(hBitmap, sizeof(BITMAP), &bitmap);Horsa
Why bother saving the old HBITMAP and calling SelectObject again if you delete the DC immediately after?Yolandayolande
P
10
void LoadScreen(HWND hWnd) {
    RECT rect;
    HDC hdc = GetDC(hWnd);
    HBRUSH brush = CreatePatternBrush((HBITMAP)LoadImage(NULL, L"file.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE));
    GetWindowRect(hWnd, &rect);
    FillRect(hdc, &rect, brush);
    DeleteObject(brush);
    ReleaseDC(hWnd, hdc);
}
Parry answered 1/5, 2017 at 21:13 Comment(4)
And the leaked HDC that was never released upon function exit? Windows GDI rules 101: If you Get it, then Release it; if you Create it, then Delete it. You did this correctly with your brush (Create/Delete), but failed to do so with your client DC (Get/Release).Fencer
Ah, thanks :) I resolved it. Just added one line before closing the curly bracket. ReleaseDC(hWnd, hdc);Parry
Functionality wise, how does this compare to serge_gubenko's answer?Rheinland
I don't know, but I see that my solution is much simpler, clean and straight-forwardParry
R
0
#include <windows.h>
#include <string.h>

HBITMAP hBitmap, hOldBitmap;
HDC hdc, hdcMem;
BITMAP bm;
HINSTANCE hI;
PAINTSTRUCT ps;
RECT rect;
RECT rc;

LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
    {
    case WM_CREATE:
    hBitmap = (HBITMAP)LoadImage(hI, "1.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    GetObject(hBitmap, sizeof(BITMAP), &bm);
    hdc = GetDC(hWnd);
    hdcMem = CreateCompatibleDC(hdc);
    hOldBitmap = SelectBitmap(hdcMem, hBitmap);
    ReleaseDC(hWnd, hdc);
    return 0;

    case WM_LBUTTONDOWN:
    //for dragging not only by the title, but also by any part of the window 
    ReleaseCapture();
    SendMessage(hWnd, 0xA1, 2, 0);
    break;
    case WM_PAINT:
    hdc=BeginPaint(hWnd,&ps);
    
    //overlay image with stretching to fit the window 
    GetClientRect(hWnd,&rect);
    SetStretchBltMode(hdc, STRETCH_HALFTONE);
    StretchBlt(hdc,0,0,rect.right,rect.bottom,
    hdcMem,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
    
    EndPaint(hWnd,&ps);
    break;      


    case WM_DESTROY:
    PostQuitMessage(0);
      
    DeleteDC(hdcMem);
    DeleteObject(hBitmap);
    DeleteObject(hOldBitmap);
    break;
    }
return DefWindowProc(hWnd, msg, wParam, lParam);
}
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPInst, LPSTR lpCmdLine, int nCmdShow)
{
//copying a pointer to a running application instance (module)
hI=hInstance;

WNDCLASS wc;

wc.style         = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc   = WindowProcedure;
wc.hInstance     = hInstance;
wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
wc.lpszClassName = "test_class";
wc.lpszMenuName  = NULL;
wc.cbClsExtra    = 0;
wc.cbWndExtra    = 0;

RegisterClass(&wc);

HWND hWnd = CreateWindow(wc.lpszClassName, "Image Window", 
//window with title (overlapping window) 
WS_OVERLAPPEDWINDOW,
//window without title
//WS_VISIBLE | WS_POPUP | WS_SYSMENU | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
CW_USEDEFAULT, CW_USEDEFAULT, 500, 500, NULL, NULL, hInstance, NULL);

ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);

MSG msg;
while(GetMessage (&msg, NULL, 0, 0))
    {
    DispatchMessage (&msg);
    TranslateMessage (&msg);
    }
UnregisterClass(wc.lpszClassName, hInstance);
return (int) msg.wParam;
}

enter image description here

Russo answered 30/3, 2021 at 11:34 Comment(1)
I know this answer is old, but I'd like to explain why it isn't very good. For one, you need to explain how your code works, and you should actually answer the question instead of just posting giant blocks of code.Landloper

© 2022 - 2024 — McMap. All rights reserved.