Ejemplo n.º 1
0
        /// <summary>
        /// PictureBox (pb) 上の座標から、pb.Image 上の座標に変換する。
        /// pb.SizeMode == PictureBoxSizeMode.Zoom であること。
        /// </summary>
        /// <param name="pb"></param>
        /// <param name="pbPt"></param>
        /// <param name="toRangeFlag"></param>
        /// <returns>null == 枠外 || 画像が設定されていない。</returns>
        public static XYPoint getPictureBoxPointToImagePoint(PictureBox pb, XYPoint pbPt, bool toRangeFlag = false)
        {
            Image img = pb.Image;

            if (img == null)
            {
                return(null);
            }

            double w        = (double)img.Width;
            double h        = (double)img.Height;
            double screen_w = (double)pb.Width;
            double screen_h = (double)pb.Height;

            Rect imgRect    = new Rect(w, h);
            Rect screenRect = new Rect(0, 0, screen_w, screen_h);

            imgRect.adjustInside(screenRect);

            double x = pbPt.x;
            double y = pbPt.y;

            x -= imgRect.l;
            y -= imgRect.t;
            x /= imgRect.w;
            y /= imgRect.h;

            if (toRangeFlag)
            {
                x = DoubleTools.toRange(x, 0.0, 1.0);
                y = DoubleTools.toRange(y, 0.0, 1.0);
            }
            else
            {
                if (DoubleTools.isRange(x, 0.0, 1.0) == false)
                {
                    return(null);
                }

                if (DoubleTools.isRange(y, 0.0, 1.0) == false)
                {
                    return(null);
                }
            }
            x *= img.Width;
            y *= img.Height;

            return(new XYPoint(x, y));
        }