我正在尝试在代码中设置WPF图像的源代码.图像作为资源嵌入到项目中.通过查看示例,我提出了以下代码.由于某种原因,它不起作用 - 图像不显示.
通过调试,我可以看到流包含图像数据.那有什么不对?
Assembly asm = Assembly.GetExecutingAssembly();
Stream iconStream = asm.GetManifestResourceStream("SomeImage.png");
PngBitmapDecoder iconDecoder = new PngBitmapDecoder(iconStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
ImageSource iconSource = iconDecoder.Frames[0];
_icon.Source = iconSource;
Run Code Online (Sandbox Code Playgroud)
图标的定义如下: <Image x:Name="_icon" Width="16" Height="16" />
在xaml中它是:
<View:BaseWindow.Icon>
/VBDAdvertisement;component/Images/logoVBD.png
</View:BaseWindow.Icon>
Run Code Online (Sandbox Code Playgroud)
我想把它转换成代码.
谢谢