XAML中
1、直接绝对路径直接给Source
2、将图片放到项目里面,设置图片为资源;Source写法为:
图片在另外项目:Source="pack://application:,,,/label里面的Content;component/folder/test.png"
图片在本项目Source="pack://application:,,,/folder/test.png"
-- 其中“folder”是文件夹,“label里面的Content”是命名空间,”component“是固定的
3、将图片放到App.xaml中,再StaticResource指定
App.xaml中:
<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,这样写得比较复杂
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("修改成功");
拓展:字体引入
字体库是.ttf结尾,用的时候就是:pack://application:,,,/项目命名空间;component/路径/#字体名称