在 C# 中缩放 GraphicsPath 对象上的矩形或形状时,如何修复鼠标滚轮在图片框刷新上上下滚动之间的延迟?

How to fix the delay between mouse wheel up and down on the picture box refresh while scaling a rectangle or a shape on the GraphicsPath object in C#?

1.I正在尝试缩放矩形,同时用户在图片框上上下移动鼠标。
2.After 向上滚动鼠标滚轮 5 次,如果向下滚动鼠标滚轮,矩形仍然保持放大(扩展)。
3.Any有解决办法吗?

GraphicsPath path=new GraphicsPath();
private float scale=1.0F;
private bool ActiveWheel=false;
public Form1()
{
    path.AddRectangle(new Rectangle(10,10,50,100));
}
private void PictureBox1_Paint(object sender,PaintEventArgs e)
{
    if(ActiveWheel)
    {
        ActiveWheel=false;
        ScaleRectangle(e);

    }
else
{
   e.Graphics.DrawPath(Pens.Red,path);
}

}
private void PictureBox1_MouseWheel(object sender,MouseEventArgs e)
{
    ActiveWheel=true;
    scale=Math.Max(scale+Math.Sign(e.Delta)*0.1F,0.1F);
    pictureBox1.Refresh();
}
}
private void ScaleRectangle(PaintEventArgs e)
{
    var matrix=new Matrix();
    matrix.Scale(scale,scale,MatrixOrder.Append);
    path.Transform(matrix);
    e.Graphics.DrawPath(Pens.Blue,path);
}

任何解决方案或想法如何突然缩小或放大形状而不会在鼠标滚轮上升和鼠标滚轮下降之间出现延迟(请参阅 2. 如果想实际查看 o/p)。

只需在 MouseWheel() 事件中调整比例值,然后在 Paint() 事件中对 GRAPHICS 表面(而不是 Path 本身)进行 ScaleTransform() 并绘制:

public partial class Form1 : Form
{

    private GraphicsPath path = new GraphicsPath();
    private float scale = 1.0F;

    public Form1()
    {
        InitializeComponent();
        path.AddRectangle(new Rectangle(10, 10, 50, 100));
        pictureBox1.MouseWheel += PictureBox1_MouseWheel;
        pictureBox1.Paint += pictureBox1_Paint;
    }

    private void PictureBox1_MouseWheel(object sender, MouseEventArgs e)
    {
        scale = Math.Max(scale + Math.Sign(e.Delta) * 0.1F, 0.1F);
        pictureBox1.Invalidate();
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.ScaleTransform(scale, scale);
        e.Graphics.DrawPath(Pens.Red, path);
    }

}

--- 编辑 ---

It scales entirely, could you show me how to scale only graphics path object and top, left have to be fixed, meaning without scaling the top, left point?

在这种情况下,平移到矩形的左上角,缩放,然后平移回原点。现在绘制未更改的矩形:

    private void PictureBox1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.TranslateTransform(10, 10);
        e.Graphics.ScaleTransform(scale, scale);
        e.Graphics.TranslateTransform(-10, -10);
        e.Graphics.DrawPath(Pens.Red, path);
    }

如果你有其他元素在不同的位置绘制 and/or 比例,那么你可以在前后重置图形表面(每个 "element" 可以做相同类型的事情来定位本身和缩放本身):

    private void PictureBox1_Paint(object sender, PaintEventArgs e)
    {

        // possibly other drawing operations

        e.Graphics.ResetTransform();
        e.Graphics.TranslateTransform(10, 10);
        e.Graphics.ScaleTransform(scale, scale);
        e.Graphics.TranslateTransform(-10, -10);
        e.Graphics.DrawPath(Pens.Red, path);
        e.Graphics.ResetTransform();

        // possibly other drawing operations

    }

这种方法很好,因为它保留了有关矩形的原始信息;变化只是视觉上的。