1

There is only one picturebox in my form and I want to drawcircle with a method on this picturebox but I cant do that and not working.The method is:

private Bitmap Circle()
    {
        Bitmap bmp;
        Graphics gfx;
        SolidBrush firca_dis=new SolidBrush(Color.FromArgb(192,0,192));

            bmp = new Bitmap(40, 40);
            gfx = Graphics.FromImage(bmp);
            gfx.FillRectangle(firca_dis, 0, 0, 40, 40);

        return bmp;
    }

Picturebox

 private void pictureBox2_Paint(object sender, PaintEventArgs e)
    {
        Graphics gfx= Graphics.FromImage(Circle());
        gfx=e.Graphics;
    }
Tarık Akyüz
  • 97
  • 2
  • 10
  • You should make your function take `Graphics` as a parameter. – SLaks Dec 06 '14 at 23:40
  • Your question doesn't make much sense. In the handler for the `Paint` method, you should _only_ ever draw to the `e.Graphics` instance. But what do you want to do here? Do you want to draw a circle every time the `PictureBox`'s `Paint` event is raised? Do you want the `PictureBox.Image` property to be set to a `Bitmap` where you've already drawn a circle? Something else? See http://stackoverflow.com/help/how-to-ask and http://stackoverflow.com/help/mcve – Peter Duniho Dec 06 '14 at 23:45
  • @PeterDuniho I want to draw circle as a bitmap – Tarık Akyüz Dec 06 '14 at 23:47
  • Well, your `Circle()` method almost does that. Change `FillRectangle()` to `FillCircle()`, and you'll get a circle instead. Then just assign the return value of that method to the `pictureBox2.Image` property (e.g. in your `Form` constructor). You can get rid of the `Paint` event handler altogether. If you want a real answer, you need to post a better code example. See http://stackoverflow.com/help/mcve – Peter Duniho Dec 07 '14 at 00:00

2 Answers2

6

You need to decide what you want to do:

  • Draw into the Image or
  • draw onto the Control?

Your code is a mix of both, which is why it doesn't work.

Here is how to draw onto the Control:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawEllipse(Pens.Red, new Rectangle(3, 4, 44, 44));
    ..
}

Here is how to draw into the Image of the PictureBox:

void drawIntoImage()
{
    using (Graphics G = Graphics.FromImage(pictureBox1.Image))
    {
        G.DrawEllipse(Pens.Orange, new Rectangle(13, 14, 44, 44));
        ..
    }
    // when done with all drawing you can enforce the display update by calling:
    pictureBox1.Refresh();
}

Both ways to draw are persistent. The latter changes to pixels of the Image, the former doesn't.

So if the pixels are drawn into the Image and you zoom, stretch or shift the Image the pixel will go with it. Pixels drawn onto the Top of the PictureBox control won't do that!

Of course for both ways to draw, you can alter all the usual parts like the drawing command, maybe add a FillEllipse before the DrawEllipse, the Pens and Brushes with their Brush type and Colors and the dimensions.

David Refoua
  • 3,476
  • 3
  • 31
  • 55
TaW
  • 53,122
  • 8
  • 69
  • 111
0
private static void DrawCircle(Graphics gfx)
{    
    SolidBrush firca_dis = new SolidBrush(Color.FromArgb(192, 0, 192));
    Rectangle rec = new Rectangle(0, 0, 40, 40); //Size and location of the Circle

    gfx.FillEllipse(firca_dis, rec); //Draw a Circle and fill it
    gfx.DrawEllipse(new Pen(firca_dis), rec); //draw a the border of the cicle your choice
}
Patrick
  • 736
  • 9
  • 27