如何获得图像尺寸

时间:2020-05-30 15:37:31

标签: c# wpf

我想获取图像的大小并分配给变量。我不知道要这么做

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();

1 个答案:

答案 0 :(得分:1)

您应该在EndInit之后得到大小。

但是根本不需要调用BeginInitEndInit

var image = new BitmapImage(new Uri(OpenFile.FileName));

WindowWidth = image.Width;
WindowHeight = image.Height;