Question
How do I resize A *. jpg or *. GIF image from say 640 × 480 to 50 × 50 and then save the image as a new one?
Procedure tform1.button1click (Sender: tobject );
VaR
BMP: tbitmap;
JPG: tsf-image;
Scale: Double;
Begin
If opendialog1.execute then
Begin
JPG: = tsf-image. Create;
Try
JPG. loadfromfile (opendialog1.filename );
If JPG. Height> JPG. Width then
Scale: = 50/jpg. Height
Else
Scale: = 50/jpg. width;
BMP: = tbitmap. Create;
Try
{Create thumbnail bitmap, keep pictures Aspect Ratio}
BMP. Width: = round (JPG. Width * scale );
BMP. Height: = round (JPG. Height * scale );
BMP. Canvas. stretchdraw (BMP. Canvas. cliprect, JPG );
{Draw thumbnail as control}
Self. Canvas. Draw (100, 10, BMP );
{Convert back to JPEG and save to file}
JPG. Assign (BMP );
JPG. savetofile (
Changefileext (opendialog1.filename, '_ thumb. jpg ')
);
Finally
BMP. Free;
End;
Finally
JPG. Free;
End;
End;
End;
Note: To test this code you need to drop a tbutton and a topendialog on a form and add the JPEG unit to the uses statement.