We help IT Professionals succeed at work.

res file problem

hugh34
hugh34 asked
on
i have the following in a file called "pic.rc"

PIC1 PICTURE"Start.bmp"
PIC2 PICTURE"Starta.bmp"
PIC3 PICTURE"Startb.bmp"
PIC4 PICTURE"Startc.bmp"


i have compiled it ok and have got my res file called
"pic.rc.RES"

now the problem is how can i call one of these pics from the resource file into image1 on a buttonclick in my delphi application?

i have included the following, but i need the actual code to get the pic into the image1!!!

implementation

{$R *.DFM}
{$R pic.rc.RES}

PLEASE can someone help A.S.A.P
Comment
Watch Question

Commented:
Just made up this, hope it work:

Procedure TForm1.LoadPicFromRes(TheJPEG: String; ThePicture: TPicture);
Var
  ResHandle, MemHandle: THandle;
  MemStream: TMemoryStream;
  ResPtr: PByte;
  ResSize: Longint;
  FBit:Tbitmap;
Begin
  ResHandle := FindResource(hInstance, PChar(TheJPEG), 'RT_BITMAP');
  MemHandle := LoadResource(hInstance, ResHandle);
  ResPtr := LockResource(MemHandle);
  MemStream := TMemoryStream.Create;
  FBit := Tbitmap.Create;
  ResSize := SizeOfResource(hInstance, ResHandle);
  MemStream.SetSize(ResSize);
  MemStream.Write(ResPtr^, ResSize);
  FreeResource(MemHandle);
  MemStream.Seek(0, 0);
  FBit.LoadFromStream(MemStream);
  ThePicture.Assign(FBit);
  FBit.Free;
  MemStream.Free;
End;

//SAmple call:
LoadPicFromRes(PIC1, Image1.Picture);


Dennis

Author

Commented:
this code is full of compile errors!

Commented:
ups sorry, this is easyer:

procedure TfrMain.btnCanvasPic(Sender: TObject);
var bBitmap : TBitmap;
begin
 bBitmap := TBitmap.Create;
 try
  bBitmap.Handle := LoadBitmap(hInstance, 'ATHENA');
  Image1.Width := bBitmap.Width;
  Image1.Height := bBitmap.Height;
  Image1.Canvas.Draw(0,0,bBitmap);
 finally
  bBitmap.Free;
 end;
end;

//Or

procedure TfrMain.btnLoadPicClick(Sender: TObject);
begin
 Image1.Picture.Bitmap.
   LoadFromResourceName(hInstance,'EARTH');
end;


Dennis
Commented:
Hugh,
  To start with, you have a number of issues with the resource file. First off, you should have something like this:

PIC1 BITMAP Start.bmp
PIC2 BITMAP Starta.bmp
..etc,etc..

When you compile this, your resulting resource file would be Pic.RES not "Pic.rc.res"

Next in your code:

{$resource Pic.res}

Image1.Picture.Bitmap.LoadFromResourceName(hInstance,'PIC1');

OR


Image1.Picture.Bitmap.
  LoadFromResourceName(hInstance,'PIC2');


etc,etc...


Good luck!!

Commented:
listening :-)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.