|
-
February 23rd, 2012, 02:34 AM
#1
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
-
February 23rd, 2012, 05:53 PM
#2
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.
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
February 24th, 2012, 12:15 AM
#3
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?
Last edited by sean.aulason; February 24th, 2012 at 01:46 AM.
-
March 2nd, 2012, 12:25 AM
#4
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.
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|