保持图像的宽高比? [英] Maintain the aspect ratio of an image?
问题描述
我使用pictureBox来显示从服务器接收的图像,但我的问题是紧凑框架中的图片框只有三种尺寸模式
I am using pictureBox to show images which are received from server but my problem is that picture box in compact framework has only three Size Modes
StretchImage,Normal,CenterImage
StretchImage, Normal, CenterImage
我得到的图片一般都比较大,所以我必须使用StrecthImage模式。但是然后保持纵横比,使得显示的图像变得扭曲。
the pictures i am getting are generally bigger in size so i have to use StrecthImage mode. But then the aspect ratio is maintained so the images shown become distorted.
他们无论如何都要解决这个问题吗?
So is their anyway to come out of this problem ?
推荐答案
最后我找到了我的问题的答案-----
finally i found answer for my question which is here-----
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
but before doing this keep the picture box size mode as stretchImage
这篇关于保持图像的宽高比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!