procedure CompressImageSpecifiedSize(Bmp: Graphics.TBitmap; AWidth, AHeight: Integer;
APixelFormat: TPixelFormat);
var
TempBmp: Graphics.TBitmap;
AX: Real;
AY: Real;
Compress: Real;
begin
{$ifdef ControlStack}
try
{$endif ControlStack}
TempBmp := Graphics.TBitmap.Create;
TempBmp.PixelFormat := APixelFormat;
TempBmp.Canvas.Lock;
AX := Bmp.Width / AWidth;
AY := Bmp.Height / AHeight;
if AX > AY then
Compress := AX
else
Compress := AY;
if Compress > 1 then
begin
TempBmp.Width := Max(1, Round(Bmp.Width / Compress));
TempBmp.Height := Max(1, Round(Bmp.Height / Compress));
end
else
begin
TempBmp.Width := Bmp.Width;
TempBmp.Height := Bmp.Height;
end;
SetStretchBltMode(TempBmp.Canvas.Handle, HALFTONE);
SetBrushOrgEx(TempBmp.Canvas.Handle, 0, 0, nil);
StretchBlt(TempBmp.Canvas.Handle, 0, 0, TempBmp.Width, TempBmp.Height,
Bmp.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height, SRCCOPY);
Bmp.Width := TempBmp.Width;
Bmp.Height := TempBmp.Height;
Bmp.Canvas.Draw(0, 0, TempBmp);
Bmp.PixelFormat := APixelFormat;
TempBmp.Canvas.Lock;
TempBmp.Free;
{$ifdef ControlStack}
except
on E: Exception do
begin
ReRaiseException(E, 1547);
raise;
end;
end;
{$endif ControlStack}
end;