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("修改成功");
相关推荐
c#上位机3 小时前
halcon刚性变换(平移+旋转)——vector_to_rigid
图像处理·人工智能·计算机视觉·c#·halcon
Miss_SQ3 小时前
Webgl打包后删除StreamingAssets文件夹下多余资源
unity·c#·webgl
小猪快跑爱摄影3 小时前
【AutoCad 2025】【C#】零基础教程(二)——遍历 Entity 插件 =》 AutoCAD 核心对象层级结构
开发语言·c#·autocad
烛阴4 小时前
C# Dictionary 入门:用键值对告别低效遍历
前端·c#
Monkey_Xuan6 小时前
C#中的引用传递和值传递
unity·c#
CreasyChan7 小时前
C# LINQ 深度解析:优缺点与性能陷阱
unity·c#·游戏开发
精神小伙就是猛7 小时前
C# sealed密封 追本溯源
开发语言·c#
雨季6669 小时前
C 语言学习指南:从入门到实战的系统路径
c#
缺点内向12 小时前
如何在 C# 中创建、读取和更新 Excel 文档
c#·.net·excel
c#上位机12 小时前
halcon创建对象数组——concat_obj
图像处理·计算机视觉·c#·halcon