Images disappear sometimes

Hi @adrian,

thanks for your response. I have created a minimal template to demonstrate the problem:

Template.xlsx (17.7 KB)

Use it as it is, and no image is printed. Give more height for row 8, and the images gets printed.

unit Unit1;

interface

uses
  System.SysUtils,
  System.Types,
  System.UITypes,
  System.Classes,
  System.Variants,

  FMX.Types,
  FMX.Controls,
  FMX.Forms,
  FMX.Graphics,
  FMX.Dialogs,
  FMX.Controls.Presentation,
  FMX.StdCtrls,

  FMX.FlexCel.Core,

  FlexCel.Report,
  FlexCel.XlsAdapter,
  FlexCel.Render;

type
  TForm1 = class(TForm)
    btn1: TButton;
    procedure btn1Click(Sender: TObject);
  private
    procedure DrawFromExcel(const xlsPath: string; const Bitmap: TBitmap; const Report: TFlexCelReport);
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.DrawFromExcel(const xlsPath: string; const Bitmap: TBitmap; const Report: TFlexCelReport);
   var
    xlsFile: TXlsFile;
    image: TUIImage;
    rowCount, colCount, tmpRow: Integer;
    I1: Integer;
    totalWidthInPx,
    scaleFactor: Double;
   begin
    xlsFile := TXlsFile.Create(xlsPath);
    try
      xlsFile.PrintToFit := False;
      xlsFile.PrintNumberOfHorizontalPages := 1;
      xlsFile.PrintNumberOfVerticalPages := 0;
      xlsFile.PrintHCentered := True;
      xlsFile.PrintPaperSize := TPaperSize.Undefined;

      // Erstes Sheet auswählen
      xlsFile.ActiveSheet := 1;

      // Ersetzung durchführen
      if Assigned(Report) then Report.Run(xlsFile);

      rowCount := xlsFile.RowCount;
      colCount := xlsFile.ColCount;

      // rowCount betrachtet nur Zellen mit Daten oder Hintergrund, daher
      // wird geprüft ob eingebettete Objekte noch weitere Zellen belegen.
      for I1 := 1 to xlsFile.ObjectCount do
         begin
          tmpRow := xlsFile.GetObjectAnchor(I1).Row2;
          if tmpRow > rowCount then
             begin
              rowCount := tmpRow;
             end;
         end;

      // Get the real Pixel width of the to-be-rendered area, so we can scale it to
      // the targeted width.
      totalWidthInPx := 0.0;
      for I1 := 1 to colCount do
         begin
          totalWidthInPx := totalWidthInPx + (xlsFile.GetColWidth(I1) / TExcelMetrics.ColMult(xlsFile));
         end;

      scaleFactor := 576 / totalWidthInPx;

      image := xlsFile.RenderCells(
          1,             //  row1 As Integer,
          1,             //  col1 As Integer,
          rowCount,      //  row2 As Integer,
          colCount,      //  col2 As Integer,
          True,          //  drawBackground As Boolean, drawBackground true bezieht sich auf den ZellenHintergrund (nicht das
                         //  Hintergrundbild), so kann der zu rendernde Bereich durch Zellenhintergrund erweitert werden.
          91.25,         //  dpi As Double, DPI und scaleFactor legt die Rendergröße fest.
          TUISmoothingMode.None,    //  aSmoothingMode As TUISmoothingMode,
          TUIInterpolationMode.Low, //  aInterpolationMode As TUIInterpolationMode,
          False,         //  antiAliased As Boolean,
          scaleFactor,   //  aPrintScale As Double,
          False,         //  forceBackgroundTransparent As Boolean,
          True           //  exportObjects As Boolean
      );

      try
        if not Assigned(image) then raise Exception.Create('Error in rendering Cells, image not assigned.');

        Bitmap.SetSize(Round(image.Width), Round(Image.Height));
        Bitmap.Canvas.BeginScene;
        try
          image.ToNativeImage(Bitmap.Canvas, 1.00);
        finally
          Bitmap.Canvas.EndScene;
        end;
      finally
        image.Free;
      end;
    finally
      xlsFile.Free;
    end;
   end;

procedure TForm1.btn1Click(Sender: TObject);
   var
    Report: TFlexCelReport;
    Bitmap: TBitmap;
   begin
    Bitmap := TBitmap.Create();
    try
      Report := TFlexCelReport.Create();
      try
        Report.SetValue('BelegNummer', 123);

        DrawFromExcel('Template.xlsx', Bitmap, Report);
      finally
        Report.Free;
      end;

      Bitmap.SaveToFile(Format('print_%s_%.4d.png', [FormatDateTime('yyyymmddhhnnsszzz', Now), Random(9999)]));
    finally
      Bitmap.Free;
    end;
   end;

end.

// edit: yes, FMX. It happens on Windows and Android. FlexCel 7.13.0.0.

Thanks!
Dominik