如何根据图像的高度计算图像的比例宽度
本文关键字:图像 计算 高度 何根 高度计 | 更新日期: 2023-09-27 18:24:27
如何根据图像的高度来计算图像的比例宽度?
我的意思是,我们只知道图像的高度。
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;
你需要知道三个中的两个来计算另一个。
- 计算宽度与高度的比率:原始宽度/原始高度
- 将宽度/高度比乘以新的期望高度以获得与新高度相对应的新宽度
如果不知道图像的原始宽度和高度,或者原始图像的宽度/高度比(也称为宽高比),则无法保持原始图像的宽高比。