views:

294

answers:

1

Dear All,

I have created a window with createwindow() api using vs2005 in c++ on windows Vista

My requirement is to draw image(any format) on that window.

I am not using any MFC in this application

Please help me with some code snippet

with regards

Vinayaka Karjigi

+4  A: 

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

serge_gubenko
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 function
Vinayaka Karjigi
can anybody guide me in putting png image on window without using any MFC
Vinayaka Karjigi