Loading Images

Images can be loaded from the files on disk, from the streams or from a memory referenced by a pointer. The following table lists functions for loading images. These are low level functions operating on TImageData structure.

Function nameUsage
LoadImageFromFile Load single image from file
LoadImageFromStream Load single image from stream (TStream descendants, Object Pascal only)
LoadImageFromMemory Load single image from memory
LoadMultiImageFromFile Load multiple images from file
LoadMultiImageFromStream Load multiple images from stream (TStream descendants, Object Pascal only)
LoadMultiImageFromMemory Load multiple images from memory

If you want to make sure that image in file is really in format suggested by its extension or if you have some unknown images in stream or memory, you can use these functions to get their file format:

Function nameUsage
DetermineFileFormat Determines format of image specified by its file name
DetermineStreamFormat Determines format of image located in stream (TStream descendants, Object Pascal only)
DetermineMemoryFormat Determines format of image located in memory

This example loads image from file and prints its width, height and size of memory it occupies.

uses
  SysUtils, Classes, ImagingTypes, Imaging;
var
  Img: TImageData;
begin
  // call this before using any TImageData record
  InitImage(Img);
  // load tigers from file
  LoadImageFromFile('X:\images\tigers.jpg', Img);
  //write some image info
  WriteLn('Mighty tigers have resolution ', Img.Width, 'x', Img.Height,
  ' and occupy ', Img.Size, ' bytes of your memory.');
  // memory occupied by image is freed
  FreeImage(Img);
end.
    

This example shows how to load multiple images stored in one file (DDS texture in this case) located in the memory.

var
  Data: Pointer;
  Size: LongInt;
  Images: TDynImageDataArray;
  I: LongInt;
begin
  // Here you for example load DDS texture compressed by your new
  // compression algorithm from file and decompress it.
  // Decompressed image is now in memory referenced by Data and
  // size of this image is in Size variable.
  // Note that there is no need to call InitImage for TDynImageDataArray.
  LoadMultiImageFromMemory(Data, Size, Images);
  // write something
  WriteLn('DDS file contains ', Length(Image), 'subimages.');
  // You can then free images in array by calling FreeImage for all of them ...
  for I := 0 to Length(Images) - 1 do
    FreeImage(Images[I]);
  // ... or simply call FreeImagesInArray which does the same job
  FreeImagesInArray(Images);
end;
    

This example shows how to load image from stream without knowing what format it is in.

function LoadImage(var Img: TImageData; Stream: TStream): Boolean;
var
  Ext: string;
begin
  // call this before using any TImageData record
  InitImage(Img);
  // determine image's format
  Ext := DetermineStreamFormat(Stream);
  // if image is in unsupported format or is invalid we output error
  if Ext = '' then
  begin
    WriteLn('Image in stream in unsupported formatus!');
    Result := False;
  end
  else
  begin
    // load image if its type is known
    Result := LoadImageFromStream(Stream, Img);
  end;
end;