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

x
?
Solved

Saving images in the file...

Posted on 1998-11-26
17
Medium Priority
?
180 Views
Last Modified: 2010-04-04
I have 44 JPEGImages in my form, is there anyway i can save
images in one file. So i can load them in that file to the 44 JPEGImages. I mean, i have 44 thumbnails in form and save them in the one file and load them at the file to the thumbnails.

mhieta
0
Comment
Question by:mhieta
  • 9
  • 8
17 Comments
 
LVL 20

Accepted Solution

by:
Madshi earned 20 total points
ID: 1348200
Hi mhieta,

I don't know the jpeg object, but surely it has saveToStream / loadFromStream methods, doesn't it?

procedure SaveJPegFiles(jpegs: array of TJpeg);
var fs    : TFileStream;
    ms    : TMemoryStream;
    i1,i2 : integer;
begin
  fs:=TFileStream('C:\Thumbnails.dat',fmCreate); ms:=TMemoryStream.Create;
  try
    i2:=high(jpegs);
    fs.Write(i2,4);
    for i1:=0 to high(jpegs) do begin
      ms.size:=0; jpegs[i1].SaveToStream(ms);
      i2:=ms.size;
      fs.Write(i2,4);
      fs.Write(ms.Memory^,i2);
    end;
  finally ms.Free; fs.Free end;
end;

Regards, Madshi.

P.S: Loading vice versa of course...
0
 
LVL 2

Author Comment

by:mhieta
ID: 1348201
Hi!
Yes, the TJPEGImage component has SaveToStream / LoadFromStream. I get your code few errors when i compiled it. Maybe i have spelling errors, i don't know. Where do i place this code. I placed my form two buttons save and load. I wanna do,
when i click save button i can change the directory where i want to save the thumbnails.dat. Vice versa load button too.

mhieta

0
 
LVL 20

Expert Comment

by:Madshi
ID: 1348202
mhieta,

you're right. I wrote this code out of my mind. I didn't test-compile it. There was a ".Create" missing...
This version compiles.
I think when the user clicks the save button, you should ask for the directory and then call this code.
Was this your question?

function SaveJPegFiles(file_: string; jpegs: array of TBitmap) : boolean;
var fs    : TFileStream;
    ms    : TMemoryStream;
    i1,i2 : integer;
begin
  result:=false;
  try
    fs:=TFileStream.Create(file_,fmCreate); ms:=TMemoryStream.Create;
    try
      i2:=high(jpegs);
      fs.Write(i2,4);
      for i1:=0 to high(jpegs) do begin
        ms.size:=0; jpegs[i1].SaveToStream(ms);
        i2:=ms.size;
        fs.Write(i2,4);
        fs.Write(ms.Memory^,i2);
      end;
    finally ms.Free; fs.Free end;
    result:=true;
  except end;
end;

Regards, Madshi.

0
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!

 
LVL 2

Author Comment

by:mhieta
ID: 1348203
Hi, Madshi

Yes this is the question. I have loaded the directory + thumb.dat to Label2.Caption. Can i replace the file_ this what is in the Label2.Caption, or what i have to do?. How i call this function?. And how i load this thumb.dat.

Regards, mhieta
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1348204
mhieta,

the save function simply overwrites existing files. If you don't like that you'll have to look if the file exists (FileExists) before calling the save function.
Load function comes in a few minutes.

Regards, Madshi.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1348205
mhieta,

here comes the load function. I hope you've Delphi4, because I've used Delphi4's dynamical arrays (I love them). If you don't have Delphi4, I hope you can change the code for Delphi3 yourself...   :-)

type TJPegBmps = array of TBitmap;

function LoadJPegFiles(file_: string) : TJPegBmps;
var fs    : TFileStream;
    ms    : TMemoryStream;
    i1,i2 : integer;
begin
  result:=nil;
  try
    fs:=TFileStream.Create(file_,fmOpenRead or fmShareDenyNone); ms:=TMemoryStream.Create;
    try
      fs.Read(i2,4);
      SetLength(result,i2);
      for i1:=0 to high(result) do begin
        fs.Read(i2,4);
        ms.size:=i2;
        fs.Read(ms.Memory^,i2);
        result[i1]:=TBitmap.Create;
        result[i1].LoadFromStream(fs);
      end;
    finally ms.Free; fs.Free end;
  except end;
end;

Regards, Madshi.
0
 
LVL 2

Author Comment

by:mhieta
ID: 1348206
Hi!, Madshi

Can you tell me, how do i call the SaveJPegFiles function. And how i can call the LoadJPegFiles function.

mhieta
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1348207
mhieta,

I will tell you, if you tell me:
(1) Do you have Delphi4?
(2) How are your 44 JPeg bitmaps defined? (Please write me the declaration of them - do you have an array of bitmaps or 44 single bitmaps...)

Regards, Madshi.
0
 
LVL 2

Author Comment

by:mhieta
ID: 1348208
Madshi,
(1) I have Delphi4
(2) I have 44 single JPEGImages

I have JPEGImage component.

mhieta
0
 
LVL 2

Author Comment

by:mhieta
ID: 1348209
Madshi,
(1) I have Delphi4
(2) I have 44 single JPEGImages

I have JPEGImage component.

mhieta
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1348210
Ok, do something like this:

var JPegs : array of TJPegImage;
procedure InitAllJPegs;
begin
  SetLength(JPegs,44);
  JPegs[0]:=JPegImage1;
  JPegs[1]:=JPegImage2;
  ...
  JPegs[43]:=JPegImage44;
end;

So now you can use e.g. JPegs[20] instead of JPegImage21. Are these images created at designtime or at runtime?
Perhaps you should create them dynamically:

var JPegs : array of TJPegImage;
procedure InitAllJPegs;
var i1 : integer;
begin
  SetLength(JPegs,44);
  for i1:=0 to 43 do
    JPegs[0]:=TJPegImage.Create;
end;

Then you could forget all this single JPegImageXX. Wouldn't that be better?

However, now the save call:

if not SaveJPegFiles('c:\test.dat',JPegs) then
  raise Exception.Create('Could not save JPegs...  :-(');

Or, the load call:

if not LoadJPegFiles('c:\test.dat',JPegs) then
  raise Exception.Create('Could not load JPegs...  :-(');

Then you have to change the load function like this:

function LoadJPegFiles(file_: string; var JPegs: array of TJPegs) : boolean;
var fs       : TFileStream;
    ms       : TMemoryStream;
    i1,i2,i3 : integer;
begin
  result:=false;
  try
    fs:=TFileStream.Create(file_,fmOpenRead or fmShareDenyNone); ms:=TMemoryStream.Create;
    try
      fs.Read(i3,4);
      for i1:=0 to i3 do begin
        fs.Read(i2,4);
        ms.size:=i2;
        fs.Read(ms.Memory^,i2);
        JPegs[i1].LoadFromStream(fs);
      end;
    finally ms.Free; fs.Free end;
    result:=true;
  except end;
end;

Regards, Madshi.
0
 
LVL 2

Author Comment

by:mhieta
ID: 1348211
Hi, Madshi

I still have problems, ok first of all when i save JPEGImages i get test.dat to c:\
test.dat but file size is 4kb(that isn't right, test.dat file is empty). And when i load JPEGImages, it probably should load jpegs into JPEGImage1, JPEGImage2, JPEGImage3..., but it doesn't load them at all.
I don't know may be i have missing something.

Regards, mhieta
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1348212
mhieta,

ok, please send me the complete unit with the definition of the JPegs and the load and save calls to "Mathias.Rauen@gmx.de".

Regards... Madshi.
0
 
LVL 2

Author Comment

by:mhieta
ID: 1348213
Madshi,

I have sended email for you did you got it. Email contains full unit.

Regards, mhieta
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1348214
mhieta,

not yet. When did you send it? Please check whether you've written the eMail address in the right way.

Regards... Madshi.
0
 
LVL 2

Author Comment

by:mhieta
ID: 1348215
I have sended the email about last week, i used hotmail service.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1348216
Hmmm. That's strange. Have you checked the eMail address? There MUST be a fault in it...
However, just send it again. Hope, it will work this time. I'll write a comment if I receive something from you.
0

Featured Post

Industry Leaders: 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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

877 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