ImageSource Pixel Size

How to determine image size in pixels? The ImageSource object has the Height and Width property, but they return a size of 1/96 inch.

+4
source share
4 answers

You need to multiply the value with a resolution of Windows DPI to get the number of physical pixels. One way to get DPI resolution is to get a Graphics object and read its DpiX and DpiY .

+5
source

There are 2 types of ImageSource: DrawingImage and BitmapSource .

Obviously, DrawingImage does not have a DPI or pixel width, because it is essentially a vector graphic.

BitmapSource , on the other hand, has PixeWidth / PixelHeight as well as DpiX / DpiY.

http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapsource.pixelheight.aspx

+3
source

Super old post, but for those who still have problems with this, you do not need to do anything crazy or complicated.

 (ImageSource.Source as BitmapSource).PixelWidth (ImageSource.Source as BitmapSource).PixelHeight 
+1
source

Borrowing from what I found here I came up with:

Inside the image tag in XAML, do:

 <Image.Resources> <c:StringJoinConverter x:Key="StringJoin" /> </Image.Resources> <Image.Tag> <!-- Get Image actual width & height and store it in the control Tag --> <MultiBinding Converter="{StaticResource StringJoin}"> <Binding RelativeSource="{RelativeSource Self}" Path="Source.PixelWidth" /> <Binding RelativeSource="{RelativeSource Self}" Path="Source.PixelHeight" /> </MultiBinding> </Image.Tag> 

You will need to configure the namespace c at the top of your XAML file for your folder / Converter namespace, for example:

 xmlns:c="clr-namespace:Project.Converters" 

Then create a converter:

 public class StringJoinConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { return string.Join((parameter ?? ",").ToString(), values); } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } 

Then you can extract the actual (pixel) width and height of the image with:

 var tag = imageControl.Tag; // width,height List<double> size = tag.ToString() .Split(',') .Select(d => Convert.ToDouble(d)) .ToList(); double imageWidth = size[0], imageHeight = size[1]; 
0
source

All Articles