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

Saving images in the file...

Posted on 1998-11-26
17
175 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

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.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

829 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