开发者

Is there any way to draw a PNG image on window without using MFC?

开发者 https://www.devze.com 2022-12-14 13:21 出处:网络
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?

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开发者_如何学C, but all are using MFC.


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;
}


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


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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消