Solved

How to load a JPEG image from resource file?

Posted on 1998-10-19
6
355 Views
Last Modified: 2013-11-19
How to load a JPEG image from resource file?
0
Comment
Question by:kaloyan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 2

Expert Comment

by:Thaddy
ID: 1343348
Is the resource a custom resource? i.e JPEG internal,
or is the resource just a standard BMP?
0
 

Author Comment

by:kaloyan
ID: 1343349
It's a custom resource(I import it with J++).
0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1343350
This is from Borland groups. Hope this helps:


"...Michael Harings wrote in message <01bcdfd3$79145380$803919d0@dell100>...
                  >I'm making a screen saver, but don't want users to take the images. So I'm
                  >wondering how to store the jpg files so user's can't access them. Any ideas
                  >welcome. I originally thought I could store them in an image file like a
                  >bitmap, but after getting ImgLib found out otherwise. TIA!
                  >
                  Make a resource file like so:

                  <---- Cut ---->

                  SPLASHBACKGROUND JPEG "..\Images\Splash\Splash.jpg"

                  <---- Cut ---->

                  Save that as images.rc (or whatever)

                  Run brcc32 images.rc (in your Program Files\Borland\Delphi 3\Bin folder)

                  This will make images.res

                  Add the resource to a DLL or your main project like so:

                  {$R Images.Res}


                  Then use this function... If you are not using a DLL just pass in
                  Application.Handle
                  Pass in SPLASHBACKGROUND and then pass in Image1.Picture.

                  Procedure LoadJPEGFromRes(TheDLLHandle : LongInt; TheJPEG : String;
                  ThePicture : TPicture);
                  Var TmpResHandle : THandle;
                      TmpMemHandle : THandle;
                      TmpMemStream : TMemoryStream;
                      TmpResPtr : PByte;
                      TmpResSize : Longint;
                      TmpJPEGImage : TJPEGImage;
                  Begin
                   TmpResHandle := FindResource(TheDLLHandle, PChar(TheJPEG), 'JPEG');
                   TmpMemHandle := LoadResource(TheDLLHandle, TmpResHandle);
                   TmpResPtr := LockResource(TmpMemHandle);
                   TmpMemStream := TMemoryStream.Create;
                   TmpJPEGImage := TJPEGImage.Create;
                    Try
                     TmpResSize := SizeOfResource(TheDLLHandle, TmpResHandle);
                      TmpMemStream.SetSize(TmpResSize);
                      TmpMemStream.Write(TmpResPtr^, TmpResSize);
                      FreeResource(TmpMemHandle);
                      TmpMemStream.Seek(0, 0);
                      TmpJPEGImage.LoadFromStream(TmpMemStream);
                      ThePicture.Assign(TmpJPEGImage);
                    Finally
                     TmpJPEGImage.Free;
                     TmpMemStream.Free;
                    End;
                  End;


                   - Jeff..."
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Accepted Solution

by:
viktornet earned 30 total points
ID: 1343351
Here is how! There are five steps to that.... so just follow them :-)

STEP ONE:

Create a resource script file (*.RC) with a simple text editor like
Notepad and add the following line:

1  RCDATA  "MyPic.jpg"

The first entry is simply the index of the resource. The second
entry specifies that we are dealing with a user-defined resource.
The third and final entry is the name of the jpeg file.

STEP TWO:

Use Borland's Resource Compiler, BRCC32.EXE, to compile it
into a .RES file. At the MS-DOS command line:

BRCC32 MyPic.RC

This will create a resource file called MyPic.RES.

STEP THREE:

Add a compiler directive to the source code of your program. It
should immediately follow the form directive, as shown here:

{$R *.DFM}
{$R MyPic.RES}

STEP FOUR:

Add the following code to your project (I've created a procedure
for it):


procedure LoadJPEGfromEXE;

var
 MyJPG : TJPEGImage; // JPEG object
 ResStream : TResourceStream; // Resource Stream object

begin
  try
    MyJPG := TJPEGImage.Create;
    ResStream := TResourceStream.CreateFromID(HInstance, 1, RT_RCDATA);
    MyJPG.LoadFromStream(ResStream); // What!? Yes, that easy!
    Canvas.Draw(12,12,MyJPG); // draw it to see if it really worked!
  finally
    MyJPG.Free;
    ResStream.Free;
  end;
end;  // procedure

See the second parameter of the CreateFromID procedure of the
TResourceStream component? It's simply the resource index. You
can include more than one jpeg in your executable just by adding a
line for each jpeg (with a different index) in the resource script
(.RC) file.

STEP FIVE

Now just call the procedure anywhere in the program. Preferably in the OnCreate() of your form :-)

Regards,
Viktor Ivanov
0
 

Author Comment

by:kaloyan
ID: 1343352
Thanks!
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1343353
No problem :-)

Niama problemi :-)

Regards,
Viktor Ivanov
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
When i run adoquery my application freezes 26 211
RESTRequest Parameter 4 81
Broadcast a message using ICS 2 50
"Could not obtain OLE control window handle" under Delphi XE8 8 84
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Learn how to use the remote presentation tool in Prezi to allow you to harness the power of this cloud based presentation platform. You can show your presentation with a remote audience using this free tool.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question