# Saving Images

# Saving Images

Images can be saved to the files on disk, to the streams or to a memory referenced by a pointer. The following table lists functions which can be used for saving images. These are low level functions operating on TImageData structure.

Function name
Usage
SaveImageToFile
Save single image to file
SaveImageToStream
Save single image to stream (TStream descendants, Object Pascal only)
SaveImageToMemory
Save single image to memory
SaveMultiImageToFile
Save multiple images to file
SaveMultiImageToStream
Save multiple images to stream (TStream descendants, Object Pascal only)
SaveMultiImageToMemory
Save multiple images to memory

You can learn how to use these functions from the following code fragments.
This example creates empty image and draws diagonal line from left-top to right-bottom corner. Finally, image is saved to the file.
uses SysUtils, Classes, ImagingTypes, Imaging; var Img: TImageData; I: LongInt; begin // call this before using any TImageData record InitImage(Img); // create 8 bit grayscale image NewImage(1024, 1024, ifGray8, Img); // draw diagonal line by direct writing to image's memory for I := 0 to Img.Width - 1 do PByteArray(@PByteArray(Img.Bits)[I * Img.Width])[I] := 128; // save image to file SaveImageToFile('/home/galfar/images/line.tga', Img); // memory occupied by image is freed FreeImage(Img); end.
In this example DDS image is loaded from stream. All contained subimages are then compressed to DXT5 format and written to output stream.
procedure LoadAndSaveSomethingInDXT5(InStream, OutStream: TStream); var Images: TDynImageDataArray; I: LongInt; begin // load DDS multiimage from stream to Images array LoadMultiImageFromStream(InStream, Images); // convert all loaded images to DXT5 format for I := 0 to Length(Images) - 1 do ConvertImage(Images[I], ifDXT5); // save converted images to output stream in DDS format SaveMultiImageToStream('dds', OutStream, Images); // all images are freed FreeImagesInArray(Images); end;