Skip to content

Rendering TBIChart to PNG #21

Description

@De-Novo-Research

Hello.

If I use the code below, I can save any TChart to PNG. Even one I create programmatically and has never been actually shown onscreen.

However, when I use this method on TBIChart.Chart, without the TBiChart being shown onscreen, the resulting PNG just has a black border and nothing else. I cannot see why this should not work, since TBITChart is just a TChart descendent.

If I show the TBiChart onscreen and then call the code below on the BiChart1.Chart, the resulting PNG is fine. Is there some method I can call to force the TBiChart.Chart to update itself so it can render to PNG even though it has not been shown on screen yet?

  TChartHelper=class helper for TChart
  public
    procedure SaveToPNG(const aFileName:string; const aWidth:integer=-1; const aHeight:integer=-1);
  end;



implementation

procedure TChartHelper.SaveToPNG(const aFileName:string; const aWidth:integer=-1; const aHeight:integer=-1);
var
  PNGExport: TPNGExportFormat;
begin
  PNGExport := TPNGExportFormat.Create;
  try
    PNGExport.Panel := self;
    if aWidth>0 then
      PNGExport.Width := aWidth
    else
      pngExport.Width := self.Width;

    if aHeight>0 then
      PNGExport.Height := aHeight
    else
      pngExport.Height:=self.Height;

    PNGExport.SaveToFile(aFileName);
  finally
    PNGExport.Free;
  end;
end;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions