图片框的paintEvent与其他方法

问题描述 投票:1回答:2

只有一个在我的PictureBox的形式,我想就这个图片框的方法来画圆,但我不能这样做,而不是working.The方法是:

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;
    }
c# paint picturebox
2个回答
5
投票

你需要决定你想要做什么:

  • 绘制到图像或
  • 画上的控制?

您的代码是两者的混合,这就是为什么它不工作。

下面是如何绘制到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 ..


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
}
© www.soinside.com 2019 - 2024. All rights reserved.