对于一个WPF应用程序,它将需要10 - 20个小图标和图像用于说明目的,将它们作为嵌入式资源存储在程序集中是正确的方法吗?

如果是这样,我如何在XAML中指定图像控件应该从嵌入式资源加载图像?


当前回答

如果您正在使用Blend,为了使它更加简单,并且没有任何问题,为Source属性获取正确的路径,只需将图像从Project面板拖放到设计器上。

其他回答

下面工作和待设置的图像是在属性中的资源:

    var bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(MyProject.Properties.Resources.myImage.GetHbitmap(),
                                      IntPtr.Zero,
                                      Int32Rect.Empty,
                                      BitmapSizeOptions.FromEmptyOptions());
    MyButton.Background = new ImageBrush(bitmapSource);
img_username.Source = bitmapSource;

Visual Studio 2010 Professional SP1。 . net Framework 4客户端配置文件。 在项目属性上添加PNG图像作为资源。 自动在资源文件夹中创建新文件。 构建操作集到资源。

这招对我很管用:

<BitmapImage x:Key="MyImageSource" UriSource="Resources/Image.png" />

是的,这是正确的方法。

你可以在资源文件中使用图像,只使用路径:

<Image Source="..\Media\Image.png" />

您必须将映像文件的构建操作设置为“资源”。

如果您正在使用Blend,为了使它更加简单,并且没有任何问题,为Source属性获取正确的路径,只需将图像从Project面板拖放到设计器上。

是的,这条路对。你可以使用一个路径在资源文件中使用图像:

<StackPanel Orientation="Horizontal">
    <CheckBox  Content="{Binding Nname}" IsChecked="{Binding IsChecked}"/>
    <Image Source="E:\SWorking\SharePointSecurityApps\SharePointSecurityApps\SharePointSecurityApps.WPF\Images\sitepermission.png"/>
    <TextBlock Text="{Binding Path=Title}"></TextBlock>
</StackPanel>