2009-10-13 111 views
1

我使用图片框来显示其从服务器接收到的图像,但我的问题是,在紧凑的框架,图片框只有三个尺寸模式保持图像的宽高比?

StretchImage,正常,CenterImage

我得到的图片一般都是大在大小,所以我不得不使用StrecthImage模式。但是,然后保持宽高比,以便显示的图像变形。

他们无论如何都是出于这个问题?

+1

我想你的意思是宽高比“没有维护“,对吗? – 2009-10-13 06:44:31

回答

3

终于让我找到我的问题的答案是这里-----

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

+0

希望这会有所帮助! – 2009-10-13 11:36:17