保持图像的宽高比? [英] Maintain the aspect ratio of an image?

查看:199
本文介绍了保持图像的宽高比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆