Mega Code Archive

 
Categories / Delphi / Graphic
 

Creating temporary canvas

Title: Creating temporary canvas Question: I need a temporary canvas to draw to, but when I attempt to create one, I get several application errors. How do I do this? Answer: Create a Bitmap, and use the TBitmap's canvas property to draw on. The following example creates a Bitmap, draw on its' canvas, draws the canvas to the form's backgound, and then free the bitmap. Example: procedure TForm1.Button1Click(Sender: TObject); var bm : TBitmap; begin bm := TBitmap.Create; bm.Width := 100; bm.Height := 100; bm.Canvas.Brush.Color := clRed; bm.Canvas.FillRect(Rect(0, 0, 100, 100)); bm.Canvas.MoveTo(0, 0); bm.Canvas.LineTo(100, 100); Form1.Canvas.StretchDraw(Form1.ClientRect, Bm); bm.Free; end;