C# 获取指定固定范围内的比例固定的实际图片大小

Rectangle? getImageRegion(PictureBox pic)
        {
            if (pic.Image == null)
                return null;

            var picHeight = pic.Width * pic.Image.Height / pic.Image.Width;
            if (picHeight > pic.Height)
            {
                var imageHeight = pic.Height;
                var imageWidth = imageHeight * pic.Image.Width / pic.Image.Height;
                return new Rectangle((pic.Width - imageWidth) / 2, (pic.Height - imageHeight) / 2, imageWidth, imageHeight);
            }
            else
            {
                var imageWidth = pic.Width;
                var imageHeight = imageWidth * pic.Image.Height / pic.Image.Width;
                return new Rectangle((pic.Width - imageWidth) / 2, (pic.Height - imageHeight) / 2, imageWidth, imageHeight);
            }
        }

 

posted on 2022-12-14 18:23  空明流光  阅读(277)  评论(0编辑  收藏  举报

导航