Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改解码像素宽度/高度后获取BitmapImage的原始大小_C#_Wpf_Bitmapimage - Fatal编程技术网

C# 更改解码像素宽度/高度后获取BitmapImage的原始大小

C# 更改解码像素宽度/高度后获取BitmapImage的原始大小,c#,wpf,bitmapimage,C#,Wpf,Bitmapimage,由于性能原因,我必须在加载期间重新缩放非常大的位图图像(例如45000*10000像素)。由于图像可以缩放,我不能简单地指定一个固定的大小,但会将像素大小减少一些(稍后动态)因素: 初始化完成后,PixelWidth等于解码的PixelWidth。但是,我需要访问图像的原始宽度和高度。是否有可能直接从图像对象中获取这些信息,或者我必须将其存储在其他地方 问题是,BitmapImage是密封的,因此我无法使用新参数扩展该类-但是,我希望将结果图像传递给图像控件的源。您应该能够使用BitmapDec

由于性能原因,我必须在加载期间重新缩放非常大的位图图像(例如45000*10000像素)。由于图像可以缩放,我不能简单地指定一个固定的大小,但会将像素大小减少一些(稍后动态)因素:

初始化完成后,PixelWidth等于解码的PixelWidth。但是,我需要访问图像的原始宽度和高度。是否有可能直接从图像对象中获取这些信息,或者我必须将其存储在其他地方


问题是,BitmapImage是密封的,因此我无法使用新参数扩展该类-但是,我希望将结果图像传递给
图像
控件的源。

您应该能够使用
BitmapDecoder
获取图像文件的原始尺寸:

int height;
int width;
using (var stream = File.OpenRead(fileName))
{
    var decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.IgnoreColorProfile,
        BitmapCacheOption.Default);
    height = decoder.Frames[0].PixelHeight;
    width = decoder.Frames[0].PixelWidth;

    stream.CopyTo(memory);
}

然后,您可以将这些值存储在变量中供以后使用。

有时,您看不到树的森林。。。 BitmapImage是一个依赖项对象。因此,无需从密封类派生,只需创建一些附加属性:

public static int GetSourcePixelWidth(this BitmapSource bitmap) { return ((int)bitmap.GetValue(SourcePixelWidthProperty)).IfZero(bitmap.PixelWidth); }
public static void SetSourcePixelWidth(this BitmapSource bitmap, int value) { bitmap.SetValue(SourcePixelWidthProperty, value); }

public static readonly DependencyProperty SourcePixelWidthProperty =
        DependencyProperty.RegisterAttached("SourcePixelWidth", typeof(int), typeof(Attached), new PropertyMetadata(0));

public static int GetSourcePixelHeight(this BitmapSource bitmap) { return ((int)bitmap.GetValue(SourcePixelHeightProperty)).IfZero(bitmap.PixelHeight); }
public static void SetSourcePixelHeight(this BitmapSource bitmap, int value) { bitmap.SetValue(SourcePixelHeightProperty, value); }

public static readonly DependencyProperty SourcePixelHeightProperty =
        DependencyProperty.RegisterAttached("SourcePixelHeight", typeof(int), typeof(Attached), new PropertyMetadata(0));

谢谢你的回答。我已经澄清了一点,因为我已经从tempImage对象中获得了这些信息,但我只是希望通过最终的图像可以以某种方式访问这些信息。
public static int GetSourcePixelWidth(this BitmapSource bitmap) { return ((int)bitmap.GetValue(SourcePixelWidthProperty)).IfZero(bitmap.PixelWidth); }
public static void SetSourcePixelWidth(this BitmapSource bitmap, int value) { bitmap.SetValue(SourcePixelWidthProperty, value); }

public static readonly DependencyProperty SourcePixelWidthProperty =
        DependencyProperty.RegisterAttached("SourcePixelWidth", typeof(int), typeof(Attached), new PropertyMetadata(0));

public static int GetSourcePixelHeight(this BitmapSource bitmap) { return ((int)bitmap.GetValue(SourcePixelHeightProperty)).IfZero(bitmap.PixelHeight); }
public static void SetSourcePixelHeight(this BitmapSource bitmap, int value) { bitmap.SetValue(SourcePixelHeightProperty, value); }

public static readonly DependencyProperty SourcePixelHeightProperty =
        DependencyProperty.RegisterAttached("SourcePixelHeight", typeof(int), typeof(Attached), new PropertyMetadata(0));