在 PictureBox 上绘制线条

发布于 2024-10-04 03:46:48 字数 722 浏览 1 评论 0原文

我的问题与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青芜 2024-10-11 03:46:48

这是一个完整的小程序,它根据点绘制线条(在本例中,它跟随鼠标)。我认为你可以将其改造成你需要的。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }


    // Variable that will hold the point from which to draw the next line
    Point latestPoint;


    private void GainBox_MouseDown(object sender, MouseEventArgs e)
    {
        if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
        {
            // Remember the location where the button was pressed
            latestPoint = e.Location;
        }
    }

    private void GainBox_MouseMove(object sender, MouseEventArgs e)
    {
        if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
        {
            using (Graphics g = GainBox.CreateGraphics())
            {
                // Draw next line and...
                g.DrawLine(Pens.Red, latestPoint, e.Location);

                // ... Remember the location
                latestPoint = e.Location;
            }
        }
    }
}

您的解决方案中的一个问题是您正在临时位图上绘图,但该位图中的图像永远不会传输到您的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.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }


    // Variable that will hold the point from which to draw the next line
    Point latestPoint;


    private void GainBox_MouseDown(object sender, MouseEventArgs e)
    {
        if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
        {
            // Remember the location where the button was pressed
            latestPoint = e.Location;
        }
    }

    private void GainBox_MouseMove(object sender, MouseEventArgs e)
    {
        if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
        {
            using (Graphics g = GainBox.CreateGraphics())
            {
                // Draw next line and...
                g.DrawLine(Pens.Red, latestPoint, e.Location);

                // ... Remember the location
                latestPoint = e.Location;
            }
        }
    }
}

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.

标点 2024-10-11 03:46:48

使用图形对象绘制线,

例如

Graphics gfx = GainBox.CreateGraphics();
gfx.Drawline([Your Parameters here]);

Use Graphics Object to Drawline

e.g.

Graphics gfx = GainBox.CreateGraphics();
gfx.Drawline([Your Parameters here]);
柳若烟 2024-10-11 03:46:48

gainbox.refresh() 应保留在 if (mouse_dn) 子句内。

gainbox.refresh() should stay inside the if (mouse_dn) clause.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文