【问题标题】:Maintain the aspect ratio of an image?保持图像的纵横比?
【发布时间】:2010-03-10 17:57:00
【问题描述】:
我正在使用图片框显示从服务器接收的图像,但我的问题是紧凑框架中的图片框只有三种尺寸模式
StretchImage、Normal、CenterImage
我得到的图片尺寸通常更大,所以我必须使用 StrethImage 模式。但随后会保持纵横比,因此显示的图像会失真。
那么他们无论如何要摆脱这个问题吗?
【问题讨论】:
标签:
image
windows-mobile
aspect-ratio
【解决方案1】:
我终于找到了我的问题的答案-----
float actualHeight = myImg.Height;
float actualWidth = myImg.Width;
float imgRatio = actualWidth / actualHeight;
float maxRatio = (float)this.Width / this.Height;
if(imgRatio!=maxRatio)
{
if (imgRatio < maxRatio)
{
imgRatio = this.Height / actualHeight;
actualWidth = imgRatio * actualWidth;
actualHeight = this.Height;
}
else
{
imgRatio = this.Width / actualWidth;
actualHeight = imgRatio * actualHeight;
actualWidth = this.Width;
}
}
pictureBox.Size=new Size((int)actualWidth,(int)actualHeight);
pictureBox.Location = new Point((int)((this.Width - actualWidth) / 2), (int)((this.Height - actualHeight) / 2));
但在此之前保持图片框大小模式为stretchImage