How do I draw a circle and line in the picturebox?
Asked Answered
L

4

16

How do I draw a circle and line in the picturebox?

Laddy answered 28/4, 2010 at 13:22 Comment(0)
S
1

the picturebox is a control and has an image as source - so you have to draw on the image and hand the image to the control to show it

MyImage = new Bitmap(fileToDisplay);
pictureBox1.ClientSize = new Size(xSize, ySize);
pictureBox1.Image = MyImage;
Sundin answered 28/4, 2010 at 13:34 Comment(2)
This draws into a bitmap, not onto the picturebox. It works but it is somethng altogether different!Laryngotomy
This doesn't answer the poster's question.Pedestal
C
36

or:

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawLine(
            new Pen(Color.Red,2f), 
            new Point(0,0), 
            new Point(pictureBox1.Size.Width, pictureBox1.Size.Height ));

        e.Graphics.DrawEllipse(
            new Pen(Color.Red, 2f),
            0,0, pictureBox1.Size.Width, pictureBox1.Size.Height  );
    }

Handle the paint event of the picture box and do your custom drawing there.

Component answered 28/4, 2010 at 13:48 Comment(1)
This is the correct answer to the question.Pedestal
S
7

The best way is to NOT draw a circle and line in a picturebox! It is not designed for that purpose.

From Bob Powell's GDI+ blog:

The root of this problem is that the fundamental rules of windows programming have been broken. And as a consequence of the picture box is blamed for something that's really not its fault. To help explain why, the four points below outline what's gone wrong in this case.

  • The PictureBox control is for displaying images. It is not a handy placeholder for a graphics surface.

  • Windows is an event driven system in which each event must be serviced in the correct context and events destined to handle button click or mouse move events must not be used to do drawing on screen or other weird stuff.

  • The PictureBox refreshes itself by drawing the System.Drawing.Image based object stored in it's Image property. If there is no image, it will show the background colour.

  • Stealing and drawing upon the Graphics object of any control is not good practice, should be strongly discouraged and breaks the rules of handling events in the right place at the right time. Basically if you do this it will cause you pain. When you bang your head against a wall it causes you pain. that is a sign that you should stop doing it. It's the same for the PictureBox.CreateGraphics call.

The right way to do it.

Following the rules of the event driven system is easy but requires a little forethought. So, if you want to draw some little bit of graphics and have it remain there when a window moves in front of it and away again or when you minimize and restore, you have to service the Paint event of whatever object it is that you wish to paint on. The PictureBox carries baggage around with it that is unnecessary for this kind of application. If you just want to draw something in one place, draw it on the form by responding to the Form.Paint event. If you want a handy placeholder for a graphic that works within a set bounds, use a Panel control and service it's Paint event. If you want to duplicate a graphic over and over for your corporate image, create a control and do the drawing in the OnPaint override.

Source: https://web.archive.org/web/20120330003635/http://bobpowell.net/picturebox.htm (the original site is defunct).

Salesman answered 28/4, 2010 at 13:34 Comment(6)
+1 This really is the proper answer. Thanks for the link, which describes the root problem.Johannesburg
-1 "Linkrot"... and this answer gives few hints as to what the problem is or what to do instead. "Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline." Source: stackoverflow.com/help/how-to-answer I did however find the bobpowell pages preserved in the "Wayback Machine": web.archive.org/web/20120216111903/http://bobpowell.net/…Tudor
This is a terrible answer. The (otherwise great) pages of bobpowell are not only gone but do in no way suggest that you shouldn't draw onto a PictureBox. In fact their default DoubleBuffering makes it the only controls that is suitable for drawing out of the box. Bob merely warns against using the draeded Control.CreateGraphics all newbies copy from the also terrible MSDN starts example page.Laryngotomy
@Laryngotomy - I have updated the answer from Bob's blog (which I can only find on the wayback machine). He did warn against drawing directly on the surface of another control. I think it would be better to create a Graphics instance from the image that is displayed in the PictureBox and draw on that instead.Salesman
Good. Well This 'question' clearly doesn't give enough context; but a circle and a line are so cheap drawing them can' possible lead to problems. 1000+ operations are a diffwerent kettle of fish.. Most of what you quote from what Bob writes is not convincing; I really wonder what he means by 'baggage'. A myth imo. So I change my above comment to 'but do in no convincing way suggests that you shouldn't draw onto a PictureBox.'Laryngotomy
@Laryngotomy - I'm not 100% sure of Bob's intent either. I don't have any experience using the PictureBox in VB6. Perhaps Bob was just trying to say "use the right tool for the job"!Salesman
S
1

the picturebox is a control and has an image as source - so you have to draw on the image and hand the image to the control to show it

MyImage = new Bitmap(fileToDisplay);
pictureBox1.ClientSize = new Size(xSize, ySize);
pictureBox1.Image = MyImage;
Sundin answered 28/4, 2010 at 13:34 Comment(2)
This draws into a bitmap, not onto the picturebox. It works but it is somethng altogether different!Laryngotomy
This doesn't answer the poster's question.Pedestal
C
0
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Asssignment
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        { 
            Graphics g = this.CreateGraphics();
            Pen p = new Pen(Color.Blue);
            int radius = 200;
            int x =Width/2;
            int y =Height/2;


            int first_point1 = (int)(Math.Cos(0) * radius + x);
            int first_point2 = (int)(Math.Sin(0) * radius + y);

            Point p1= new Point(first_point1,first_point2);
            for(int i=1;i<500; i++)
            {
                int dx = (int)(Math.Cos(i)*radius+x );
                int dy = (int)(Math.Sin(i)*radius+y );
                Point p2 = new Point(dx, dy);
                g.DrawLine(p, p1, p2);
                p1 = p2;
            }
        }
    }
}
Camel answered 19/5, 2019 at 13:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.