Images and Graphics

How to display, add graphics and manipulate JPEG, PNG and BMP images by pixels.

A TImage object (an object of a TImage class) can be used to display an image file like a JPEG or PNG file. However with the TImage object it is NOT possible to edit the image, draw graphics and change pixels. To work with the graphics and pixels of the image it must be moved to a TBitmap object.

In the example app several memory (no visual) objects are used for the image handling:

  • BMPWork object for load / save of BMP image files and for all graphics and pixels edit.
  • JPEG object used for load / save of JPEG files. After loading image is moved to BMPWork object.
  • PNG object used for load / save of PNG files. After loading image is moved to BMPWork object.
  • BMPBackup object is used to store previous step of BMPWork image to allow undo of lastest step.

Image1 object is the visual component used to display bitmap of BMPWork.

Manipulation of image is done in methods (functions) like ButtonFlipHorizClick(), ButtonCirclesClick() etc.. These functions will first store a copy of BMPWork in BMPBackup object. The bitmap has a property canvas that is an array of pixels. The pixels are each a TColor consisting of Red, green and blue byte values.

To draw graphics see the example of ButtonCirclesClick(), here circles are drawn on the BMPWork.Canvas with method ellipse(), using same value for both radiuses.

In ButtonFlipHorizClick() a loop in a loop goes through the whole bitmap pixel by pixel to move the source image in BMPBackup to their new destination in BMPWork bitmap. Other flip and turn operations work by similar method.

To display bitmap result the BMPWork is assigned to Image1 component.

Image...

In method ButtonSaveClick() the extension of file name is used to determine what image format the BMPWork bitmap should be saved in. E.g. change CatNapOnShelf.jpg to CatNapOnShelf.png to save as PNG file.

In ButtonSaveClick() to save as JPEG file format (officially called JFIF) the BMPWork is assigned to JPEG object and then method SaveToFile() is called. Similar for PNG by use of PNG object. To save in BMP the BMPWork can be saved directly.



Developed using Delphi 10.

The Zip file includes all source files.


Download

ImageEdit.zip

License: CC-BY 4.0. Freeware, no warranty. Software Disclaimer.


References

Embarcadero, DocWiki TBitmap
How to Convert JPEG to BMP and BMP to JPEG in Object Pascal / Delphi



Tags: Delphi, Object Pascal, Programming

Page link: <http://foersom.org/SwDev/Delphi/ImagesGraphics.html>

Edited: 2019-11-03