Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在WinC窗体的背景图像中查找鼠标坐标#_C#_Winforms_Paint - Fatal编程技术网

C# 如何在WinC窗体的背景图像中查找鼠标坐标#

C# 如何在WinC窗体的背景图像中查找鼠标坐标#,c#,winforms,paint,C#,Winforms,Paint,我在嵌板上画了一幅插图。图像将被放大和缩小。从缓冲的图形图像中,我如何在图像中找到鼠标位置,而不是在面板中 private void panel2_paint(object sender, PaintEventArgs e) { if (bitmap != null) { float widthZoomed = panel2.Width / Zoom; float heig

我在嵌板上画了一幅插图。图像将被放大和缩小。从缓冲的图形图像中,我如何在图像中找到鼠标位置,而不是在面板中

  private void panel2_paint(object sender, PaintEventArgs e)
       {
         if (bitmap != null)
            {
                float widthZoomed = panel2.Width / Zoom;
                float heigthZoomed = panel2.Height / Zoom;

                if (widthZoomed > 30000.0f)
                {
                    Zoom = panel2.Width / 30000.0f;
                    widthZoomed = 30000.0f;
                }
                if (heigthZoomed > 30000.0f)
                {
                    Zoom = panel2.Height / 30000.0f;
                    heigthZoomed = 30000.0f;
                }


                if (widthZoomed < 2.0f)
                {
                    Zoom = panel2.Width / 2.0f;
                    widthZoomed = 2.0f;
                }
                if (heigthZoomed < 2.0f)
                {
                    Zoom = panel2.Height / 2.0f;
                    heigthZoomed = 2.0f;
                }

                float wz2 = widthZoomed / 2.0f;
                float hz2 = heigthZoomed / 2.0f;
                Rectangle drawRect = new Rectangle(
                    (int)(viewPortCenter.X - wz2),
                    (int)(viewPortCenter.Y - hz2),
                    (int)(widthZoomed),
                    (int)(heigthZoomed));
                drawrecX = drawRect.X;
                drawrecY = drawRect.Y;
                dispwidth = (int)(widthZoomed);
                dispheight = (int)(heigthZoomed);

                myBuffer.Graphics.Clear(Color.White); //Clear the Back buffer
                Console.WriteLine(this.panel2.DisplayRectangle.Width);
                Console.WriteLine(this.panel2.DisplayRectangle.Height);

                myBuffer.Graphics.DrawImage(bitmap, this.panel2.DisplayRectangle, drawRect, GraphicsUnit.Pixel);
                //pictureBox1.Image =
                myBuffer.Render(this.panel2.CreateGraphics());
                //this.toolStripStatusLabel1.Text = "Zoom: " + ((int)(this.Zoom * 100)).ToString() + "%";
            }  
}
但是这个密码没有给我确切的位置。有什么想法吗?

试试这个:


谢谢你的建议。但它不起作用。我的图像大小和面板大小不同,这就是为什么我使用图像与面板大小的比率。使用panel2_MouseWheel事件放大和缩小图像,然后在面板偏移中绘制。还有其他想法吗?@Ebro您似乎只需要将这些值乘以缩放。鼠标搜索的X和Y坐标基于面板的宽度和高度。在我的例子中,图像超出了面板的大小。这是找到确切位置的主要问题。使用panel2_MouseWheel事件放大和缩小图像,然后在面板偏移中绘制“面板偏移”是什么?你必须解释..可能是感兴趣的?你必须对鼠标位置应用相同的变换。对于Matrix类来说,这总是容易得多。您可以直接将其指定给Graphics.Transform属性,使其对绘制有效。再次使用它来映射鼠标位置,使用它的TransformPoints()方法。如果需要,从图像坐标返回鼠标位置也很容易,请使用Matrix.Invert()方法。@HansPassant感谢您的反馈。但我不知道怎么做。你能给我一个示例代码或链接吗?
private void panel2_DoubleClick(object sender, EventArgs e)
        {
            var mouseArgs = (MouseEventArgs)e;
            double Pic_width = dispwidth / panel2.Width;//to find the relative position 
            double Pic_height = dispheight / panel2.Height;
            int xpoint = (int)Pic_width * mouseArgs.X + drawrecX;//drawrecX is the X coordinate from the drawing image
            int ypoint = (int)Pic_height * mouseArgs.Y + drawrecY;

        }
private void panel2_DoubleClick(object sender, EventArgs e)
{        
    var mouseArgs = (MouseEventArgs)e;

    int x = mouseArgs.X - drawrecX;
    int y = mouseArgs.Y - drawrecY;

    var size = ImageSizeWithoutZoom;
    var zoomSize = ImageSizeWithZoom;

    double xPoint = x * size.X / zoomSize.X;
    double yPoint = y * size.Y / zoomSize.Y;
}