C#实现在picturebox中每单击鼠标在该位置画一个矩形,下次单击鼠标在新位置又画个矩形,但之前的矩形要消失
1个回答
展开全部
在PictureBox的MouseDown事件里,处理绘制的逻辑,以下是我的测试代码,希望对你有帮助
void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (null == pictureBox1.BackgroundImage)
{
pictureBox1.BackgroundImage = new Bitmap(pictureBox1.Width, pictureBox1.Height);
}
using (Graphics g = Graphics.FromImage(pictureBox1.BackgroundImage))
{
g.Clear(pictureBox1.BackColor);
g.DrawRectangle(Pens.Red, e.X, e.Y, 100, 50);
g.Save();
pictureBox1.Invalidate();
}
}
void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (null == pictureBox1.BackgroundImage)
{
pictureBox1.BackgroundImage = new Bitmap(pictureBox1.Width, pictureBox1.Height);
}
using (Graphics g = Graphics.FromImage(pictureBox1.BackgroundImage))
{
g.Clear(pictureBox1.BackColor);
g.DrawRectangle(Pens.Red, e.X, e.Y, 100, 50);
g.Save();
pictureBox1.Invalidate();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询