uses Jpeg;
function RecompressImage(FileJPEG: string; maxSize, inc: integer): boolean;
var
MyJPEG: TJPEGImage;
MyBMP: TBitmap;
i,s: integer;
begin
result:=False;
i:=100;
MyJPEG:=TJPEGImage.Create;
try
MyJPEG.LoadFromFile(FileJPEG);
repeat
MyBMP:=TBitmap.Create;
try
MyBMP.Height:=MyJPEG.Height;
MyBMP.Width:=MyJPEG.Width;
MyBMP.Canvas.Draw(0,0,MyJPEG);
MyJPEG.Assign(MyBMP);
dec(i,inc);
if i<=0 then
exit;
MyJPEG.CompressionQuality:=i;
MyJPEG.Compress;
MyJPEG.SaveToFile(FileJPEG+'r');
s:=GetFileSize(FileJPEG+'r');//,nil);
finally
MyBMP.Free;
end;
until (s<MaxSize);
finally
MyJPEG.Free;
end;
DeleteFile(FileJPEG);
MoveFile(PChar(FileJPEG+'r'),PChar(FileJPEG));
result:=True;
end;
|