Firemonkey: TBitmap.Canvas drawing methods have no visible results. What am I doing wrong?
I/m building a custom FireMonkey GUI control. I want to render the control to a back buffer. The back buffer will be drawn on the control's canvas.
The back buffer is a Fmx.TBitmap object.
I am using a back buffer because the control rendering code is a little involved and does not need to be called each time the control is repainted. The back buffer will only be updated when some control properties change.
The BackBuffer.Canvas drawing operations have no visible effect. However clearing the bitmap, or setting the value of the bitmap pixels individually do work as expected.
For some reason the BackBuffer.Canvas object will not draw on the back buffer bitmap.
- I think I've set the required Canvas.Fill properties correctly.
- All the canvas properties I've checked appear to be correct. (Canvas width/height/etc)
I've extracted the relevant code in case that contains some clues.
TMyControl(TControl) private protected BackBuffer : TBitmap; procedure Paint; override; procedure Resize; override; public constructor Create(AOwner: TComponent); override; end; constructor TMyControl.Create(AOwner: TComponent); begin inherited; BackBuffer := TBitmap.Create(10, 10); end; procedure TFxSampleDisplay.Resize; var w, h : integer; begin inherited; // Ensure BackBuffer is the same size as the control. w := round(BoundsRect.Width); h := round(BoundsRect.Height); BackBuffer.SetSize(w,h); end; procedure TMyControl.Paint; var r : TRectF; begin inherited; //******** This has visible results ******** BackBuffer.Clear($1100ff00); // Fill with semi-opaque green background BackBuffer.Pixels[2,2] := $ffff0000; // Draw a red pixel //******** This doesn't have visible results ******** r.Left := 0; r.Top := 0; r.Right := 50; r.Bottom := 50; BackBuffer.Canvas.Fill.Color := $ffff0000; // Set fill to RED. BackBuffer.Canvas.Fill.Kind := TBrushKind.bkSolid; BackBuffer.Canvas.FillRect(r, 10,10, AllCorners, 1); //******** Draw the backbuffer on to the controls canvas ******** Canvas.DrawBitmap(BackBuffer, BoundsRect, BoundsRect, 1); end;
Try surrounding your drawing with:
BackBuffer.Canvas.BeginScene; .. .. BackBuffer.Canvas.EndScene; BackBuffer.BitmapChanged;
P.S. I'm pretty new with FireMonkey style, so just try it out and write if it worked please!