Solved

Saving images in the file...

Posted on 1998-11-26
17
172 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Author Comment

by:mhieta
Comment Utility
Madshi,
(1) I have Delphi4
(2) I have 44 single JPEGImages

I have JPEGImage component.

mhieta
0
 
LVL 2

Author Comment

by:mhieta
Comment Utility
Madshi,
(1) I have Delphi4
(2) I have 44 single JPEGImages

I have JPEGImage component.

mhieta
0
 
LVL 20

Expert Comment

by:Madshi
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Madshi,

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

Regards, mhieta
0
 
LVL 20

Expert Comment

by:Madshi
Comment Utility
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
Comment Utility
I have sended the email about last week, i used hotmail service.
0
 
LVL 20

Expert Comment

by:Madshi
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

10 Experts available now in Live!

Get 1:1 Help Now