Mega Code Archive

 
Categories / Delphi / Graphic
 

Change the size of an JPEG image and save it

Title: Change the size of an JPEG image and save it Question: How to change the size (width and heigth) of an JPEG file and save it with the changed size? Answer: [(time: Few hours later) Update: Now the image will not be stretched out, the width and height scale stays the same :)] - Add the JPEG unit to your uses - Add a OpenDialog and SaveDialog to your form Use this code to chnage the size of a JPEG image: var jpegImage: TJPegImage; bitmap: TBitmap; begin jpegImage := TJPegImage.create; OpenDialog1.Execute; jpegImage.loadFromFile( OpenDialog1.FileName ); // We create a bitmap with the right dimensions bitmap := TBitmap.create; bitmap.width := 200; bitmap.height := round(jpegImage.Height/(jpegImage.Width/200)); // We draw the jpeg image onto the bitmap bitmap.canvas.stretchDraw( rect( 0,0, 200, round(jpegImage.Height/(jpegImage.Width/200)) ), jpegImage ); // We copy the bitmap back to the jpeg and save jpegImage.Assign( bitmap ); jpegImage.jpegNeeded; jpegImage.saveToFile('c:\picture.jpg'); end; You can make this procedure bigger and bigger ofcourse, but this is the basic. (ps. Don't be to hard on me, this is my very first article of this year. Thanks :) )