Search code examples
c++.netwinformsmonoclr

Load image from Resource Files of a c++ CLR/CLI project


My project is a c++ CLR/CLI, I have some PNG images in my Resource Files and I want to change the BackgroundImage of a System::Windows::Forms::Button when the button is clicked, my code below:

#include <Windows.h>
#include "resource.h"

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    HBITMAP hBitMap = (HBITMAP) LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_PNG2), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    Bitmap^  bitMap = Bitmap::FromHbitmap((IntPtr) hBitMap);
    DeleteObject(hBitMap);
    button1->BackgroundImage = (System::Drawing::Image^) bitMap;
}

Here's my resource.h:

//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by Disneyy.rc
//
#define IDB_PNG1                        101
#define IDB_PNG2                        102
#define IDB_PNG3                        103

// Next default values for new objects
// 
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        104
#define _APS_NEXT_COMMAND_VALUE         40001
#define _APS_NEXT_CONTROL_VALUE         1001
#define _APS_NEXT_SYMED_VALUE           101
#endif
#endif

When compiling I'm getting this error:

1>MyForm.obj : error LNK2028: unresolved token (0A000036) "extern "C" void * __stdcall LoadImageW(struct HINSTANCE__ *,wchar_t const *,unsigned int,int,int,unsigned int)" (?LoadImageW@@$$J224YGPAXPAUHINSTANCE__@@PB_WIHHI@Z) referenced in function "private: void __clrcall Disneyy::MyForm::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click@MyForm@Disneyy@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
1>MyForm.obj : error LNK2028: unresolved token (0A00003F) "extern "C" int __stdcall DeleteObject(void *)" (?DeleteObject@@$$J14YGHPAX@Z) referenced in function "private: void __clrcall Disneyy::MyForm::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click@MyForm@Disneyy@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
1>MyForm.obj : error LNK2019: unresolved external symbol "extern "C" void * __stdcall LoadImageW(struct HINSTANCE__ *,wchar_t const *,unsigned int,int,int,unsigned int)" (?LoadImageW@@$$J224YGPAXPAUHINSTANCE__@@PB_WIHHI@Z) referenced in function "private: void __clrcall Disneyy::MyForm::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click@MyForm@Disneyy@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
1>MyForm.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall DeleteObject(void *)" (?DeleteObject@@$$J14YGHPAX@Z) referenced in function "private: void __clrcall Disneyy::MyForm::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click@MyForm@Disneyy@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)

EDIT: Ok I fixed it by adding User32.lib to Additional Dependencies also specifying the Entry Point as main. However there's something wrong with this line:

HBITMAP hBitMap = (HBITMAP) LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_PNG2), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

Now I'm getting this error:

First-chance exception at 0x77130c3f in Disneyy.exe: 0xC0000005: Access violation reading location 0x00000066.
A first chance exception of type 'System.AccessViolationException' occurred in Disneyy.exe
An unhandled exception of type 'System.AccessViolationException' occurred in Disneyy.exe
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

EDIT: Once again I fixed it by changing IMAGE_BITMAP to BI_PNG, but now a new error comes up at line: Bitmap^ bitMap = Bitmap::FromHbitmap((IntPtr) hBitMap);

A first chance exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll
An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll
Additional information: A generic error occurred in GDI+.

Solution

  • Finally after hours and hours of research I found an adapted solution which works just fine!

    This solution is a combination of Giuseppe Pischedda's answer: https://www.codeproject.com/Articles/17996/Load-JPEG-images-from-DLL-with-LoadResource-in-Man

    and Bordon's answer: https://social.msdn.microsoft.com/Forums/de-DE/c8dea6f9-0564-49d6-9782-117207031140/loadfromresource-fails-for-loading-png-files-from-resource?forum=vcmfcatl

    adapted by me for PNG images:

    public: System::Drawing::Image^  getImageFromRes(long resource_ID) {
        // Function getImageFromRes
        // A function for loading PNG images from resources in C++ CLR/CLI
        // Copyright (C) Giuseppe Pischedda 2007 for most code
        // and a little part of this code by Bordon and adapted by me for PNG images in C++ CLR/CLI.
    
        //Load the resource module:
        HMODULE hInst = NULL;
    
        // Find the resource using the resource ID from file "resource.h"
        HRSRC hResource = ::FindResource(hInst, MAKEINTRESOURCE(resource_ID), L"PNG");
        if (!hResource) return nullptr;
    
        // Load the resource and save the total size.
        DWORD Size = SizeofResource(hInst, hResource);
        HGLOBAL MemoryHandle = LoadResource(hInst, hResource);
        if (MemoryHandle == NULL) return nullptr;
    
        //Create a cli::array of byte with size = total size + 2
        cli::array<BYTE>^  MemPtr = gcnew array<BYTE>(Size + 2);
    
        //Cast from LPVOID to char *
        char *lkr = (char *) (LockResource(MemoryHandle));
    
        //Copy from unmanaged memory to managed array
        System::Runtime::InteropServices::Marshal::Copy((IntPtr) lkr, MemPtr, 0, Size);
    
        // Create a new MemoryStream with size = MemPtr
        System::IO::MemoryStream^  stream = gcnew System::IO::MemoryStream(MemPtr);
    
        //Write in the MemoryStream
        stream->Write(MemPtr, 0, Size);
    
        //Set the position for read the stream
        stream->Position = 0;
    
        //Free allocated resources
        FreeLibrary(hInst);
    
        //Create an Image abstract class pointer
        System::Drawing::Image^  ptrPNG;
    
        //Assign the stream to abstract class pointer
        ptrPNG = System::Drawing::Image::FromStream(stream);
        return ptrPNG;
    }
    

    Usage:

    System::Drawing::Image^  image = getImageFromRes(IDB_PNG2);
    if (image != nullptr) button1->BackgroundImage = image;