Table of Contents

TExcelFile.GetImage Method

Overloads

TExcelFile.GetImage(Integer, TXlsImgType)

Returns an image bytes and type. Note that for SVG images, xlsx files store both a PNG and SVG image. In those cases, for backward compatibility reasons, this method will return the PNG image. To get the SVG, call GetImageAlternate

Syntax

Unit: FlexCel.Core

function TExcelFile.GetImage(const imageIndex: Integer; var imageType: TXlsImgType): TBytes; overload;

Parameters

<-> Parameter Type Description
const imageIndex Integer Index of the image. (1 based)
var imageType TXlsImgType Returns the image type for the data returned. (If it is a bmp, jpg or other)

Returns

Image data. Use the returned imageType to find out the format of the stored image.

See also

TExcelFile.GetImage(Integer, TXlsImgType, TStream)

Returns an image and its type. Note that for SVG images, xlsx files store both a PNG and SVG image. In those cases, for backward compatibility reasons, this method will return the PNG image. To get the SVG, call GetImageAlternate

Syntax

Unit: FlexCel.Core

procedure TExcelFile.GetImage(const imageIndex: Integer; var imageType: TXlsImgType; const outStream: TStream); overload;

Parameters

<-> Parameter Type Description
const imageIndex Integer Index of the image. (1 based)
var imageType TXlsImgType Returns the image type for the data returned. (If it is a bmp, jpg or other)
const outStream TStream Stream where the image data will be copied.

Examples

The method "GetImageFromName" in the code below will extract an image in the active sheet if you know the name of the image. Note that the name must be a real name that was manually added to the image. Image names like "Image 1" might or might not work depending in the Excel version.

The method "GetAllImagesInSheet" will loop over all the existing images in the active sheet and save them.

function GetExtension(const ImgType: TXlsImgType): string;
begin
  case ImgType of
    TXlsImgType.Bmp:
    begin
      exit('.bmp');
    end;
    TXlsImgType.Emf:
    begin
      exit('.emf');
    end;
    TXlsImgType.Jpeg:
    begin
      exit('.jpg');
    end;
    TXlsImgType.Wmf:
    begin
      exit('.wmf');
    end;
    TXlsImgType.Tiff:
    begin
      exit('.tif');
    end;
    TXlsImgType.Gif:
    begin
      exit('.gif');
    end;
    TXlsImgType.Png:
    begin
      exit('.png');
    end;
    else
    begin
      exit('');
    end;
  end;
end;

procedure GetImageFromName;
var
  xls: TXlsFile;
  filename: string;
  imageType: TXlsImgType;
  ImageStream: TFileStream;
begin
  xls := TXlsFile.Create('myfile.xlsx');
  try
    //Get an image named "myimage" and save it 
    //as myimage.png, myimage.jpeg, etc depending in the 
    //image type.
    filename := 'myimage';  
    imageType := TXlsImgType.Unknown;
    ImageStream := TFileStream.Create(filename, fmCreate);
    try
        xls.GetImage(-1, '@myimage', imageType, ImageStream, true);  //For this to work, last parameter must be true.
    finally
      ImageStream.Free;
    end;
    TFile.Move(filename, TPath.ChangeExtension(filename, GetExtension(imageType)));
  finally
    xls.Free;
  end;
end;

procedure GetAllImagesInSheet;
var
  xls: TXlsFile;
  imageType: TXlsImgType;
  i: Int32;
  filename: string;
  ImageStream: TFileStream;
begin
  xls := TXlsFile.Create('myfile.xlsx');
  try
     //Get all the images in the active sheet and save them
     //as myimage1.png, myimage2.jpeg, etc depending in the
     //image type.
    imageType := TXlsImgType.Unknown;
    for i := 1 to xls.ImageCount do
    begin
      filename := 'myimage' + IntToStr(i);
      ImageStream := TFileStream.Create(filename, fmCreate);
      try
        xls.GetImage(i, imageType, ImageStream);
      finally
        ImageStream.Free;
      end;
      TFile.Move(filename, TPath.ChangeExtension(filename, GetExtension(imageType)));
    end;
  finally
    xls.Free;
  end;

end;

See also

TExcelFile.GetImage(Integer, string, TXlsImgType, TStream, Boolean)

Returns an image and its type. Note that for SVG images, xlsx files store both a PNG and SVG image. In those cases, for backward compatibility reasons, this method will return the PNG image. To get the SVG, call GetImageAlternate

Syntax

Unit: FlexCel.Core

procedure TExcelFile.GetImage(const imageIndex: Integer; const objectPath: string; var imageType: TXlsImgType; const outStream: TStream; const usesObjectIndex: Boolean); overload;

Parameters

<-> Parameter Type Description
const imageIndex Integer Index of the image. (1 based)
const objectPath string Object path to the image when it is a grouped image. For toplevel images you can use String.Empty.
In other case, you need to use the value returned by GetObjectProperties(Integer, Boolean)
Important: ObjectPath is ignored if usesObjectIndex is false.


If it is "absolute"(it starts with "\"), then the path includes the objectIndex, and the objectIndex is not used. An object path of "\1\2\3" is exactly the same as using objectIndex = 1 and objectPath = "2\3"
You might also use the name of the image as object path, like in xls.GetImage(-1, "@myimage", ref imageType, ImageStream, true)
var imageType TXlsImgType Returns the image type for the data returned. (If it is a bmp, jpg or other)
const outStream TStream Stream where the image data will be copied.
const usesObjectIndex Boolean If false (the default) then imageIndex is an index to the list of images.
When true imageIndex is an index to the list of all objects in the sheet. When you have the object id, you can avoid calling ObjectIndexToImageIndex which is a slow method, by setting this parameter to true.

Examples

The method "GetImageFromName" in the code below will extract an image in the active sheet if you know the name of the image. Note that the name must be a real name that was manually added to the image. Image names like "Image 1" might or might not work depending in the Excel version.

The method "GetAllImagesInSheet" will loop over all the existing images in the active sheet and save them.

function GetExtension(const ImgType: TXlsImgType): string;
begin
  case ImgType of
    TXlsImgType.Bmp:
    begin
      exit('.bmp');
    end;
    TXlsImgType.Emf:
    begin
      exit('.emf');
    end;
    TXlsImgType.Jpeg:
    begin
      exit('.jpg');
    end;
    TXlsImgType.Wmf:
    begin
      exit('.wmf');
    end;
    TXlsImgType.Tiff:
    begin
      exit('.tif');
    end;
    TXlsImgType.Gif:
    begin
      exit('.gif');
    end;
    TXlsImgType.Png:
    begin
      exit('.png');
    end;
    else
    begin
      exit('');
    end;
  end;
end;

procedure GetImageFromName;
var
  xls: TXlsFile;
  filename: string;
  imageType: TXlsImgType;
  ImageStream: TFileStream;
begin
  xls := TXlsFile.Create('myfile.xlsx');
  try
    //Get an image named "myimage" and save it 
    //as myimage.png, myimage.jpeg, etc depending in the 
    //image type.
    filename := 'myimage';  
    imageType := TXlsImgType.Unknown;
    ImageStream := TFileStream.Create(filename, fmCreate);
    try
        xls.GetImage(-1, '@myimage', imageType, ImageStream, true);  //For this to work, last parameter must be true.
    finally
      ImageStream.Free;
    end;
    TFile.Move(filename, TPath.ChangeExtension(filename, GetExtension(imageType)));
  finally
    xls.Free;
  end;
end;

procedure GetAllImagesInSheet;
var
  xls: TXlsFile;
  imageType: TXlsImgType;
  i: Int32;
  filename: string;
  ImageStream: TFileStream;
begin
  xls := TXlsFile.Create('myfile.xlsx');
  try
     //Get all the images in the active sheet and save them
     //as myimage1.png, myimage2.jpeg, etc depending in the
     //image type.
    imageType := TXlsImgType.Unknown;
    for i := 1 to xls.ImageCount do
    begin
      filename := 'myimage' + IntToStr(i);
      ImageStream := TFileStream.Create(filename, fmCreate);
      try
        xls.GetImage(i, imageType, ImageStream);
      finally
        ImageStream.Free;
      end;
      TFile.Move(filename, TPath.ChangeExtension(filename, GetExtension(imageType)));
    end;
  finally
    xls.Free;
  end;

end;

See also