将图形和图像添加到 PictureBox

Adding graphics and image to a PictureBox

我有一个 PictureBox 控件。我想在 PictureBox.

中放一张图片

这是我做的:

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

我不想让图片占满整个PictureBox

接下来,我想在 PictureBox 上使用以下代码绘制图形:

Graphics g = pictureBox1.CreateGraphics();

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

应该是下图所示的东西:

在上图中,图像应该只在蓝色矩形的边界内,我想围绕它绘制图形。如何绘制图像?

您可以使用 Paint 事件或通过创建 Graphics 对象来绘制圆和线,如下所示:
示例代码片段:

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

您可以使用以下方法绘制图像:

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

引用这些线程:
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?