A
A
Alexander Shilov2018-09-27 09:19:57
Delphi
Alexander Shilov, 2018-09-27 09:19:57

How to compress file size in Delphi?

I have an image that weighs 558 KB (screenshot)
With the help of the code, text is superimposed on the image and saves the image with text already with a size of 6.5 MB, how can I compress the image weight when saving?

procedure TForm1.Button1Click(Sender: TObject);
var r : integer;
    numberStr : string;
    SR : tSearchRec;
    jpg:TJpegimage;
begin
Memo1.Clear;
numberStr:=Memo2.Lines[0];
r := FindFirst(Edit1.Text+numberStr+'.jpg', faAnyFile, SR);
  if r = 0 then begin
    Memo1.Lines.Add('Screen : '+FormatDateTime('mm/dd/yyyy hh:nn:ss',
      FileAccessDateToDateTime(SR.FindData.ftCreationTime)));
  end;
jpg:=TJpegImage.Create;
jpg.LoadFromFile(Edit1.Text+numberStr+'.jpg');
with Image1 do
 begin
   canvas.font.Size:=26;
   canvas.Font.Color:=clRed;
   picture.Bitmap.Assign(jpg);
   canvas.Brush.Style:=bsClear;
   canvas.TextOut(10,20,Memo1.Lines[0]);
   picture.SaveToFile(Edit1.Text+numberStr+'(number).png');
 end;
 Memo2.Lines.Delete(0);
end;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Shilov, 2018-09-27
@tabbols95

As a result, I edited the kalapanga examples and achieved the result

jpg.Assign(Image1.Picture.Graphic);
     jpg.CompressionQuality := 100;
     jpg.Compress;
     jpg.SaveToFile(Edit1.Text+'\SnapShots(new)\'+numberStr+'(new).bmp');
     jpg.Free;

K
kalapanga, 2018-09-27
@kalapanga

TPicture knows nothing about png. Look at the file, you only have it with the png extension, and inside it is a regular bitmap. You need to go back to the corresponding class (TJPEGImage, TPNGImage):

jpgout := TJPEGImage.Create;
  try
    with jpgout do
    begin
      Assign(Image1.Picture.Bitmap);
      SaveToFile('myfile.jpg');
    end;
  finally
    jpgout.Free;
  end;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question