Click here to Skip to main content
12,689,568 members (25,660 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

46.8K views
6 bookmarked
Posted

Drawing on picture box images

, 24 Jan 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Drawing on picture box images
You can draw an Ellipse on the pictureBox image using the following code:
public void drawEllipse(PictureBox pb, int x, int y, int w, int h, float Bwidth,Color col)
        {
            //refresh the picture box
            pb.Refresh();
            //create a graphics object
            Graphics g = pb.CreateGraphics();
            //create a pen object
            Pen p = new Pen(col, Bwidth);
            //draw Ellipse
            g.DrawEllipse(p, x, y, w, h);
            //dispose pen and graphics object
            p.Dispose();
            g.Dispose();
        }

In here, I grab the picturebox graphics using pb.CreateGraphics() so that I can draw on its surface. But drawing on the PictureBox only affects the pixels on the screen, not the pixels in the image. You can however, obtain the Graphics for the image. You can see that in the next example.

You can draw a line on the pictureBox image using the following code:


public void drawline(PictureBox pb, Point p1, Point p2, float Bwidth,Color c1)
        {
            //refresh the picture box
            pb.Refresh(); 
            //create a new Bitmap object
            Bitmap map=(Bitmap)pb.Image;  
            //create a graphics object
            Graphics g = Graphics.FromImage(map);  
            //create a pen object and setting the color and width for the pen
            Pen p = new Pen(c1, Bwidth);  
            //draw line between  point p1 and p2
            g.DrawLine(p, p1, p2);  
            pb.Image = map;  
            //dispose pen and graphics object
            p.Dispose();  
            g.Dispose();
        }

After creating the graphics object(g), there are several predefined methods you can use to draw objects. Some are listed below:
g.DrawArc
g.DrawCurve
g.DrawBezier
g.DrawIcon
g.DrawImage
g.DrawPath 
g.DrawRectangle

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Dhanushka Madushan lk
Software Developer
Sri Lanka Sri Lanka
No Biography provided

You may also be interested in...

Comments and Discussions

 
General//draw a filled Ellipse Graphics g = Graphics.FromImage(Ima... Pin
Dhanushka Madushan Peiris24-Jan-12 23:07
memberDhanushka Madushan Peiris24-Jan-12 23:07 
GeneralRe: //draw a filled Ellipse Graphics g = Graphics.FromImage(Ima... Pin
rexpertq31-May-13 3:09
memberrexpertq31-May-13 3:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170113.4 | Last Updated 24 Jan 2012
Article Copyright 2012 by Dhanushka Madushan lk
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid