【问题标题】:PictureBox PaintEvent with other methodPictureBox PaintEvent 与其他方法
【发布时间】:2015-02-04 22:01:55
【问题描述】:

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

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

【问题讨论】:

  • 你应该让你的函数以Graphics作为参数。
  • 你的问题没有多大意义。在Paint 方法的处理程序中,您应该 绘制到e.Graphics 实例。但是你想在这里做什么?每次引发PictureBoxPaint 事件时要画一个圆圈吗?是否要将 PictureBox.Image 属性设置为已绘制圆圈的 Bitmap?还有什么?见stackoverflow.com/help/how-to-askstackoverflow.com/help/mcve
  • @PeterDuniho 我想把圆画成位图
  • 好吧,您的 Circle() 方法几乎可以做到这一点。将FillRectangle() 更改为FillCircle(),你会得到一个圆圈。然后只需将该方法的返回值分配给 pictureBox2.Image 属性(例如,在您的 Form 构造函数中)。您可以完全摆脱 Paint 事件处理程序。如果你想要一个真正的答案,你需要发布一个更好的代码示例。见stackoverflow.com/help/mcve

标签: c# paint picturebox


【解决方案1】:

你需要决定你想做什么:

  • 在图像中绘制
  • 在控件上画

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

以下是如何在Control 上绘制

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

以下是如何将PictureBoxImage 绘制

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

这两种绘画方式都是持久的。后者改变为Image的像素,前者不改变。

因此,如果像素被绘制到图像中并且您缩放、拉伸或移动图像,像素将随之而来。绘制到 PictureBox 控件顶部的像素不会这样做!

当然,对于两种绘图方式,您都可以更改所有常用部分,例如绘图命令,可以在DrawEllipsePensBrushes 之前添加一个FillEllipse,以及它们的画笔类型和@987654330 @ 和尺寸。

【讨论】:

    【解决方案2】:
    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-06
      • 2013-02-23
      相关资源
      最近更新 更多