获取 WPF ImageControl 中显示图像的大小
Get the size of a displayed image inside a WPF ImageControl
我在 WPF 的图像控件中有一个图像。
如何获取imageControl中图片的高宽转换为IN后的显示大小?
Size displayedImageSize = new Size((int)imageControl.ActualWidth, (int)imageControl.ActualHeight);
我测试了Tam Bui
解决方案,当图像尺寸小于图像控件尺寸时它不能正常工作。
我的解决方案:
先用ImageSourceToMemoryStream
(我的方法)把Image.Source
转成MemoryStream
,再转成BitmapImage
,最后得到[=16的高和宽=].
输出(在 Visual Studio 2017 中测试,.Net Framework 4.5.2):
我在 png 和高清 jpg 图像上测试了它。
PNG:
高清 JPG:
注意:如果此解决方案对您有帮助,请标记为答案(别忘了给我投票)。
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#:
注意:BitmapEncoder
必须设置为GifBitmapEncoder
才能计算高清图片的尺寸(或更高),否则不会return得到正确的结果。
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());
}
谢谢
我在 WPF 的图像控件中有一个图像。
如何获取imageControl中图片的高宽转换为IN后的显示大小?
Size displayedImageSize = new Size((int)imageControl.ActualWidth, (int)imageControl.ActualHeight);
我测试了Tam Bui
解决方案,当图像尺寸小于图像控件尺寸时它不能正常工作。
我的解决方案:
先用ImageSourceToMemoryStream
(我的方法)把Image.Source
转成MemoryStream
,再转成BitmapImage
,最后得到[=16的高和宽=].
输出(在 Visual Studio 2017 中测试,.Net Framework 4.5.2):
我在 png 和高清 jpg 图像上测试了它。
PNG:
高清 JPG:
注意:如果此解决方案对您有帮助,请标记为答案(别忘了给我投票)。
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#:
注意:BitmapEncoder
必须设置为GifBitmapEncoder
才能计算高清图片的尺寸(或更高),否则不会return得到正确的结果。
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());
}
谢谢