views:

157

answers:

2

Hi,

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

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 ?

A: 

This link would help

Shadow
+1  A: 

hi all,

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

Madhup
hope this will help!
Madhup