CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Drawing Control

  1. #1
    Join Date
    Feb 2012
    Posts
    16

    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

  2. #2
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    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.......

  3. #3
    Join Date
    Feb 2012
    Posts
    16

    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.

  4. #4
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    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
  •  





Click Here to Expand Forum to Full Width

Featured