私は今日、これと同じ問題を解決しなければなりませんでした。幅と高さの比率が任意の画像で機能するようにしたかったのです。
これが、元のフルサイズの画像でポイント「unscaled_p」を見つけるための私の方法です。
Point p = pictureBox1.PointToClient(Cursor.Position);
Point unscaled_p = new Point();
// image and container dimensions
int w_i = pictureBox1.Image.Width;
int h_i = pictureBox1.Image.Height;
int w_c = pictureBox1.Width;
int h_c = pictureBox1.Height;
最初の秘訣は、画像がコンテナに対して水平方向に大きいか垂直方向に大きいかを判断することです。これにより、どの画像の寸法がコンテナを完全に満たすかがわかります。
float imageRatio = w_i / (float)h_i; // image W:H ratio
float containerRatio = w_c / (float)h_c; // container W:H ratio
if (imageRatio >= containerRatio)
{
// horizontal image
float scaleFactor = w_c / (float)w_i;
float scaledHeight = h_i * scaleFactor;
// calculate gap between top of container and top of image
float filler = Math.Abs(h_c - scaledHeight) / 2;
unscaled_p.X = (int)(p.X / scaleFactor);
unscaled_p.Y = (int)((p.Y - filler) / scaleFactor);
}
else
{
// vertical image
float scaleFactor = h_c / (float)h_i;
float scaledWidth = w_i * scaleFactor;
float filler = Math.Abs(w_c - scaledWidth) / 2;
unscaled_p.X = (int)((p.X - filler) / scaleFactor);
unscaled_p.Y = (int)(p.Y / scaleFactor);
}
return unscaled_p;
ズームは画像を中央に配置するため、画像で塗りつぶされていない寸法を決定するには、「フィラー」の長さを考慮に入れる必要があることに注意してください。結果の「unscaled_p」は、「p」が相関するスケーリングされていない画像上の点です。
お役に立てば幸いです。