Hi Farzan,
Here's my ResizeJPG function that may help you. It takes a JPG image,
converts it to a Bitmap (to do the resizing) then converts it back to a JPG.
The resizing 'Scale' should be passed as a percentage of the original image
(e.g. 50 will halve the size of the original etc.). Note too, that this
function only reduces the size of the image. To increase the size we'd have
to do some super-sampling and the results are generally crap when you try to
increase an image.
function ResizeJPG(JPGName, JPGSaveAs: string; JPGQual: TJPEGQualityRange;
Scale: Double; AddOverlay: boolean; var NewSize: TNewSize): boolean;
var
jpg: TJPEGImage;
PBitmap: TBitmap;
ARect: TRect;
begin
Result := FALSE;
if not FileExists(JPGName) or
(Scale >= 1)or
(Scale <= 0) then
Exit;
// Convert JPG to BMP
PBitmap := TBitmap.Create;
try
jpg := TJPEGImage.Create;
try
jpg.LoadFromFile(JPGName);
with PBitmap do
begin
PixelFormat := pf24bit;
// Resize the BMP image
Height := Trunc(jpg.Height * Scale);
Width := Trunc(jpg.Width * Scale);
ARect.Left := 0;
ARect.Top := 0;
ARect.Right := Trunc((jpg.Width + 1) * Scale);
ARect.Bottom := Trunc((jpg.Height + 1) * Scale);
NewSize.Height := ARect.Bottom;
NewSize.Width := ARect.Right;
// Draw the JPG on the re-sized BMP image
Canvas.StretchDraw(ARect, jpg);
end;
finally
jpg.Free;
end;
// Convert the resized BMP back to a JPG and save it
jpg := TJPEGImage.Create;
try
jpg.CompressionQuality := JPGQual;
jpg.Assign(PBitmap);
jpg.SaveToFile(JPGSaveAs);
if AddOverlay then
begin
AddTextOverlay(JPGSaveAs,JPGSaveAs);
end;
Result := FileExists(JPGSaveAs);
finally
jpg.Free;
end;
finally
PBitmap.Free;
end;
end;
HTH
Mike
----- Original Message -----
From: "farzan_beh" <f-***@araku.ac.ir>
To: <delphi-***@yahoogroups.com>
Sent: Thursday, September 05, 2002 12:50 AM
Subject: Re: [Delphi] Resizing Image - Again
Post by farzan_behThank you Stephen for your invaluable help; It worked properly.
But I didnt get about "resize the image proportionately". As you mentioned it
doesnt have a very good look. Would you explain following part more?
Post by Stephen WoodJust remember to resize the image proportionately or else it won't look too
good...
Thank you ver much;
Farzan Behzadian.
Post by Stephen WoodWell the very easiest way is to dump the image into a TImage, set the
StretchDraw property to true, resize your image and save it again...
otherwise you can do it manually with a TCanvas.StretchDraw method...
Just remember to resize the image proportionately or else it won't look too
good...
HTH
http://www.delphicollection.com/public/Digests/index.shtml
---------------------------------------------------------------
---------------------------------------------------------------
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/