4 8 Votes

Delphi: Load JPEG image to TBitmap

Tutorial by Delphian | 17/07/2012 at 01:19

Today I want to show you how you can load a JPG image from a file into a TBitmap in Delphi, for example, to edit this image in an arbitrary way.

On the top of our unit, we have to add the unit "JPEG" to our uses clause:

uses Windows, ... ,JPEG;

And here is our code that loads the JPG and saves it in the bitmap:

procedure TForm1.Button1Click(Sender: TObject);
var
   jpg: TJpegImage;
   bmp: TBitmap;
begin
   jpg := TJpegImage.Create;
   jpg.LoadFromFile('C:\image.jpg');
 
   bmp := TBitmap.Create;
   bmp.Assign(jpg);
 
   // Editing
 
   bmp.free;
   jpg.free;
end;

First, we create a new empty jpg image and we are using LoadFromFile to load an arbitrary image to it.

After that, we create an empty bitmap and with "Assign", we can load our "jpg" to the bitmap.

Then we can carry out any desired edits and afterwards, we should not forget to free both "bmp" and "jpg".

ReplyPositiveNegative

About the Author

Avatar AuthorThe author has not added a short description to his profile yet.
Show Profile | Message

 

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own articles on askingbox.com. How to do.