-
Drawing Control
hi there,
in Delphi, there is a control named TPaintBox which is similar to PictureBox but with a Canvas property that is very useful if we intend to do some drawing on it.
1. is there any control in C# like TPaintBox to draw something on it?
2. would you please write a simple code snippet in which a simple line is drawn on this control?
thanks for your time
-
Re: Drawing Control
You can use the:
Code:
// Create a Graphics object for the Control.
Graphics g = pictureBox.CreateGraphics();
to get the Graphics object for the PictureBox control. Then use the members of the Graphics class to draw on it.
-
Re: Drawing Control
thanks but how should i redraw the line?
it vanishes as soon as i scroll it or something covers it for a while!
EDIT:
ok,
i find a solution and it's using Bitmap instead.
so here is my code:
Code:
Bitmap bm = new Bitmap(picturebox.Width, picturebox.Height);
Graphics g = Graphics.FromImage(bm);
System.Drawing.Pen p = new System.Drawing.Pen(Color.Red);
g.DrawLine(p, 0, 0, 10, 10);
picturebox.Image = bm;
p.Dispose();
g.Dispose();
but there's a problem. since i create PictureBoxes at runtime and i remove them by calling Controls.Clear(), when should i dispose this bitmap object?
-
Re: Drawing Control
In general, all drawing code should be placed in the forms Paint event handler. This is automatically called when part of your application needs to be drawn.