2

I'm developing a Firemonkey (FMX) application using Delphi 10.3.3.

I have a Viewport3D component. Inside the Viewport3D, i have many 3D Shapes such as TCube and TRectangle3D.

I need to save the contents of the Viewport3D (a screenshot of the current view) to an image file. I prefer to save as a transparent PNG. How can I do that?

Xel Naga
  • 826
  • 11
  • 28

1 Answers1

6
Image1.Bitmap.Assign( Viewport3D1.MakeScreenshot );

for converting bitmap to png;

Uses FMX.Surfaces;

procedure TMain.Button1Click(Sender: TObject);
var
 Stream: TMemoryStream;
 Surf: TBitmapSurface;

begin
 Stream:=TMemoryStream.Create;
 Stream.Position := 0;
 Surf := TBitmapSurface.Create;
 try
  Surf.Assign(Viewport3D1.MakeScreenshot);
  if TBitmapCodecManager.SaveToStream(Stream, Surf, '.png') then
  Begin
   Stream.SaveToFile('screenshot.png');
  End
  Else
   raise EBitmapSavingFailed.Create('Error saving Bitmap to png');
 finally
  Stream.Free;
  Surf.Free;
 end;
end;
  • 1
    It should be noted that `Viewport3D.Color` property should be set to `Null` in order to end up with a transparent image. I'm pretty sure OP already know this but it might come in handy for others. – SilverWarior Aug 08 '21 at 15:26
  • 1
    Please don't make more work for others by vandalizing your posts. By posting on the Stack Exchange (SE) network, you've granted a non-revocable right, under the [CC BY-SA license](https://creativecommons.org/licenses/by-sa/4.0), for SE to distribute the content (regardless of your future choices). By SE policy, the non-vandalized version is distributed. Thus, any such destructive edits will be reverted. Please see [How does deleting work?](/help/what-to-do-instead-of-deleting-question) for more information on how deleting content works on this site. – Ryan M May 19 '22 at 17:43
  • 1
    Please do not vandalize your posts, especially not accepted answers. By posting on this site, you've irrevocably granted the Stack Exchange network the right to distribute that content under the CC BY-SA 4.0 license for as long as it sees fit to do so. For alternatives to deletion, see: [I've thought better of my question; can I delete it?](https://stackoverflow.com/help/what-to-do-instead-of-deleting-question) – Ollie May 19 '22 at 17:43