Solved

Saving images in the file...

Posted on 1998-11-26
17
174 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 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