我想获取图像的大小并分配给变量。我不知道要这么做
OpenFile = new OpenFileDialog();
OpenFile.Filter = "Image Files (*.bmp;*.png;*.jpg)|*.bmp;*.png;*.jpg";
if (OpenFile.ShowDialog() == true)
{
ImagePath = string.Format($"{OpenFile.FileName}");
}
BitmapImage image = new BitmapImage();
image.UriSource = new Uri(OpenFile.FileName);
image.BeginInit();
WindowWidth = image.Width;
WindowHeight = image.Height;
image.EndInit();
答案 0 :(得分:1)
您应该在EndInit
之后得到大小。
但是根本不需要调用BeginInit
和EndInit
:
var image = new BitmapImage(new Uri(OpenFile.FileName));
WindowWidth = image.Width;
WindowHeight = image.Height;