Recent

Author Topic: Image copied from GLScene looks good on screen but turns black when saving [solv  (Read 7751 times)

mrBrown

  • New Member
  • *
  • Posts: 15
I have a program that uses GLScene for real-time viewing. For reporting I need to save the image on the screen to disk.
How to do that is explained here http://orion.lcg.ufrj.br/RPMS/myrpms/lazarus/lazarus-components/GLScene-CVS/Demos/rendering/tobitmap/Unit1.pas.
This has always worked for me in the past, but since a recent upgrade to lazarus 0.9.30 and the latest svn version of GLScene my images turned black.

The funny thing is, when I assign the bitmap to an TImage component on the form, then the image is displayed correctly on the screen. But when I save it I get a black image.

My guess is that I'm doing something wrong with the pixel format or palette, but I don't have a clue how to solve it.

Attached is a small test progam. The copy-ing on the screen works ok, but the file (hard coded to c:\data\temp\test.png') is black. Also the image copied to the clipboard is black.

Using windows 7.

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
   var
   bmp : TBitmap;
   scale : integer;
   pic   : TPicture;
begin
   scale := 2;

   // Rendering to a bitmap requires an existing bitmap,
   // so we create and size a new one

   pic := TPicture.Create;
   bmp := TBitmap.Create;

   // Don't forget to specify a PixelFormat, or current screen pixel format
   // will be used, which may not suit your purposes!

   bmp.PixelFormat:=pf32bit;
   bmp.Width:=Round(GLSceneViewer1.Width*scale);
   bmp.Height:=Round(GLSceneViewer1.Height*scale);

   // Here we just request a render
   // The second parameter specifies DPI (Dots Per Inch), which is
   // linked to the bitmap's scaling
   // "96" is the "magic" DPI scale of the screen under windows

   GLSceneViewer1.Buffer.RenderToBitmap(bmp, Round(96*scale));

   pic.Assign(bmp);
   image1.Picture.Assign(pic);                         // works on screen!
   image1.Picture.SaveToFile('c:\data\temp\test.png'); // gives black image

   clipboard.Assign(bmp);                              // gives black image

   bmp.Free;

end;
« Last Edit: July 11, 2011, 02:19:10 pm by mrBrown »

mrBrown

  • New Member
  • *
  • Posts: 15
Drawing the bitmap on the canvas did the trick

bmp.canvas.draw(0,0,bmp);

Code: [Select]
   scale := 2;

   // Rendering to a bitmap requires an existing bitmap,
   // so we create and size a new one

   bmp := TBitmap.Create;

   // Don't forget to specify a PixelFormat, or current screen pixel format
   // will be used, which may not suit your purposes!

   bmp.PixelFormat:=pf32bit;
   bmp.Width:=Round(GLSceneViewer1.Width*scale);
   bmp.Height:=Round(GLSceneViewer1.Height*scale);

   // Here we just request a render
   // The second parameter specifies DPI (Dots Per Inch), which is
   // linked to the bitmap's scaling
   // "96" is the "magic" DPI scale of the screen under windows

   GLSceneViewer1.Buffer.RenderToBitmap(bmp, Round(96*scale));


   // Draw the bitmap on the canvas of a TPicture

   pic := TPicture.Create;
   pic.bitmap.width := bmp.width;
   pic.bitmap.height := bmp.height;
   pic.bitmap.Canvas.Draw(0,0,bmp);


   // And save picture to disk

   pic.SaveToFile('c:\data\temp\test.png'); // gives black image

   bmp.Free;
   pic.Free;


Don't ask me why...

 

TinyPortal © 2005-2018