Hi Ali
Here is a quick example of how do do it:
procedure SaveToStreamAtMaxSize(Bitmap: TIEBitmap; Dest: TStream; MaxSizeBytes: Int64; Format: TIOFileType; JPEGQuality: Integer = 75; ResampleFilter: TResampleFilter = rfFastLinear);
const
Scale_Reduction_Per_Pass = 0.25; // Reduce size of image by 25% each pass
var
currScale: Double;
tempBmp: TIEBitmap;
begin
currScale := 1.0;
tempBmp := TIEBitmap.Create();
try
While True do
begin
Dest.Size := 0;
tempBmp.Assign( Bitmap );
if ( tempBmp.Width < 50 ) or ( tempBmp.Height < 50 ) then
raise Exception.create( 'Cannot resample further' );
tempBmp.Resample( currScale, ResampleFilter );
tempBmp.Params.JPEG_Quality := JPEGQuality;
tempBmp.SaveToStream( Dest, Format );
if Dest.Size <= MaxSizeBytes then
EXIT; // SUCCESS
if Dest.Size > MaxSizeBytes * 4 then
currScale := currScale * ( 1.0 / Sqrt(Dest.Size / MaxSizeBytes )) // Image is huge? Reduce more quickly
else
currScale := currScale * ( 1.0 - Scale_Reduction_Per_Pass ); // Image is close to size? Fine tuning...
end;
finally
tempBmp.Free();
end;
end;
procedure TForm1.Button6Click(Sender: TObject);
const
MAX_SIZE_BYTES = 150 * 1024;
var
bmp: TIEBitmap;
ms: TMemoryStream;
begin
bmp := TIEBitmap.Create();
ms := TMemoryStream.Create();
try
bmp.LoadFromFile( 'D:\Testing_Multimedia\Big_Big_Pix\570130.jpg' );
SaveToStreamAtMaxSize( bmp, ms, MAX_SIZE_BYTES, ioJPEG, 80 );
// Do something with ms, e.g. output to database blob field
// Test output
// ms.Position := 0;
// ImageEnView1.IO.LoadFromStream( ms );
finally
ms.Free();
bmp.Free();
end;
end;
Nigel
Xequte Software
www.imageen.com