如何根据图像的高度计算图像的比例宽度?

时间:2012-02-14 17:38:04

标签: c# .net

如何根据图像的高度计算比例宽度

我的意思是我们只知道图像高度。

string pathToImage = System.IO.Path.Combine(Settings.ContentFolderPath, file);

Image image = new Image();
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(pathToImage, UriKind.Absolute);
src.EndInit();
image.Source = src;
image.Stretch = Stretch.Uniform;
image.Height = canvas1.Height;  
image.Width = ???; 

谢谢!


更新(1)

感谢所有帮助过我的人。

请愚蠢的人不要投票......

更新(2)

解决方案:

string pathToImage = System.IO.Path.Combine(Settings.ContentFolderPath, file);

Image image = new Image();
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(pathToImage, UriKind.Absolute);
src.EndInit();
double ratio = src.Width / src.Height; 
image.Source = src;
image.Stretch = Stretch.Uniform;
image.Height = canvas1.Height;  
image.Width = canvas1.Height * ratio;

2 个答案:

答案 0 :(得分:22)

已知高度和宽度:

var ratio = image.Height / image.Width;

已知身高和比例:

var width = image.Height / ratio;

已知宽度和比例:

var height = image.Width * ratio;

你需要知道三个中的两个来计算另一个。

答案 1 :(得分:6)

  1. 计算宽高比:原始宽度/原始高度
  2. 将宽度/高度比乘以新的所需高度,以获得与新高度相对应的新宽度。
  3. 如果不知道图像的原始宽度和高度,或原始图像的宽高比(也称为高宽比),则无法保持原始图像的宽高比。

相关问题