Change the image resolution of existing xlsx file

We want to open an existing Excel file with a lot of images, change the image resolution and save it to a new file.

I have been playing around with settings like DpiForReadingImages and DpiForImages but no luck.

Any ideas?

Hi,
DpiForReadingImages and DpiForImages are for rendering, they won't affect an xlsx file. To change the resolution of the images sadly you will have to do it manually, iterating over the images and converting them. Some code like:


procedure ChangeDPI(const fileName: string; const dpi: integer);
var
  xls: TXlsFile;
  i: Integer;
  imgType: TXlsImgType;
  imgData: ByteArray;
begin
  xls := TXlsFile.Create(fileName, true);
  try 

    for i := 1 to xls.ImageCount do
    begin
      imgData := xls.GetImage(i, imgType);
      imgData := ConvertDpi(imgData, dpi);
      xls.SetImage(i, imgData);
    end;
    xls.Save(filename)

    finally
       xls.Free;
    end;

end;




Where ConvertDpi(imgData, dpi); is some method that uses a graphics library to read the image data (stored in imgData) and reduce its dpi. You might use FlexCel's TUIImage for that with lots of work, but probably some library dedicated to that will be better since TUIImage is not designed for that and lacks a simple method to change the dpi.

Thanks Adrian, this helps a lot!