BitmapImage.PixelFormat throws DirectoryNotFoundException

本文关键字:DirectoryNotFoundException throws PixelFormat BitmapImage | 更新日期: 2023-09-27 18:27:25

    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 控件中。

BitmapImage.PixelFormat throws DirectoryNotFoundException

这有效

BitmapImage b = new BitmapImage(new Uri("pack://application:,,,/YourApplicationName;component/Images/SampleImage.png"));
PixelFormat f = b.Format;
相关文章:
  • 没有找到相关文章