如何在C#中调整保持纵横比的图像大小

时间:2014-10-21 12:24:34

标签: c# image

我需要知道一种方法来调整图像大小以适应盒子而不会使图像拉伸太多。盒子设置了宽度和高度,我希望图像尽可能多地填充盒子,但保持它原来的宽高比。

4 个答案:

答案 0 :(得分:10)

//calculate the ratio
double dbl = (double)image.Width / (double)image.Height;

//set height of image to boxHeight and check if resulting width is less than boxWidth, 
//else set width of image to boxWidth and calculate new height
if( (int)((double)boxHeight * dbl) <= boxWidth )
{
    resizedImage = new Bitmap(original, (int)((double)boxHeight * dbl), boxHeight);
}
else
{
    resizedImage = new Bitmap(original, boxWidth, (int)((double)boxWidth / dbl) );
}

使用相同比率缩放的公式为:

newWidth =  (int)((double)boxHeight * dbl)

or

newHeight =  (int)((double)boxWidth / dbl)

答案 1 :(得分:1)

 Bitmap original,resizedImage;
try
                {

                    using (FileStream fs = new System.IO.FileStream(imageLabel.Text, System.IO.FileMode.Open))
                    {

                        original = new Bitmap(fs);
                    }

                    int rectHeight = BOXWIDTH;
                    int rectWidth = BOXWIDTH;
                    //if the image is squared set it's height and width to the smallest of the desired dimensions (our box). In the current example rectHeight<rectWidth
                    if (original.Height == original.Width)
                    {
                        resizedImage = new Bitmap(original, rectHeight, rectHeight);
                    }
                    else
                    {
                        //calculate aspect ratio
                        float aspect = original.Width / (float)original.Height;
                        int newWidth, newHeight;
                        //calculate new dimensions based on aspect ratio
                        newWidth = (int)(rectWidth * aspect);
                        newHeight = (int)(newWidth / aspect);
                        //if one of the two dimensions exceed the box dimensions
                        if (newWidth > rectWidth || newHeight > rectHeight)
                        {
                           //depending on which of the two exceeds the box dimensions set it as the box dimension and calculate the other one based on the aspect ratio
                            if (newWidth > newHeight)
                            {
                                newWidth = rectWidth;
                                newHeight = (int)(newWidth / aspect);

                            }
                            else
                            {
                                newHeight = rectHeight;
                                newWidth = (int)(newHeight * aspect);

                            }
                        }
                        resizedImage = new Bitmap(original, newWidth, newHeight);



                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show( ex.Message);
                }
            }

答案 2 :(得分:0)

Math.Max 可以简化问题:

double ratio = Math.Max((double)image.width / (double)box.width , (double)image.height / (double)box.height);
image.width = (int)(image.width / ratio);
image.height = (int)(image.height / ratio);

答案 3 :(得分:-1)

我相信如果你只改变高度或只改变宽度,它将保持更好的比例,宽度/高度将随之改变。所以你可以试试