JPEG Bilder als Resource ins Programm einbinden

  • *.rh file erstellen
// resource.rh
#define ID_JPEG 1000
  • *.rc file erzeugen, in dem JPEG als RCDATA resource type steht
#include "resource.rh"

  • *.rc file zum Projekt hinzufügen
  • TImage irgenwo auf der Form plazieren und ‚MyImage‘ nennen
  • Code um Image aus der Resource zu laden
#include <jpeg.hpp>
#include "main.h"
#include "resource.rh"
#include <memory>

void __fastcall TForm1::Button1Click(TObject *Sender)
    // Find the resource using the resource ID from resource.rh
    HRSRC rsrc = FindResource(HInstance, MAKEINTRESOURCE(ID_JPEG),RT_RCDATA);
    if (!rsrc) return;

    // Load the resource and save its total size. We will need the size
    // when we read the data.
    DWORD Size = SizeofResource(HInstance , rsrc);
    HGLOBAL MemoryHandle = LoadResource(HInstance,rsrc);
    if (MemoryHandle == NULL) return;

    // We need to get the bytes out of the resource somehow.
    // The API function LockResource allows us to do that.
    // It returns a BYTE pointer to the raw data in the resource
    // In this case, the resource is a .JPG file.
    BYTE *MemPtr = (BYTE *)LockResource(MemoryHandle);

    // Now the jpeg image is memory is in MemPtr.
    // Copy from MemPtr into a TMemoryStream
    std::auto_ptr<TMemoryStream->stream(new TMemoryStream);
    stream->Write(MemPtr, Size);
    stream->Position = 0;

    // Create a TJPEGImage, and tell it to load from the memory stream.
    std::auto_ptr<TJPEGImage-> JImage(new TJPEGImage());

    // Now that we have a TJEGImage, we can use Assign to
    // copy that image into a TImage.
    MyImage->Width = JImage->Width;
    MyImage->Height = JImage->Height;

Schreibe einen Kommentar