-1

I have a PictureBox control. I want to put an image in this PictureBox.

This I did:

pictureBox1.Image = Image.FromFile(@"D:\test.jpg");

I don't want the image to fill the entire PictureBox.

Next, I want to draw graphics on the PictureBox which I do using the following code:

Graphics g = pictureBox1.CreateGraphics();

g.DrawArc(....);
g.DrawLine(....);

It should be something as shown in the following picture:

enter image description here

In the above picture, the image should be only in the bounds of the blue rectangle, around which I want to draw the graphics. How to draw an image?

Abhishek
  • 2,925
  • 4
  • 34
  • 59
  • 6
    So what is your question? – Harry Aug 13 '15 at 09:23
  • Take a look at this example : https://kishordgupta.wordpress.com/2011/02/18/c-tips-how-to-draw-on-a-picturebox-image-using-mouse-by-c/ – Fabjan Aug 13 '15 at 09:32
  • Image [size](https://msdn.microsoft.com/en-us/library/system.drawing.image.size.aspx) defines the *bounds*. – Sinatr Aug 13 '15 at 09:34
  • Use `SizeMode = CenterImage`. To draw __onto__ the surface use the `Paint` event. To draw __into__ the Image see [here](http://stackoverflow.com/questions/27337825/picturebox-paintevent-with-other-method/27341797?s=3|0.0377#27341797) - To combine resizing the Image and drawing into it use a `DrawImage` overload with two rectangles in the`Paint` event! – TaW Aug 13 '15 at 09:34

1 Answers1

2

You can use the Paint event or by creating Graphics object as you are doing and then draw circle and line as below:
Example code snippet:

   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  );
    }

and you can draw image using below methods:

g.DrawImage(image, new Rectangle(10, 10, 200, 200));

Reference these threads:
how to draw drawings in picture box
How do I draw a circle and line in the picturebox?
Drawing on picture box images
FAQ: How do I draw an image respectively on the PictureBox control and Image object?

Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75