0

我试图找出为什么我无法从资源中加载图像。

  • 当 lpszName 是文件名时,图像工作,当更改为资源时,它们不工作。

  • 图像的创建方式相同(可以互换,同样的问题)

  • 图像位于 Resource 和 Resource.rc 文件中。

编码:

LRESULT CALLBACK WndProc(HWND hWinMain, UINT message, WPARAM wParam, LPARAM lParam)
{
    DWORD
        lastError;
    static HDC
        hdcFromResource,
        hdcFromFilename;
    HBITMAP
        hFromResource,
        hFromResourcePrevious,
        hFromFilename,
        hFromFilenamePrevious;
    HDC 
        hdc;
    PAINTSTRUCT ps;
    switch (message)
    {
    case WM_CREATE:
        hdc = GetDC(hWinMain);
        hdcFromFilename = CreateCompatibleDC(hdc);
        hdcFromResource = CreateCompatibleDC(hdc);
        ReleaseDC(hWinMain, hdc);
        hFromFilename = (HBITMAP)LoadImageW(NULL, (L"filename.bmp"), IMAGE_BITMAP, 100, 100, LR_LOADFROMFILE);
        if (!hFromFilename)
        {
            // ERROR HANDELING
        }
        hFromFilenamePrevious = (HBITMAP)SelectObject(hdcFromFilename, hFromFilename);
        DeleteObject(hFromFilename);
        DeleteObject(hFromFilenamePrevious);
        hFromResource = (HBITMAP)LoadImageW(NULL, MAKEINTRESOURCEW(IDB_RESOURCE), IMAGE_BITMAP, 100, 100, LR_CREATEDIBSECTION | LR_LOADFROMFILE);   
        lastError = GetLastError();
        lastError;
        hFromResourcePrevious = (HBITMAP)SelectObject(hdcFromResource, hFromResource);
        DeleteObject(hFromResource);
        DeleteObject(hFromResourcePrevious);
        return 0;
    case WM_PAINT:
        hdc = BeginPaint(hWinMain, &ps);
        BitBlt(hdc, 0, 0, 100, 100, hdcFromFilename, 0, 0, SRCCOPY);
        BitBlt(hdc, 100, 100, 100, 100, hdcFromResource, 0, 0, SRCCOPY);
        EndPaint(hWinMain, &ps);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hWinMain, message, wParam, lParam);
}
  • 无论图像是否加载,if(!hFromFilename)都不会触发。

  • 图片加载失败后的断点均无中断(通过WM_CREATE的断点,其他函数中的其他断点正常工作)

  • 我不确定如何在没有断点的情况下轻松读取 GetLastError。

资源.h #define IDB_RESOURCE 101

资源.rc #define IDB_RESOURCE BITMAP DISCARDABLE "resource.bmp"

4

1 回答 1

0

The problem as answered by Christopher Oicles was that LR_LOADFROMFILE is not needed.

May be useful for others:

  • To load an image from filename (Using LoadImage()) example:
LoadImage(
NULL,                           // NULL for load image via filename
L("directory\\filename.bmp"),   // String to filename
IMAGE_BITMAP                    // Flag for loading bitmap
0,                              // 0 Should use the images actual width
0,                              // 0 Should use the images actual height
LR_LOADFROMFILE                 // Flag for loading from file
);
  • To load an image from resource (Using LoadImage()) example:
LoadImage(
hInstance,                      // hInstance to the file containing the resource
MAKEINTRESOURCE(IDB_RESOURCE),  // Resource definition
IMAGE_BITMAP                    // Flag for loading bitmap
0,                              // 0 Should use the images actual width
0,                              // 0 Should use the images actual height
LR_CREATEDIBSECTION             // Do not use LR_LOADFROMFILE if it is a resource
);

Resourcefile.h

#define IDB_RESOURCE 101                         // Resource ID, and number

Resourcescript.rc

#include "Resourcefile.h"
IDB_RESOURCE BITMAP "directory\\filename.bmp"    // Resource ID, type, and filename

Thank you.

于 2016-03-05T01:38:33.170 回答