Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I load any format of image into a TBitmap?

Delphi Imaging

How do I load any format of image into a TBitmap?

by  djjd47130  Posted    (Edited  )
It is rather common that developers wish to load various types of files into a TBitmap for manipulation, such as JPEG, PNG, or TIFF. The TBitmap is widely used as the most standard graphic for performing any type of imaging in Delphi. The question is - how to load any format of image into a TBitmap?

Delphi VCL's Graphics unit contains another class called TWICImage. This class, based on a TGraphic (just like a TBitmap), allows you to load virtually any type of image file. WIC Image stands for Windows Imaging Component. It wraps the functionality of Microsoft's WIC image. The formats which are supported depend on the formats which are registered inside of Windows.

IMPORTANT: TWICImage is only available on Windows XP SP3 or higher operating systems and relies on DirectX runtime. If your application is expected to run apart from these requirements, then you will need to recognize this yourself and handle your imaging differently.

TWICImage is used very similarly to the TBitmap. You can use LoadFromFile() and SaveToFile() for simple file loading/saving, and Assign() to/from other TGraphic classes.

For example,

Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  W: TWICImage;
  B: TBitmap;
begin
  W:= TWicImage.Create;
  try
    W.LoadFromFile('C:\Original PNG File.png');
    B:= TBitmap.Create;
    try
      B.Assign(W);
      B.SaveToFile('C:\New BMP File.bmp');
    finally
      B.Free;
    end;
  finally
    W.Free;
  end;
end;

This loads a PNG file into a WIC Image, assigns it to a Bitmap, and saves it to a BMP file.

The TWICImage contains a property called ImageFormat which determines the format of the image (once loaded). You can change this format to change the way the image is handled, and eventually the way that it is saved back to a file.

Here are the possible property values for TWICImage.ImageFormat:
> wifBmp
> wifPng
> wifJpeg
> wifGif
> wifTiff
> wifWMPhoto
> wifOther

Here's Embarcadero's documentation on the TWICImage:

http://docwiki.embarcadero.com/Libraries/en/Vcl.Graphics.TWICImage

Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top