6

I am developing a Windows API application without using MFC. I am using standard Windows libraries.

How do I draw a PNG image in a window?

Help me with some sample code.

I have tried some codes which are available on the Internet, but all are using MFC.

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
Vinayaka Karjigi
  • 1,070
  • 5
  • 13
  • 37
  • 1
    Why don't you dig into MFC source code, see how it did it? – deerchao Dec 15 '09 at 06:13
  • MFC uses the Win32 API internally so there will definitely be a way to do it. Dig into the MFC class that draws the PNG and see what APIs are being called there – A9S6 Dec 15 '09 at 06:39

5 Answers5

6

Take a look at this StackOverflow question. It offers several options which should meet your needs.

Adapted from MSDN:

#include <windows.h>
#include <gdiplus.h>
#include <stdio.h>
using namespace Gdiplus;

void draw()
{
   // start up GDI+ -- only need to do this once per process at startup
   GdiplusStartupInput gdiplusStartupInput;
   ULONG_PTR gdiplusToken;
   GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);


   Rect rect(20,20,50,50);
   Graphics grpx(dc);
   Image* image = new Image(L"SomePhoto.png");
   grpx.DrawImage(Img,rect);

   delete image;

   // shut down - only once per process
   GdiplusShutdown(gdiplusToken);
   return;
}
Community
  • 1
  • 1
Justin Grant
  • 44,807
  • 15
  • 124
  • 208
3

Your choices are: GDI+, WIC(Windows Imaging Component) or libpng

Anders
  • 97,548
  • 12
  • 110
  • 164
2

You can use GDI+. See Loading and Displaying Bitmaps.

Sergey Podobry
  • 7,101
  • 1
  • 41
  • 51
1

The below code worked for me. It's free of MFC and can be used straight away to draw PNG images in a window.

    Gdiplus::Image image(L"C:\\Logo.png") ;

    Gdiplus::Graphics* graphics = Gdiplus::Graphics::FromHDC(GetDC(hWnd));

    RectF ImgRect(0,0,y3/10,y3/10) ;

    Gdiplus::Status result = graphics->DrawImage(&image, ImgRect);

Thanks for all your support and quick response to solve my problem.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vinayaka Karjigi
  • 1,070
  • 5
  • 13
  • 37
0

If you know PNG'coding,you can decoding it. So you can draw PNG in any way~

Smart_Joe
  • 63
  • 1
  • 7