'Get the size of a displayed image inside a WPF ImageControl

I have an image inside an image control of WPF.

How can I get the displayed size of the image inside the imageControl with the height and width converted to IN?



Solution 1:[1]

I tested Tam Bui solution, it does not work properly when the image size is smaller than the image control size.

My solution:

First convert the Image.Source to MemoryStream with ImageSourceToMemoryStream (my method), then convert it to BitmapImage and finally get the height and width of BitmapImage.

Output (tested in Visual Studio 2017, .Net Framework 4.5.2):

I tested it on png and HD jpg images.

PNG:

PNG

HD JPG:

HD JPG

Note: if this solution helps you, please mark as answer (don't forget to vote for me).

XAML:

    <Image x:Name="ImageControl" HorizontalAlignment="Left" Height="113" Margin="56,42,0,0" VerticalAlignment="Top" Width="159"/>
    <Button x:Name="ChooseImage" Content="Choose image" Click="ChooseImage_Click" HorizontalAlignment="Left" Height="25" Margin="392,276,0,0" VerticalAlignment="Top" Width="90"/>
    <Button x:Name="CalculateImageSize" Content="Calculate image size" Click="CalculateImageSize_Click" HorizontalAlignment="Left" Height="25" Margin="257,276,0,0" VerticalAlignment="Top" Width="120"/>

C#:

Note: BitmapEncoder must be set to GifBitmapEncoder to calculate the size of HD images (or higher), otherwise it will not return the correct result.

    int Height = 0;
    int Width = 0;
    public System.IO.MemoryStream ImageSourceToMemoryStream(BitmapEncoder BitEncoder, ImageSource ImgSource)
    {
        System.IO.MemoryStream Stream = null;
        switch ((ImgSource as BitmapSource) != null)
        {
            case true:
                BitEncoder.Frames.Add(BitmapFrame.Create((ImgSource as BitmapSource)));
                Stream = new System.IO.MemoryStream();
                BitEncoder.Save(Stream);
                break;
        }
        return Stream;
    }
    private void ChooseImage_Click(object sender, RoutedEventArgs e)
    {
        System.Windows.Forms.OpenFileDialog OP = new System.Windows.Forms.OpenFileDialog();
        OP.Filter= "All files (*.*)|*.*|JPG (*.jpg)|*.jpg|BMP (*.bmp)|*.bmp|GIF (*.gif)|*.gif|PNG (*.png)|*.png";
        OP.AutoUpgradeEnabled = false;
        if (OP.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            var IMG = System.Drawing.Image.FromFile(OP.FileName);
            BitmapImage BitMapImage = new BitmapImage();
            BitMapImage.BeginInit();
            System.IO.MemoryStream MemoryStream = new System.IO.MemoryStream();
            IMG.Save(MemoryStream, System.Drawing.Imaging.ImageFormat.Png);
            MemoryStream.Seek(0, System.IO.SeekOrigin.Begin);
            BitMapImage.StreamSource = MemoryStream;
            BitMapImage.EndInit();
            ImageControl.Source = BitMapImage;
        }
    }
    private void CalculateImageSize_Click(object sender, RoutedEventArgs e)
    {
        BitmapImage BitMapImage = new BitmapImage();
        BitMapImage.BeginInit();
        //BitmapEncoder must be set to GifBitmapEncoder to calculate the size of HD images (or higher), otherwise it will not return the correct result
        var MemoryStream = ImageSourceToMemoryStream(new GifBitmapEncoder(), ImageControl.Source);
        MemoryStream.Seek(0, System.IO.SeekOrigin.Begin);
        BitMapImage.StreamSource = MemoryStream;
        BitMapImage.EndInit();
        Height = (int)BitMapImage.Height;
        Width = (int)BitMapImage.Width;
        ImageControl.Source = BitMapImage;
        Size DisplayedImageSize = new Size(Width, Height);
        MessageBox.Show("Image control width = " + ImageControl.Width.ToString() + ", Image width = " + Width.ToString() + ", Image control height = " + ImageControl.Height.ToString() + ", Image height = " + Height.ToString());
    }

Thanks

Solution 2:[2]

Size displayedImageSize = new Size((int)imageControl.ActualWidth, (int)imageControl.ActualHeight);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Tam Bui