[英]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 / 800
和768 / 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.