JPEG Bilder als Resource ins Programm einbinden

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

ID_JPEG RCDATA "conf99.JPG"
  • *.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());
    JImage->LoadFromStream(stream.get());

    // 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;
    MyImage->Picture->Assign(JImage.get());
}