user8731365
user8731365

Reputation:

How to get size of image

I want to get size of image and assign to variable. I have no idea to do it

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

Upvotes: 0

Views: 89

Answers (1)

Clemens
Clemens

Reputation: 128013

You should get the size after EndInit.

There is however no need to call BeginInit and EndInit at all:

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

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

Upvotes: 1

Related Questions