Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to load a JPEG image from resource file?

Posted on 1998-10-19
6
Medium Priority
?
381 Views
Last Modified: 2013-11-19
How to load a JPEG image from resource file?
0
Comment
Question by:kaloyan
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 10

Accepted Solution

by:
viktornet earned 120 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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 set up basic frames and paths in Prezi and understand the open space that Prezi allows you to create presentations in.
Suggested Courses

916 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