Solved

How to load a JPEG image from resource file?

Posted on 1998-10-19
6
337 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
converting a pms to a cymk 13 25
Delphi cmd execution 6 40
Simple Delphi Question 9 77
Convert GUI app into console app for Win32 Env 5 58
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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Learn how to download your full Prezi presentation for offline presenting. Prezi doesn’t have to be viewed and shared in a web browser, even with a free account you can download your full presentation to share with others. Be sure to download any vi…
It’s easy to embed any of your public Prezi presentations on your website or social network to share with others. Learn how simple it is in this tutorial.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now