gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:30:26 27 4
gpt4 key购买 nike

我想在面板上画一条线,然后随着鼠标的移动而移动。为此,我绘制了一条线,当鼠标移动时,我将线重新绘制到新位置,并通过绘制一 strip 有背景颜色的线来删除上一条线。如果我不使用高质量平滑模式,它工作正常。但是如果使用高质量的平滑模式,它会在面板上留下痕迹。有人知道如何解决这个问题吗?谢谢你。这是代码

        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;
}

这是使用 SmoothingMode 的快照

Snapshot of the results

这是没有 SmoothingMode 的快照

Snapshot of the results

最佳答案

最安全的选择是使用 g.Clear(panel1.BackColor) 清除图形,而不是在一条线上画一条线。这将清除已绘制的所有内容,以便您可以安全地绘制一条新线:

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;
}

希望这对您有所帮助!

关于c# - 在面板上移动高质量的线 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13338146/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com