WPF 引用项目内图片

参考

环境

软件/系统 版本 说明
Windows Windows 10 专业版 22H2 19045.4046
Microsoft Visual Studio Microsoft Visual Studio Community 2022 (64 位) - 17.6.5
Microsoft .Net SDK 8.0.101 手动安装
Microsoft .Net SDK 7.0.306 Microsoft Visual Studio 携带
.net 6.x 创建当前文章演示 WPF 项目时指定 .net 版本所选择的框架

步骤

  1. 项目目录下创建 images 文件夹,将 apple.jpg 放入该文件夹下。
    image

  2. 选中 apple.jpg 文件,在编辑器 属性 窗口设置以下参数:

    • 复制到输出目录:不复制
    • 生成操作:资源
      image
  3. XAML

        <StackPanel Orientation="Vertical">
            <Image
                Width="200"
                Height="200"
                Source="/images/apple.jpg" />
            <Image
                Width="200"
                Height="200"
                Source="pack://application:,,,/images/apple.jpg" />
            <Image
                Grid.Row="1"
                Grid.Column="1"
                Width="200"
                Margin="5">
                <Image.Source>
                    <BitmapImage UriSource="images/apple.jpg" />
                </Image.Source>
            </Image>
        </StackPanel>
    
  4. 预览
    image

posted @ 2024-02-25 17:22  夏秋初  阅读(191)  评论(0)    收藏  举报