Table of Contents

Persistence and export

A diagram is worth saving. TTMSFNCBloxControl serializes its full element tree — geometry, appearance, links, and text — to a file or stream, reloads it later, and can render the diagram to an image for reports or previews. This chapter covers all three: file persistence, stream persistence (for databases and blobs), and image export.

Saving and loading files

SaveToFile writes the whole diagram; LoadFromFile restores it. The conventional extension is .blox.

procedure TForm1.SaveDiagram(const AFileName: string);
begin
  BloxControl1.SaveToFile(AFileName);
end;

procedure TForm1.LoadDiagram(const AFileName: string);
begin
  if TFile.Exists(AFileName) then
    BloxControl1.LoadFromFile(AFileName);
end;

Saving and loading streams

SaveToStream / LoadFromStream serialize to any TStream, which is the route for storing a diagram in a database blob, a document archive, or memory.

procedure TForm1.RoundTripThroughStream;
var
  LStream: TMemoryStream;
begin
  LStream := TMemoryStream.Create;
  try
    BloxControl1.SaveToStream(LStream);

    { Rewind before reading the stream back. }
    LStream.Position := 0;
    BloxControl1.LoadFromStream(LStream);
  finally
    LStream.Free;
  end;
end;
Tip

Save / Load (no arguments) round-trip the control's state internally, and SavedBlox returns the serialized diagram as a string — handy for diffing or embedding in JSON.

Image output

SaveToImage renders the diagram to a PNG file. Parameters control what is drawn: ABloxOnly (elements only, hiding rulers and grid), ALinkPoints (show the link-point markers), and ABackground (paint the control background). An overload returns a TTMSFNCBitmap, and SaveToImageStream writes to a stream.

Putting it together

A common workflow persists the editable diagram and exports a flattened image for a report in one step — combining file saving with image export:

procedure TForm1.SaveAndExport(const ABaseName: string);
begin
  { Persist the editable diagram. }
  BloxControl1.SaveToFile(ABaseName + '.blox');

  { Export a flattened image: elements only, link points hidden, background drawn. }
  BloxControl1.SaveToImage(ABaseName + '.png',
    True {ABloxOnly}, False {ALinkPoints}, True {ABackground});
end;

Pitfalls

  • Streams are read from the current position. Reset Position := 0 before LoadFromStream after writing to the same stream.
  • SaveToImage with ABloxOnly := True excludes rulers and the snap grid. Pass False if a report needs the measurement context.
  • Loading replaces the current diagram. Persist or confirm unsaved changes before LoadFromFile / LoadFromStream.

See also