Recent

Author Topic: [SOLVED] Setting Compression Level  (Read 7235 times)

eddyg30

  • Newbie
  • Posts: 3
[SOLVED] Setting Compression Level
« on: May 26, 2012, 03:41:48 am »
Anybody knows how to set the compression level to be used in tbgrabitmap.savetofile? I need to set it when saving my edited bitmaps into jpg or png files.

Thanks,
« Last Edit: May 26, 2012, 07:03:45 pm by eddyg30 »

circular

  • Hero Member
  • *****
  • Posts: 4220
    • Personal webpage
Re: Setting Compression Level
« Reply #1 on: May 26, 2012, 05:22:49 pm »
You need to create a writer. For Jpeg, use the FPWriteJpeg unit, and do
Code: [Select]
var writer : TFPWriterJPEG;
begin
  writer := TFPWriterJPEG.Create;
  writer.CompressionQuality:= 95; //any quality here
  bitmap.SaveToFile(filename, writer);
  writer.Free;
end;

For Png file, it is not a lossy compression, so there is nothing to be set.
Conscience is the debugger of the mind

eddyg30

  • Newbie
  • Posts: 3
Re: Setting Compression Level
« Reply #2 on: May 26, 2012, 06:17:47 pm »
Thanks.. this is just what i need. Tried to trace the sources awhile ago and got confused in these writer things, it may take days for me to understand and bring much more questions without your help. Thanks again!

eddyg30

  • Newbie
  • Posts: 3
Re: Setting Compression Level
« Reply #3 on: May 26, 2012, 06:55:24 pm »
By the way, i got an error message:
Unit1.pas(550,79) Error: Wrong number of parameters specified for call to "SaveToFile"

for:
Code: [Select]
bmp2.SaveToFile(self.cachedir1 + '\' + inttostr(cache1)+'.jpg', writer1);

I 'solved' it though by deleting all "SaveToFile(const filename: string)" declarations in TBGRACustomBitmap and TBGRADefaultBitmap classes, leaving only the "SaveToFile(const filename: string; Handler:TFPCustomImageWriter)" ones. A bug or something?
« Last Edit: May 26, 2012, 06:57:26 pm by eddyg30 »

circular

  • Hero Member
  • *****
  • Posts: 4220
    • Personal webpage
Re: [SOLVED] Setting Compression Level
« Reply #4 on: May 26, 2012, 08:47:57 pm »
Oh I see. What if you just add "overload;" at the end of the line defining SaveToFile(const filename: string) ?
Conscience is the debugger of the mind

Shebuka

  • Sr. Member
  • ****
  • Posts: 427
Re: [SOLVED] Setting Compression Level
« Reply #5 on: May 28, 2012, 10:35:07 am »
you can also use this (using Graphics unit):

Code: [Select]
var
  MyBitmap: TBitmap;
  WrkJpg: TJpegImage;   
begin
      WrkJpg := TJpegImage.Create;
      try
        WrkJpg.CompressionQuality := 80;
        WrkJpg.Assign(MyBitmap);
        WrkJpg.SaveToFile(Utf8ToSys(TheFileName));
      finally
        FreeAndNil(WrkJpg);
      end;

also http://wiki.lazarus.freepascal.org/Developing_with_Graphics#Image_formats
« Last Edit: May 28, 2012, 10:37:43 am by Shebuka »

Gintas

  • Jr. Member
  • **
  • Posts: 71
    • Developer's Diary
Re: [SOLVED] Setting Compression Level
« Reply #6 on: July 11, 2013, 04:04:13 pm »


PNG has a compression options like Maximum or Fastest,but it saves just a few bytes.
You can assing TBGRABitmap to TBGRACustomBitmap and save it using TFPWriter.JPEG.

 

TinyPortal © 2005-2018