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
Solved

How to load a JPEG image from resource file?

Posted on 1998-10-19
6
347 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi: Connect to running MS Outlook 4 117
RESTRequest Parameter 4 41
Multi-layered image in FireMonkey 9 48
How to save the image in the .cds File ClientDataSet? 1 22
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Keep your audience engaged and get the most out of your next presentation with these quick Prezi tips.
In this tutorial viewers will learn how to create blended and gradiated shapes in Illustrator using the blend tool Draw two shapes, one of them in a different color: Select both and create a blend by going to Object > Blend > Make: Blends can also b…
Learn how to set up basic frames and paths in Prezi and understand the open space that Prezi allows you to create presentations in.

790 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