我在 WPF 的影像控制元件中有一個影像。
如何在將高度和寬度轉換為 IN 的情況下獲取 imageControl 內影像的顯示大小?
uj5u.com熱心網友回復:
我測驗Tam Bui了解決方案,當影像尺寸小于影像控制元件尺寸時,它無法正常作業。
我的解決方案:
首先用(我的方法)轉換Image.Source成,然后轉換成,最后得到.的高度和寬度。MemoryStreamImageSourceToMemoryStreamBitmapImageBitmapImage
輸出(在 Visual Studio 2017、.Net Framework 4.5.2 中測驗):
我在 png 和 HD 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#:
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
uj5u.com熱心網友回復:
Size displayedImageSize = new Size((int)imageControl.ActualWidth, (int)imageControl.ActualHeight);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/451317.html
