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

C# 如何根据图像的高度计算图像的比例宽度?,c#,.net,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)

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

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

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;

已知高度和宽度时:

var ratio = image.Height / image.Width;
在已知高度和比率的情况下:

var width = image.Height / ratio;
var height = image.Width * ratio;
在已知宽度和比率的情况下:

var width = image.Height / ratio;
var height = image.Width * ratio;
你需要知道三个中的两个来计算另一个

  • 计算宽高比:原始宽度/原始高度
  • 将宽高比乘以新的所需高度,得到与新高度对应的新宽度

  • 如果不知道原始图像的原始宽度和高度,或原始图像的宽度/高度比(也称为纵横比),则无法保持原始图像的纵横比。

    计算比例的方法相同…将宽度乘以公共比率。这就是你要问的吗?@KirkWoll是的。你能写出你的答案,包括代码样本吗…@DmitryBoyko那么你怎么可能计算宽度呢?你有其他的输入吗?纵横比?总像素数?呃。。那么图像的宽度有什么问题呢?我完全不同意,关键是在你只知道高度的情况下找出图像的宽度,这对我来说似乎是一个有效的问题。我有高度,图像的宽度,比如var Width=900 var height=800,我试图降低图像保持图像的纵横比HiteshRanaut,然后图像的宽度应始终为高度的1.125倍(高度的9/8)。或者相反,高度应始终为宽度的8/9。如果你需要更多的建议(也可能需要更及时的回应!),那么你最好提出一个新问题,而不是发表评论。最好的,RichHi@Rich已经解决了这个问题。谢谢