【问题标题】:Is there any way to draw a PNG image on window without using MFC?有什么方法可以在不使用 MFC 的情况下在窗口上绘制 PNG 图像?
【发布时间】:2010-08-26 14:01:27
【问题描述】:

我正在开发一个Windows API 应用程序而不使用MFC。 我正在使用标准的 Windows 库。

如何在窗口中绘制 PNG 图像?

帮我写一些示例代码。

我试过一些网上有的代码,但都是用MFC的。

【问题讨论】:

  • 为什么不深入研究 MFC 源代码,看看它是如何做到的?
  • MFC 在内部使用 Win32 API,所以肯定会有办法做到这一点。深入研究绘制 PNG 的 MFC 类,看看那里调用了哪些 API

标签: winapi image png


【解决方案1】:

看看this StackOverflow question。它提供了几个选项,应该可以满足您的需求。

改编自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;
}

【讨论】:

    【解决方案2】:

    您的选择是:GDI+、WIC(Windows 映像组件)或libpng

    【讨论】:

      【解决方案3】:

      您可以使用 GDI+。见Loading and Displaying Bitmaps

      【讨论】:

        【解决方案4】:

        下面的代码对我有用。它没有 MFC,可以直接用于在窗口中绘制 PNG 图像。

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

        感谢您的所有支持和快速响应以解决我的问题。

        【讨论】:

          【解决方案5】:

          如果你知道PNG'编码,你可以解码它。所以你可以用任何方式画PNG~

          【讨论】:

            猜你喜欢
            • 2013-06-25
            • 2015-06-19
            • 2018-05-19
            • 2017-05-27
            • 1970-01-01
            • 2012-11-04
            • 1970-01-01
            • 2011-09-20
            • 1970-01-01
            相关资源
            最近更新 更多