WPF中Image控件Source的多种指定方式

XAML中

1、直接绝对路径直接给Source

2、将图片放到项目里面,设置图片为资源;Source写法为:

(1)Source="pack://application:,,,/label里面的Content;component/folder/test.png"

(2)Source="pack://application:,,,/folder/test.png"

-- 其中"folder"是文件夹,"label里面的Content"是命名空间,"component"是固定的

3、将图片放到App.xaml中,再StaticResource指定

App.xaml中:

XML 复制代码
<Application.Resources>
    <ResourceDictionary>
        <BitmapImage x:Key="AppXamlBitmapImage" UriSource="pack://application:,,,/folder/test.png"/>
        <BitmapImage x:Key="AppXamlBitmapImage2" UriSource="pack://application:,,,/label里面的Content;component/folder/test.png"></BitmapImage>
    </ResourceDictionary>
</Application.Resources>

然后使用就直接Source="{StaticResouce keyName}",如果需要更换的使用DynamicResource也是可以的

-- 使用这种方式的图片只能在运行后看到

code_behind或VidwModel中

1、最多用就是创建BitmapImage对象

2、使用ImageSourceConverter,这样写得比较复杂

cs 复制代码
var converter = new ImageSourceConverter();
image1.Source = (ImageSource)converter?.ConvertFromString("D:\\desktop\\tmpp\\二维码.jpg");

image1.Source = new BitmapImage(new Uri("D:\\desktop\\tmpp\\二维码.jpg"));
MessageBox.Show("修改成功");
相关推荐
贪玩的蛋挞9 小时前
C#与闭包
开发语言·c#
忧郁的紫菜14 小时前
基础实现:单篇 Markdown 转 Word
开发语言·c#·word
甜美的小天鹅14 小时前
Swifter C#之inline还是不inline,这是个问题
开发语言·c#
云中飞鸿14 小时前
该如何进行WPF界面设计
wpf
翼帆18 小时前
.NET 程序保护实战系列01-流水线架构与保护引擎总览
c#·破解
落寞的电源20 小时前
Delegate = Object + MethodInfo
开发语言·数据库·c#
Denuin多啦咧梦21 小时前
玩转 .NET 依赖注入:Microsoft.Extensions.DependencyInjection 深度指南
c#·.net·依赖注入
Ricky_Theseus1 天前
Trie 字典树:前缀匹配利器
开发语言·c#
北域码匠1 天前
嵌入式限幅滤波:工业信号降噪利器
c#·传感器采集·数据预处理·嵌入式算法·限幅滤波·数字滤波·数据降噪
爱听歌的鸡翅2 天前
C# 深度学习框架 TorchSharp 原生训练模型和图像识别-手写数字识别
开发语言·深度学习·c#