本文介绍了如何将 PNG 从 RESOURCES 加载到 CImage 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此文档 LoadFromResource 它指出:

According to this documentation for LoadFromResource it states:

BITMAP 资源加载图像

所以,我的代码中有这个:

So, I have this in my code:

rImage.LoadFromResource(AfxFindResourceHandle(), IDB_PNG1);

不起作用.然后我意识到我使用的是 PNG 文件而不是 BMP 文件.我认为这就是找不到资源的原因.

Doesn't work. I then realised I am using PNG files and not BMP files. I assume this is the reason the resources can't be found.

我也尝试过使用 AfxGetInstanceHandle().但这也行不通.

I have also tried using AfxGetInstanceHandle(). But that also doesn't work.

因此,目前我使用的是外部 PNG 文件.它工作正常.但是有没有任何方法可以将资源中的 PNG 加载到 CImage 中?

Thus, at the moment I am using external PNG files. It works fine. But is there any way to load a PNG from the resources into a CImage?

提供给我的评论很有帮助.

The comment provided to me was helpful.

它让我这里.所以,如果我有这个方法:

It led me here. So, if I have this method:

IStream* CreateStreamOnResource(LPCTSTR lpName, LPCTSTR lpType)
{
    IStream * ipStream = NULL;

    HRSRC hrsrc = FindResource(NULL, lpName, lpType);
    if (hrsrc == NULL)
        goto Return;

    DWORD dwResourceSize = SizeofResource(NULL, hrsrc);
    HGLOBAL hglbImage = LoadResource(NULL, hrsrc);
    if (hglbImage == NULL)
        goto Return;

    LPVOID pvSourceResourceData = LockResource(hglbImage);
    if (pvSourceResourceData == NULL)
        goto Return;

    HGLOBAL hgblResourceData = GlobalAlloc(GMEM_MOVEABLE, dwResourceSize);
    if (hgblResourceData == NULL)
        goto Return;

    LPVOID pvResourceData = GlobalLock(hgblResourceData);

    if (pvResourceData == NULL)
        goto FreeData;

    CopyMemory(pvResourceData, pvSourceResourceData, dwResourceSize);

    GlobalUnlock(hgblResourceData);

    if (SUCCEEDED(CreateStreamOnHGlobal(hgblResourceData, TRUE, &ipStream)))
        goto Return;

FreeData:
    GlobalFree(hgblResourceData);

Return:
    return ipStream;
}

然后这样称呼它:

rImage.Load(CreateStreamOnResource(MAKEINTRESOURCE(IDB_PNG1), _T("PNG")));

工作正常......谢谢大家.

Works fine ... Thanks guys.

基于提供的评论.现在好点了吗?我没有看到任何泄漏:

Based on comment provided. This better now? I see no leaks:

IStream *pStream = CreateStreamOnResource(MAKEINTRESOURCE(uPNGResourceID), _T("PNG"));
if (pStream != nullptr)
{
    rImage.Load(pStream);
    rImage.SetHasAlphaChannel(true);
    pStream->Release();
}

旁注:如果这些是我自己的嵌入式资源并且我知道是透明的 32 位资源,我真的需要调用 SetHasAlphaChannel 吗?

Side note: Do I really need to call SetHasAlphaChannel if these are my own embedded resources and I know the are transparent 32 bit resources?

推荐答案

  1. AfxFindResource
  2. 加载资源
  3. GlobalAlloc
  4. CopyMemory
  5. CreateStreamOnHGlobal
  6. CImage::Load(IStream*...)

或者您编写自己的 IStream 实现,该实现从 LoadResource 获取指针和大小.CSharedFile 也是一个选项.

Or you write your own IStream implementation that takes a pointer and a size from LoadResource. Also CSharedFile is an option.

此外,新的 MFC 提供了一个 CPngBitmap,允许直接从内存源加载 PNG 文件作为 HBITMAP.

Also the new MFC provides a CPngBitmap that allows to load a PNG file as a HBITMAP directly from a memory source.

这篇关于如何将 PNG 从 RESOURCES 加载到 CImage 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 20:50
查看更多