Maintain the aspect ratio of an image?

后端 未结 1 1927
后悔当初
后悔当初 2021-01-25 07:41

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, Norm

相关标签:
1条回答
  • 2021-01-25 08:24

    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));
    

    but before doing this keep the picture box size mode as stretchImage

    0 讨论(0)
提交回复
热议问题