Answers:
如果要在多个地方使用图像,那么仅将图像数据加载到内存中一次,然后在所有Image元素之间共享就值得了。
为此,请在以下位置创建一个BitmapSource  作为资源:
<BitmapImage x:Key="MyImageSource" UriSource="../Media/Image.png" />然后,在您的代码中,使用类似以下内容的代码:
<Image Source="{StaticResource MyImageSource}" />就我而言,我发现必须将Image.png文件设置为具有Resource而不是的构建动作Content。这将导致图像在已编译的程序集中进行。
DynamicResource标记扩展而不是StaticResource,假设您在编译时就知道键。在WPF中,您可以在运行时创建资源字典。实际上,这就是您加载Xaml文档时发生的情况,只是您没有看到等效的C#。
                    Width="{Binding Source.PixelWidth, RelativeSource={RelativeSource Self}}" 到Image,否则我经常看到的图像越来越荒唐放大出于某种原因(如16×16的图标延伸到东西,看起来像200x200像素)。
                    我发现使用图像,视频等的最佳做法是:
<Image Source="/WPFApplication;component/Images/Start.png" />优点:
有人问有关用代码执行此操作的问题,但没有得到答案。
经过数小时的搜索,我发现了一个非常简单的方法,没有找到任何示例,因此在这里分享了我的适用于图像的方法。(我的是一个.gif)
摘要:
它返回ImageSource“目标”似乎喜欢的BitmapFrame。
采用:
doGetImageSourceFromResource ("[YourAssemblyNameHere]", "[YourResourceNameHere]");方法:
static internal ImageSource doGetImageSourceFromResource(string psAssemblyName, string psResourceName)
{
    Uri oUri = new Uri("pack://application:,,,/" +psAssemblyName +";component/" +psResourceName, UriKind.RelativeOrAbsolute);
    return BitmapFrame.Create(oUri);
}学到:
根据我的经验,打包字符串不是问题,请检查您的流,尤其是如果第一次读取它已将指针设置为文件末尾,并且您需要在重新读取之前将其重置为零。
我希望这可以节省您很多时间,希望这件作品对我有帮助!
在代码中将资源加载到正在执行的程序集中,其中我的图像Freq.png位于该文件夹中,Icons并且定义为Resource:
this.Icon = new BitmapImage(new Uri(@"pack://application:,,,/" 
    + Assembly.GetExecutingAssembly().GetName().Name 
    + ";component/" 
    + "Icons/Freq.png", UriKind.Absolute)); 我还做了一个功能:
/// <summary>
/// Load a resource WPF-BitmapImage (png, bmp, ...) from embedded resource defined as 'Resource' not as 'Embedded resource'.
/// </summary>
/// <param name="pathInApplication">Path without starting slash</param>
/// <param name="assembly">Usually 'Assembly.GetExecutingAssembly()'. If not mentionned, I will use the calling assembly</param>
/// <returns></returns>
public static BitmapImage LoadBitmapFromResource(string pathInApplication, Assembly assembly = null)
{
    if (assembly == null)
    {
        assembly = Assembly.GetCallingAssembly();
    }
    if (pathInApplication[0] == '/')
    {
        pathInApplication = pathInApplication.Substring(1);
    }
    return new BitmapImage(new Uri(@"pack://application:,,,/" + assembly.GetName().Name + ";component/" + pathInApplication, UriKind.Absolute)); 
}用法(假设您将函数放在ResourceHelper类中):
this.Icon = ResourceHelper.LoadBitmapFromResource("Icons/Freq.png");注意:请参阅WPF中的MSDN Pack URI:
 pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml
new Uri(@"pack://application:,,,/" + pathInApplication)技巧也可以。
                    是的,这是正确的方法。
您可以仅使用路径在资源文件中使用图像:
<Image Source="..\Media\Image.png" />您必须将图像文件的构建操作设置为“资源”。
是的,这是正确的方法。您可以使用以下路径在资源文件中使用图像:
<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>以下工作和要设置的图像是属性中的资源:
    var bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(MyProject.Properties.Resources.myImage.GetHbitmap(),
                                      IntPtr.Zero,
                                      Int32Rect.Empty,
                                      BitmapSizeOptions.FromEmptyOptions());
    MyButton.Background = new ImageBrush(bitmapSource);
img_username.Source = bitmapSource;