在 PictureBox 上绘制线条
我的问题与 Stack Overflow 问题相关在 C# 中使用鼠标点击在图片框上绘制线条,但是当鼠标按钮抬起,绘制的线消失。我该如何解决这个问题?
private void GainBox_MouseDn(object sender, MouseEventArgs e)
{
mouse_dn = true;
}
private void GainBox_MouseMv(object sender, MouseEventArgs e)
{
//Line drawn from lookup table
if (mouse_dn)
{
img = new Bitmap(256, 256);
//Get the coordinates (x, y) for line from lookup table.
for (x = x1; x < x2; x++)
img.SetPixel(x, y, Color.Red);
//Same for y coordinate
}
GainBox.Refresh();
}
private void GainBox_MouseUp(object sender, MouseEventArgs e)
{
mouse_dn = false;
}
My question is related to Stack Overflow question Draw lines on a picturebox using mouse clicks in C#, but when the mouse button is up, the drawn line disappears. How do I fix this?
private void GainBox_MouseDn(object sender, MouseEventArgs e)
{
mouse_dn = true;
}
private void GainBox_MouseMv(object sender, MouseEventArgs e)
{
//Line drawn from lookup table
if (mouse_dn)
{
img = new Bitmap(256, 256);
//Get the coordinates (x, y) for line from lookup table.
for (x = x1; x < x2; x++)
img.SetPixel(x, y, Color.Red);
//Same for y coordinate
}
GainBox.Refresh();
}
private void GainBox_MouseUp(object sender, MouseEventArgs e)
{
mouse_dn = false;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个完整的小程序,它根据点绘制线条(在本例中,它跟随鼠标)。我认为你可以将其改造成你需要的。
您的解决方案中的一个问题是您正在临时位图上绘图,但该位图中的图像永远不会传输到您的
PictureBox
。在此提供的解决方案中,不需要任何额外的位图。Here is a small complete program that does draw lines based on points (in this case, it follows the mouse). I think you can rework that into what you need.
One problem in your solution is that you are drawing on a temporary bitmap, but the image in that bitmap is never transferred to your
PictureBox
. In the solution presented here, there isn't any extra bitmap needed.使用图形对象绘制线,
例如
Use Graphics Object to Drawline
e.g.
gainbox.refresh()
应保留在if (mouse_dn)
子句内。gainbox.refresh()
should stay inside theif (mouse_dn)
clause.