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 小时前
机器视觉中的3D高反光工件检测
人工智能·3d·c#·视觉检测
CodeCraft Studio1 小时前
文档处理控件TX Text Control系列教程:使用 .NET C# 从 PDF 文档中提取基于模板的文本
pdf·c#·.net
一念春风2 小时前
C# 背景 透明 抗锯齿 (效果完美)
开发语言·c#
且听风吟ayan2 小时前
leetcode day19 844+977
leetcode·c#
C137的本贾尼2 小时前
解决 LeetCode 串联所有单词的子串问题
算法·leetcode·c#
CoderIsArt5 小时前
C# 中的一个特性(Attribute)[ThreadStatic]
c#
心疼你的一切5 小时前
C# 中关于补位的写法 PadLeft,PadRight 函数
开发语言·unity·c#·游戏引擎·csdn·心疼你的一切
yue0085 小时前
C#项目05-猜数字多线程
c#·多线程·猜数字·invoke·control·项目案例·methodinvoke
视觉人机器视觉6 小时前
机器视觉3D深度图颜色含义解析
人工智能·3d·c#·视觉检测
yujunl6 小时前
用大内存主机下载Visual Studio
c#