gpt4 book ai didi

c# - PictureBox PaintEvent 与其他方法

转载 作者:太空宇宙 更新时间:2023-11-03 20:01:46 25 4
gpt4 key购买 nike

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

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

最佳答案

你需要决定你想做什么:

  • 绘制图像
  • 绘制控件?

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

下面是如何绘制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();
}

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

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

当然对于这两种绘制方式,你可以改变所有常用的部分,比如绘制命令,也许在DrawEllipse之前添加一个FillEllipsePens Brushes 以及它们的 Brush 类型和 Colors 以及尺寸。

关于c# - PictureBox PaintEvent 与其他方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27337825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com