颜色对话框没有显示
问题描述:
我有一个小应用程序,看起来非常像Windows中的旧Paint。我使用图片框Paint事件实现了所有的Graphic。唯一的问题是,当我点击这个按钮时,应该出现一个颜色对话框,让我改变我的笔的颜色。但每当我点击框从未出现的按钮和我的程序卡住Paint事件,最精确的线,我做到以下几点:颜色对话框没有显示
pictureBox1.Image = bmp;
我做错了吗?请,我会很感激任何帮助!
我希望下面的代码片段就足够了。
这是我的PictureBox的Paint事件:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
using(Graphics g = Graphics.FromImage(bmp))
{
if (lineButton && mouseIsUp)
{
g.DrawLine(myPen, mAnchorPoint, mFinalPoint);
mAnchorPoint = Point.Empty;
mFinalPoint = Point.Empty;
}
pictureBox1.Image = bmp;
}
}
这是当我想改变我的笔的颜色,会启动这个按钮事件:
private void ColorButton_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
if (cd.ShowDialog() == DialogResult.OK)
{
myPen.Color = cd.Color;
}
}
答
不要使用pictureBox1。 Image = bmp;里面pictureBox1_Paint。相反:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
using(Graphics g = Graphics.FromImage(bmp))
{
if (lineButton && mouseIsUp)
{
g.DrawLine(myPen, mAnchorPoint, mFinalPoint);
mAnchorPoint = Point.Empty;
mFinalPoint = Point.Empty;
}
//pictureBox1.Image = bmp;
}
e.Graphics.DrawImage(bmp, 0, 0);
}
或者,更好的方法,设置pictureBox1.Image = BMP;一次,在bmp(不在pictureBox1_Paint)上执行所有绘图并最终调用pictureBox1.Invalidate();。您无需在pictureBox1_Paint()中编写代码。
* pictureBox1.Image = bmp; * calls * pictureBox1_Paint *,一个无止境的循环。 – 2014-10-04 12:44:38