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("修改成功");
相关推荐
缺点内向1 天前
C#编程实战:如何为Word文档添加背景色或背景图片
开发语言·c#·自动化·word·.net
学海无涯书山有路1 天前
async-await异步编程
c#
切糕师学AI1 天前
ARM 汇编器中的伪指令(Assembler Directives)
开发语言·arm开发·c#
lzhdim1 天前
C#开发的提示显示例子 - 开源研究系列文章
开发语言·c#
人工智能AI技术1 天前
【C#程序员入门AI】向量数据库入门:C#集成Chroma/Pinecone,实现AI知识库检索(RAG基础)
人工智能·c#
xb11321 天前
C# 定时器和后台任务
开发语言·c#
A_nanda1 天前
c# 用VUE+elmentPlus生成简单管理系统
javascript·vue.js·c#
wuguan_1 天前
C#之线程
开发语言·c#
gc_22991 天前
学习C#调用OpenXml操作word文档的基本用法(21:学习嵌入对象类)
c#·word·openxml·ole
老骥伏枥~1 天前
C# if / else 的正确写法与反例
开发语言·c#