Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: TImage Picture Size



Permlink Replies: 7 - Last Post: Aug 31, 2017 11:47 PM Last Post By: Adalberto Baldini Threads: [ Previous | Next ]
Adalberto Baldini

Posts: 139
Registered: 1/31/12
TImage Picture Size
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 25, 2017 11:36 PM
Delphi® XE5 Version 19.0.14356.6604

I load a Jpeg image into TImage.Picture using different compression quality.
I want to compare size of image of different comprassion rate.
How can I know the picture size ?

Adalberto Baldini
Peter Below

Posts: 1,227
Registered: 12/16/99
Re: TImage Picture Size
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 26, 2017 1:24 AM   in response to: Adalberto Baldini in response to: Adalberto Baldini
Adalberto Baldini wrote:

Delphi® XE5 Version 19.0.14356.6604

I load a Jpeg image into TImage.Picture using different compression
quality. I want to compare size of image of different comprassion
rate. How can I know the picture size ?

Adalberto Baldini

Try to use picture.savetostream to save the image to a TMemoryStream,
then check the Size property of the stream.

--
Peter Below
TeamB
Adalberto Baldini

Posts: 139
Registered: 1/31/12
Re: TImage Picture Size
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 28, 2017 12:11 AM   in response to: Peter Below in response to: Peter Below
With this code I don't get any reduction of picture size :

var
f: file of Byte;
size: Longint;
compression : Extended;
dimension, rate : int64;
JpegImage: TJPEGImage;
JpegStream : TMemoryStream;
begin
if wRdGrp_Filesize.ItemIndex = 0 then rate := 500000 else rate := 1000000;
AssignFile(f, OpenDlg.FileName);
Reset(f);
try
size := FileSize(f);
finally
CloseFile(f);
end;
JpegImage := TJPEGImage.Create;
JpegStream := TMemoryStream.Create;
if size <= rate then compression := 100 else Compression:= int (100 / int(size / rate)) ;
JpegImage.CompressionQuality := StrToInt(FloattoStr(compression));
JpegImage.LoadFromFile(OpenDlg.FileName);
JpegImage.SavetoStream(JpegStream);
dimension := JpegStream.Size;

I load an image from file its size is 5644191, then comprassion rate = 9 , dimension = 5644191

No changes,. Whats wrong ?

Thanks
Adalberto Baldini
Jeff Overcash (...

Posts: 1,529
Registered: 9/23/99
Re: TImage Picture Size
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 28, 2017 12:30 AM   in response to: Adalberto Baldini in response to: Adalberto Baldini
Adalberto Baldini wrote:
With this code I don't get any reduction of picture size :

var
f: file of Byte;
size: Longint;
compression : Extended;
dimension, rate : int64;
JpegImage: TJPEGImage;
JpegStream : TMemoryStream;
begin
if wRdGrp_Filesize.ItemIndex = 0 then rate := 500000 else rate := 1000000;
AssignFile(f, OpenDlg.FileName);
Reset(f);
try
size := FileSize(f);
finally
CloseFile(f);
end;
JpegImage := TJPEGImage.Create;
JpegStream := TMemoryStream.Create;
if size <= rate then compression := 100 else Compression:= int (100 / int(size / rate)) ;
JpegImage.CompressionQuality := StrToInt(FloattoStr(compression));
JpegImage.LoadFromFile(OpenDlg.FileName);
JpegImage.SavetoStream(JpegStream);
dimension := JpegStream.Size;

I load an image from file its size is 5644191, then comprassion rate = 9 , dimension = 5644191

No changes,. Whats wrong ?

Thanks
Adalberto Baldini

You aren't calling Compress after setting the CompressionSize.

From the help

"Call Compress to compress for optimization, such as before streaming out when
assigning a bitmap to a jpeg. Changing properties like CompressionQuality does
not force compression. To force compression, call Compress"

--
Jeff Overcash (TeamB)
(Please do not email me directly unless asked. Thank You)
Learning is finding out what you already know. Doing is demonstrating that you
know it. Teaching is reminding others that they know it as well as you. We are
all leaners, doers, teachers. (R Bach)

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: TImage Picture Size
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 28, 2017 11:31 AM   in response to: Adalberto Baldini in response to: Adalberto Baldini
Adalberto Baldini wrote:

With this code I don't get any reduction of picture size :

That is because you are not actually re-compressing the data. JPEG is
a lossy format, any change in size while loading and resaving the data
as-is will just be due to lost image quality. To actually re-compress
the data, you have to call the TJPEGImage.Compress() method before
saving the data, eg:

var
  f: TFileStream;
  compression : Extended;
  size, dimension, rate : Int64;
  JpegImage: TJPEGImage;
  JpegStream : TMemoryStream;
begin
  if wRdGrp_Filesize.ItemIndex = 0 then
    rate := 500000
  else
    rate := 1000000;
  JpegImage := TJPEGImage.Create;
  try
    f := TFileStream.Create(OpenDlg.FileName, fmOpenRead or
fmShareDenyWrite);
    try
      size := f.Size;
      JpegImage.LoadFromStream(f);
    finally
      f.Free;
    end;
    JpegStream := TMemoryStream.Create;
    try
      if size <= rate then
        compression := 100
      else
        Compression := int(100 / int(size / rate));
      JpegImage.CompressionQuality := Trunc(compression);
      JpegImage.Compress;
      JpegImage.SaveToStream(JpegStream);
      dimension := JpegStream.Size;
    finally
      JpegStream.Free;
    end;
  finally
    JpegImage.Free;
  end;
end;


--
Remy Lebeau (TeamB)
Adalberto Baldini

Posts: 139
Registered: 1/31/12
Re: TImage Picture Size
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 30, 2017 11:33 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Hi Remy,
I followed your code but it seems that Compress looses data.
I debugged code and when JpegImage is created it has a compression rate = 90
I didn't change it and executed below steps, with this results :
JpegImage..Empty = False
dimension = 0 ,
and continuing I raise Jpeg error #42

Adalberto Baldini

JpegImage.CompressionQuality := Trunc(compression);
JpegImage.Compress;
JpegImage.SaveToStream(JpegStream);
dimension := JpegStream.Size;
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: TImage Picture Size
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 31, 2017 10:11 AM   in response to: Adalberto Baldini in response to: Adalberto Baldini
Adalberto Baldini wrote:

I followed your code but it seems that Compress looses data.

As it should be, because JPG is a lossy format. Every time you
re-save/re-compress the image, data is lost. If you don't want that,
use a lossless format, like GIF or PNG.

--
Remy Lebeau (TeamB)
Adalberto Baldini

Posts: 139
Registered: 1/31/12
Re: TImage Picture Size
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 31, 2017 11:47 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Before to compress, image need to be decompressed , adding

JpegImage.DIBNeeded; // decompress

it works.

Thanks.
Adalberto
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02