简体   繁体   English

从PictureBox中的鼠标位置获取真实的图像坐标

[英]Get real image coordinates from mouse location in PictureBox

In my Windows form I have a PictureBox which image is loaded from a directory. 在我的Windows窗体中,我有一个PictureBox,从目录中加载图像。

I need to display the real dimension of the image into the PictureBox, for example the image (width=1024,height=768), and the picturebox (width=800, height=600). 我需要在PictureBox中显示图像的实际尺寸,例如图像(宽度= 1024,高度= 768)和图片框(宽度= 800,高度= 600)。

I want to load the image into the PictureBox with same pixel values. 我想将图像加载到具有相同像素值的PictureBox中。 So that when I point anywhere in PictureBox I get the pixel value same with the pixel value that I get if I point to the real image (example get dimension using Photoshop). 因此,当我指向PictureBox中的任何位置时,如果我指向真实图像(例如使用Photoshop获取维度),我得到的像素值与我获得的像素值相同。

Tried so far but no success: 尝试到目前为止但没有成功:

private void PictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    MouseEventArgs me = (MouseEventArgs)e;
    Bitmap b = new Bitmap(PictureBox1.Image);
    MessageBox.Show("X=" + (1024/ 800) * me.X + ", Y=" + (768/ 600) *me.Y);
}     

1024 / 800 and 768 / 600 are both integer division which produce 1 1024 / 800768 / 600都是整数除法,产生1

change order of operations: 改变操作顺序:

MessageBox.Show("X=" + (1024 * me.X / 800)  + ", Y=" + (768 * me.Y / 600));

Here is complete method (assuming PictureBox1.SizeMode is set to StretchImage ). 这是完整的方法(假设PictureBox1.SizeMode设置为StretchImage )。 Use real width and height values, not "magic" constants 1024x768 or 800x600 使用实际宽度和高度值,而不是“魔术”常数1024x768或800x600

private void PictureBox1_MouseDown(object sender, MouseEventArgs me)
{            
    Image b = PictureBox1.Image;
    int x = b.Width * me.X / PictureBox1.Width;
    int y = b.Height * me.Y / PictureBox1.Height;
    MessageBox.Show(String.Format("X={0}, Y={1}", x, y));
}

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

相关问题 获取真实的鼠标在图像的实际大小中的位置,而不是PictureBox中鼠标的位置 - Getting the real Location of mouse in actual size of image, not the location of mouse in PictureBox 试图找到鼠标的位置,以便我可以获取图片框数组的坐标 - Trying to find location of mouse so I can get the coordinates of the picturebox array 如何从图片框获取图像坐标(点),而不考虑平移和缩放 - How to get the image coordinates(points) from a picturebox irrespective of panning and zooming 如何从PictureBox获取真实图像像素点x,y - How to get real image pixel point x,y from PictureBox 在pictureBox图像上绘制的坐标 - Coordinates drawing on pictureBox Image 鼠标位置的动态坐标 - Dynamic coordinates for mouse location 如何在鼠标移动事件中实时在pictureBox1中的图像上绘制红色像素? - How to draw pixels in color red on the image in pictureBox1 in real time on mouse move event? 如何在Picturebox中获取光标在图像上的确切位置 - How can I get the exact location of cursor on image in Picturebox 如何从PictureBox中的图像获取位置 - How to get position from Image in PictureBox 如何从zip中设置图片框图片的位置而无需提取? - how to set picturebox image location from zip without extrat?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM