小编典典

PictureBox PaintEvent与其他方法

c#

我的表单中只有一个图片框,我想在此图片框上用一种方法画圆,但是我不能这样做并且不起作用。该方法是:

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

图片框

 private void pictureBox2_Paint(object sender, PaintEventArgs e)
    {
        Graphics gfx= Graphics.FromImage(Circle());
        gfx=e.Graphics;
    }

阅读 412

收藏
2020-05-19

共1个答案

小编典典

您需要决定要做什么:

  • 入图像
  • 借鉴 控制

您的代码混合了两者,这就是为什么它不起作用的原因。

下面是如何画 Control

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

下面是如何绘制 ImagePictureBox::

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

两种绘制方式都是持久的。后者更改为图像的像素,前者则不更改。

因此,如果将像素绘制到图像中,并且进行缩放,拉伸或移动图像,则像素将随之变化。绘制到PictureBox控件顶部的像素将无法做到这一点!

当然,对于这两种绘制方式,您都可以更改所有常用的零件,例如绘图命令,也可以FillEllipseDrawEllipsePens和之前添加一个,以及Brushes其画笔类型和Colors尺寸。

2020-05-19