Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Load a photo inside the code

Status
Not open for further replies.

pierrotsc

Programmer
Nov 25, 2007
358
US
Let's see if i can express myself. I have a editor where you can use the opendialog command and load a image file. I would like to have 3 image files built-in into the program. I do not want to give these files but i want when a user click on a button, the file to load from the code and not from the HD. I hope that makes sense.
Can it be done.
The file is big, around 500k.
Thanks.
PO
 
Sure you can. Of course, it depends on the image file and what it can support. Let's assume BMP.

Create a file with a RC extension...what it needs to look like is as follows for a bitmap or an extra icon (resource id, resource type, resource location):
Code:
Icon1 ICON icon1.ico
Bitmap1 BITMAP bitmap1.bmp

I'm sure there are some other codes which might catch other kinds of image files.

Then use BRCC32 to make a .RES file out of it.

Code:
brcc32 iconres.rc

Then in your main code, you put a line like:
Code:
{$R ICONres.RES}

To use said resources, you use Load??? where the ??? is whatever specific resource is coded and reference the resource tag in the RC file.

Code:
var
  Icon : HIcon;

Icon := LoadIcon(h, 'Icon1');
DrawIcon(Canvas.Handle, 10, 10, Icon);

Lots of help gotten from this site:

Of course, if you put 500KB of compressible data into the EXE it might be preferential to compress it with something like UPX, getting the same advantage as a ZIP file in such cases.
 
Thanks a bunch. Does it have to be a bmp file? Could I use a Jpeg? The bmp is too big.
I never heard of UPX. I'll try to google it. Could I compress my bmp?
PO
 
Does it have to be a bmp file? Could I use a Jpeg?

Aaron's example shows how to embed any kind of file you want into an EXE. There are many other types that you can imbed, and ways you can make use of them, too, which are illustrated on that site I linked to earlier. You can display/use those files within the program if the procedure/object supports pointing to a instance of it.

For example, if you are creating database files with your program, you can embed a empty database file in your program and write it to disk using TResourceStream when the "new file" option is selected.

I never heard of UPX. I'll try to google it. Could I compress my bmp?

UPX is an executable packer. In other words, it compresses the data in the exe/dll/whatever in such a way that it still works seamlessly. There's extra overhead involved in decompressing it to run it, but it's negligible on most systems.

I mentioned it as an option because the resource system places files in their uncompressed states in the executable and it could help to keep the size of the exe down and hinder extraction of the resources (there are programs that do that) from them. Download here:
 
I think i may not be able to do it. The photo size is 2400x3600 pixels. That may be too big. The image editor in D7 has a limit of 1024 pixels.
Thanks for your input.
PO
 
I think i may not be able to do it. The photo size is 2400x3600 pixels. That may be too big. The image editor in D7 has a limit of 1024 pixels.

You'll be able to do it, don't let the arbitrary limit of the cheesy (it really is pretty bad) Delphi image editor stop you. If Windows can handle the file, TBitmap (or whatever else) should be able to as well.

Just for fun I loaded a 6.8MB bitmap file (1950x1200 or some such thing) into an EXE and showed it full screen. No problems.
 
I'm able to load a hige image into a timage component. Tht's ok. I'm trying to make it part of the form. The form cannot ger that big. How would i insert the graphic and have sliders on each side to scroll and see the whole picture? Is that possible?
I'm using timage32 from the gr32 project. When i right-click the timage box, i get the editor. I can load the image but i need scrollbars on each side to see the whole picture. Can it be done this way or another way?
Sorry to be a pain..
PO
 
No pain, we're all trying to understand and learn at various points. As for this matter...

How would i insert the graphic and have sliders on each side to scroll and see the whole picture? Is that possible?

We're not on an image problem now, but a form problem. A lot depends on how you are wanting to do it. It can be done as you describe or you can scale the image to fit the screen. The issue here is form variable manipulations, not images. We'll go with your scenario. There's a TBitmap, TImage (at form coord 0,0), and the form involved here.

To set the scroll bars of the form so you can view the entire image placed in TImage.
Code:
HorzScrollBar.Visible := true;
HorzScrollBar.Range := Bitmap.Width;
VertScrollBar.Visible := true;
VertScrollBar.Range := Bitmap.Height;

Of course, there's many other things to do with the form depending on how you are wanting to display the image (including complete full-screen, scaled or not scaled, etc). But this should get the basics.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top