BitmapImage.PixelFormat抛出DirectoryNotFoundException

时间:2013-08-13 11:56:31

标签: c# wpf bitmap

    public MainWindow()
    {
        InitializeComponent();

        BitmapImage b = new BitmapImage(new Uri("Images/SampleImage.png", UriKind.Relative));
        //PixelFormat f = b.Format;         // throws DirectoryNotFoundException
        image.Source = b;
        PixelFormat f = b.Format;           // OK
    }
如果在行PixelFormat之前调用,则

image.Source = b;属性抛出异常。但是如果在这条线之后调用它会成功。为什么会这样?我想要这样做,如果我不想将此位图设置为image.Source,只想在程序中使用此对象吗?

Visual Studio 2012,Windows 8,C#WPF项目。 Images / SampleImage.png是资源文件,它显示在Image控件中。

1 个答案:

答案 0 :(得分:1)

这有效

BitmapImage b = new BitmapImage(new Uri("pack://application:,,,/YourApplicationName;component/Images/SampleImage.png"));
PixelFormat f = b.Format;