![](/img/trans.png)
[英]Getting the real Location of mouse in actual size of image, not the location of mouse in PictureBox
[英]Get real image coordinates from mouse location in PictureBox
在我的Windows窗體中,我有一個PictureBox,從目錄中加載圖像。
我需要在PictureBox中顯示圖像的實際尺寸,例如圖像(寬度= 1024,高度= 768)和圖片框(寬度= 800,高度= 600)。
我想將圖像加載到具有相同像素值的PictureBox中。 因此,當我指向PictureBox中的任何位置時,如果我指向真實圖像(例如使用Photoshop獲取維度),我得到的像素值與我獲得的像素值相同。
嘗試到目前為止但沒有成功:
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
和768 / 600
都是整數除法,產生1
改變操作順序:
MessageBox.Show("X=" + (1024 * me.X / 800) + ", Y=" + (768 * me.Y / 600));
這是完整的方法(假設PictureBox1.SizeMode
設置為StretchImage
)。 使用實際寬度和高度值,而不是“魔術”常數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.