我正在使用 pictureBox 来显示从服务器接收的图像,但我的问题是紧凑框架中的图片框只有三种尺寸模式
拉伸图像、正常、中心图像
我收到的图片通常尺寸较大,因此我必须使用 StrecthImage 模式。但随后纵横比保持不变,因此显示的图像会变形。
那么他们无论如何都能摆脱这个问题吗?
最后我在这里找到了我的问题的答案-----
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)