Page 1 of 1

SimpleGraph - Exporting Graph as Image

PostPosted: March 18th, 2006, 6:28 pm
by Kambiz
Version 2.1 of SimpleGraph introduces a new method named CopyToGraphic. Using this method you can make a copy of the graph as a Graphic object.

For example, the following routine exports the graph as a JPEG file.

Code: Select all
uses
  JPEG;

procedure ExportGraphAsJPEG(SG: TSimpleGraph; const FileName: String);
var
  JPEG: TJPEGImage;
begin
  JPEG := TJPEGImage.Create;
  try
    SG.CopyToGraphic(JPEG);
    JPEG.SaveToFile(FileName);
  finally
    JPEG.Free;
  end;
end;


Or more generic, the following routine exports the graph in to a file as specified image format:

Code: Select all
procedure ExportGraphAsImage(SG: TSimpleGraph; GraphicClass: TGraphicClass;
  const FileName: String);
var
  Graphic: TGraphic;
begin
  Graphic := TGraphicClass.Create;
  try
    SG.CopyToGraphic(Graphic);
    Graphic.SaveToFile(FileName);
  finally
    Graphic.Free;
  end;
end;


Here is the example of usage for the above routine:

Code: Select all
procedure TForm1.Button1Click(Sender: TObject);
begin
  if SavePictureDialog1.Execute then
    ExportGraphAsImage(SimpleGraph1, TJPEGImage, SavePictureDialog1.FileName);
end;


You can use freeware GraphicEx library to extend image formats available in Delphi.