C# 如何在GDI+;

C# 如何在GDI+;,c#,graphics,gdi+,scale,picturebox,C#,Graphics,Gdi+,Scale,Picturebox,我有一个画框。 在图片框中,我使用以下方法绘制线条: gfx.drawine(nPen,line.xBegin,line.yBegin,line.xEnd,line.yEnd) line是一个包含行的起始值和结束值的对象 我在那个画框上画了10条线。 我的鼠标滚轮侦听器包含以下代码: gfx.TranslateTransform((panelpreview.Width) / 2, (panelpreview.Height) / 2); gfx.ScaleTransform(imageScale,

我有一个画框。 在图片框中,我使用以下方法绘制线条:

gfx.drawine(nPen,line.xBegin,line.yBegin,line.xEnd,line.yEnd)

line
是一个包含行的起始值和结束值的对象

我在那个画框上画了10条线。 我的鼠标滚轮侦听器包含以下代码:

gfx.TranslateTransform((panelpreview.Width) / 2, (panelpreview.Height) / 2);
gfx.ScaleTransform(imageScale, imageScale);
gfx.TranslateTransform(-(panelpreview.Width) / 2, -(panelpreview.Height) / 2);
目前,我正在尝试通过以下方式移动线:

    private void panelPreview_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            checkLinesIntersection(e.Location); //checks if e.Location intersects with the line
            panelPreview.MouseUp += new MouseEventHandler(panelPreview_MouseUpLine);
            panelPreview.MouseMove += new MouseEventHandler(panelPreview_MouseMoveLine);
        }
    }

    void panelPreview_MouseMoveLine(object sender, MouseEventArgs e)
    {
        if (_Leading.movable)
            newFont.Leading = e.Y - (_base.yBegin + newFont._descenderHeight);

        if (_xHeight.movable)
            if (_base.yBegin - e.Y >= 0)
                newFont.xHeight = _base.yBegin - e.Y;

        if (_capHeight.movable)
            if (_base.yBegin - e.Y >= 0)
                newFont.capHeight = _base.yBegin - e.Y;

        if (_ascenderHeight.movable)
            if (_base.yBegin - e.Y >= 0)
                newFont.ascenderHeight = _base.yBegin - e.Y;

        if (_descenderHeight.movable)
            if (e.Y - _base.yBegin >= 0)
                newFont.descenderHeight = e.Y - _base.yBegin;

        UpdatePreviewWindow();
    }

    void panelPreview_MouseUpLine(object sender, MouseEventArgs e)
    {
        panelPreview.MouseMove -= new MouseEventHandler(panelPreview_MouseMoveLine);
        panelPreview.MouseUp -= new MouseEventHandler(panelPreview_MouseUpLine);
    }
问题是,在放大后,即使在视觉上我确实按下了线,它也没有做出应有的反应

    public void checkLinesIntersection(Point mouseLocation)
    {
        lineIntersects(_Leading, mouseLocation);
        lineIntersects(_xHeight, mouseLocation);
    }

    private void lineIntersects(nLine line, Point mouseLocation)
    {
        if (mouseLocation.X >= line.xBegin && mouseLocation.X <= line.xEnd)
            if (mouseLocation.Y >= line.yBegin || mouseLocation.Y + 2 >= line.yBegin)
                if (mouseLocation.Y <= line.yEnd || mouseLocation.Y - 2 <= line.yEnd)
                    switch (line.sName)
                    {
                        case "xHeight":
                            newFont.selectedLine = nFont.Lines._xHeight;
                            break;
                        default:
                            break;
                    }
    }
public void checkLinesIntersection(点鼠标定位)
{
线相交(_前导,鼠标定位);
线相交(_xHeight,鼠标定位);
}
专用空心线相交(线、点鼠标定位)
{
if(mouseLocation.X>=line.xBegin&&mouseLocation.X=line.yBegin | | | mouseLocation.Y+2>=line.yBegin)

if(mouseLocation.YSo!在这个工具上工作了一段时间后,我改变了方法,使用XNA处理图形(绘制、缩放、移动绘制对象)。 在XNA中使用
Camera
Matrix
进行变换对我帮助很大。 无论如何!我不会发布我的XNA解决方案来处理这个问题,但是通过使用XNA,我在WinForms中找到了处理这个问题的方法

float Zoom = 1f;
Microsoft.Xna.Framework.Rectangle mouseRect;
Matrix inverse;

void TAB_PICTURE_BOX_MouseWheel(object sender, MouseEventArgs e)
    {
        try
        {
            if (e.Delta > 0)
            {
                Zoom+= .1f;
            }
            if (e.Delta < 0)
            {
                Zoom -= .1f;
            }

            gfx.ResetTransform();
            gfx.TranslateTransform((Width + img.Width) / 2, (Height - img.Height) / 2);
            gfx.ScaleTransform(Zoom, Zoom);
            gfx.TranslateTransform(-(Width + img.Width) / 2, -(Height - img.Height) / 2);      
                getInversePosition(e);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

我确信有一种方法可以在没有XNA参与的情况下处理它,但在我的场景中,XNA成功了。

我的水晶球看不到复选框Intersion()认为不需要复选框;)已将它们添加到主postWell,无法工作,缩放后,鼠标坐标不再与线坐标相同。在测试命中之前,您必须将线坐标映射到鼠标坐标。或者反过来。我认为这可能是处理方法。尽管现在的问题是如何映射它们?无法缩放坐标…有没有一种方法可以使线对象在鼠标缩放时与线对象相交时做出反应?
 private void getInversePosition(MouseEventArgs e)
    {
        Matrix matrix = Matrix.CreateTranslation(new Vector3(-(Width + img.Width) / 2, -(Height - img.Height) / 2, 0)) *
                  Matrix.CreateScale(new Vector3(Zoom, Zoom, 1)) *
                  Matrix.CreateTranslation(new Vector3((Width + img.Width) / 2, (Height - img.Height) / 2, 0));

        inverse = Matrix.Invert(matrix);
        Vector2 mousePosition = Vector2.Transform(new Vector2(e.X, e.Y), inverse);
        mouseRect = new Microsoft.Xna.Framework.Rectangle((int)mousePosition.X, (int)mousePosition.Y, 1, 1);
    }