简体   繁体   English

在面板上移动高质量线c#

[英]Moving a high quality line on a panel c#

I want to draw a line on a panel and then move it as the mouse moves. 我想在面板上画一条线,然后在鼠标移动时移动它。 To do so, I draw the line and when the mouse moves I redraw the line to the new location and remove the previous line by drawing a line with the background color on it. 为此,我绘制线条,当鼠标移动时,我将线条重新绘制到新位置,并通过在其上绘制带有背景颜色的线条来移除上一条线条。 It works fine if I do not use the high quality smoothing mode. 如果我不使用高质量的平滑模式,它可以正常工作。 But if use high quality smoothing mode, it leave traces on the panel. 但如果使用高质量的平滑模式,它会在面板上留下痕迹。 Does anybody know how to fix this? 有人知道如何解决这个问题吗? Thank you. 谢谢。 Here is the code 这是代码

        int x_previous = 0;
        int y_previous = 0;

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            Pen pen1 = new System.Drawing.Pen(Color.Black, 3);
            Pen pen2 = new System.Drawing.Pen(panel1.BackColor, 3);
            Graphics g = panel1.CreateGraphics();
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.DrawLine(pen2, new Point(0, 0), new Point(x_previous, y_previous));
            g.DrawLine(pen1, new Point(0, 0), new Point(e.Location.X, e.Location.Y));
            x_previous = e.Location.X;
            y_previous = e.Location.Y;
        }

Here is the snapshot with SmoothingMode 这是使用SmoothingMode的快照

结果快照

Here is the snapshot without SmoothingMode 这是没有SmoothingMode的快照

结果快照

Instead of drawing a line over a line, the safest option would be to clear the graphics using g.Clear(panel1.BackColor) . 而不是在一条线上画一条线,最安全的选择是使用g.Clear(panel1.BackColor)清除图形。 This will wipe everything off that has been drawn, so that you can safely draw a new line: 这将擦除已绘制的所有内容,以便您可以安全地绘制新行:

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
    Pen pen1 = new System.Drawing.Pen(Color.Black, 3);
    Pen pen2 = new System.Drawing.Pen(panel1.BackColor, 3);
    Graphics g = panel1.CreateGraphics();
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    // Clear the graphics, creating a blank area to draw on
    g.Clear(panel1.BackColor);
    g.DrawLine(pen1, new Point(0, 0), new Point(e.Location.X, e.Location.Y));
    x_previous = e.Location.X;
    y_previous = e.Location.Y;
}

Hope this helps! 希望这可以帮助!

Instead of drawing the line in the event handler for the mouse movement you should use it to Invalidate the panel and perform the line drawing in a handler for its Paint event. 您不应在事件处理程序中为鼠标移动绘制线条,而应使用它来使面板无效并在其Paint事件的处理程序中执行线条绘制。 There will be no need to erase the old line. 没有必要删除旧行。

I haven't used WinForms in a while, so you'll have to forgive me if this doesn't work. 我有一段时间没有使用WinForms,所以如果这不起作用,你将不得不原谅我。

I assume that anti-aliasing has slightly blurred the edges of the line so that the initial line extends slightly further out than the width of the pen. 我假设抗锯齿略微模糊了线的边缘,因此初始线比笔的宽度稍微延伸得更远。 That also means that the edges of the white pen won't be completely opaque when drawing over the black line. 这也意味着当画在黑线上时,白笔的边缘不会完全不透明。

Try increasing the width of the white pen until it completely covers the black, and maybe see if you can leave the Graphics object with lower quality rendering for the white pen, and use the smoothing only for the black pen. 尝试增加白笔的宽度,直到它完全覆盖黑色,并且可能会看到是否可以使用较低质量的白色笔渲染图形对象,并仅使用黑色笔的平滑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM