C# 獲取PictureBox的SizeMode爲Zoom圖形的大小

在使用 pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;

 

可以通過反射ImageRectangle 屬性獲取最後顯示的大小..

 

方法

 

/// <summary>
        /// 獲取PictureBox在Zoom下顯示的位置和大小
        /// </summary>
        /// <param name="p_PictureBox">Picture 如果沒有圖形或則非ZOOM模式 返回的是PictureBox的大小</param>
        /// <returns>如果p_PictureBox爲null 返回 Rectangle(0, 0, 0, 0)</returns>
        public Rectangle GetPictureBoxZoomSize(PictureBox p_PictureBox)
        {
            if (p_PictureBox != null)
            {
                PropertyInfo _ImageRectanglePropert = p_PictureBox.GetType().GetProperty("ImageRectangle", BindingFlags.Instance | BindingFlags.NonPublic);

                return (Rectangle)_ImageRectanglePropert.GetValue(p_PictureBox, null);
            }
            return new Rectangle(0, 0, 0, 0);
        }

發佈了95 篇原創文章 · 獲贊 17 · 訪問量 56萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章