Solved

Saving images in the file...

Posted on 1998-11-26
17
173 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

910 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

16 Experts available now in Live!

Get 1:1 Help Now