1

We are using SVG images in our application.

Here's what our code looks like for JPEG images (it's a working sample from our application, it is similar to this):

Var
  memoryStreamFile: TMemoryStream;
  blobFieldFile:TBlobField;  
  JPEGImage: TJPEGImage;
  //ImageJPEG:TImage is a TImage on the form

blobFieldFile:=ClientDataSet.fieldbyName('Image') as TblobField ;
memoryStreamFile := TMemoryStream.Create;

blobFieldFile.SaveToStream(memoryStreamFile);
memoryStreamFile.Position:=0;

JPEGImage := TJPEGImage.Create;
try
  JPEGImage.LoadFromStream(memoryStreamFile);
  ImageJPEG.Picture.Assign(JPEGImage);
finally
  JPEGImage.Free;
end; 

I would like to do the same as above for SVG images:

Var
  memoryStreamFile: TMemoryStream;
  blobFieldFile:TBlobField;  
  SVGImage: TSVGImage; -->>This does not exist
  //ImageSVG:TImage is a TImage on the form

blobFieldFile:=ClientDataSet.fieldbyName('Image') as TblobField ;
memoryStreamFile := TMemoryStream.Create;

blobFieldFile.SaveToStream(memoryStreamFile);
memoryStreamFile.Position:=0;

SVGImage := TSVGImage.Create;//Sample of how I would envision it to work. Obviously this does not exist
try
  SVGImage.LoadFromStream(memoryStreamFile);
  ImageSVG.Picture.Assign(SVGImage);
finally
  SVGImage.Free;
end; 

We basically generate the SVG code in text format and would like to load the text into TImage.

Is there a way to stream this that I'm not seeing?

I know that I can take our SVG text and save it to file, and then do TImage.Picture.LoadFromFile(), but I prefer to do TImage.Picture.LoadFromStream().

I know that there are other components that could be used, ie Delphi SVG (we currently use this).

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
L. Piotrowski
  • 1,533
  • 1
  • 20
  • 35

1 Answers1

3

You should use ClientDataSet.CreateBlobStream() instead of blobFieldFile.SaveToStream(), then you don't need the TMemoryStream at all:

var
  Blob: TField;
  Stream: TStream;
  JPEG: TJPEGImage;
begin
  Blob := ClientDataSet.FieldByName('Image');
  Stream := ClientDataSet.CreateBlobStream(Blob, bmRead);
  try
    JPEG := TJPEGImage.Create;
    try
      JPEG.LoadFromStream(Stream);
      Image.Picture.Assign(JPEG);
    finally
      JPEG.Free;
    end; 
  finally
    Stream.Free;
  end;
end; 

As for loading an SVG image, all you need is a TGraphic class that supports SVG. The VCL has no such class available natively, but the Delphi SVG package you are already using does - TSVG2Graphic:

var
  Blob: TField;
  Stream: TStream;
  SVG: TSVG2Graphic;
begin
  Blob := ClientDataSet.FieldByName('Image');
  Stream := ClientDataSet.CreateBlobStream(Blob, bmRead);
  try
    SVG := TSVG2Graphic.Create;
    try
      SVG.LoadFromStream(Stream);
      Image.Picture.Assign(SVG);
    finally
      SVG.Free;
    end; 
  finally
    Stream.Free;
  end;
end; 
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770