wpf convert png to xaml

原文: wpf convert png to xaml

把png图片转化成xaml资源

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <DrawingImage x:Key="res_scheme_del">
<DrawingImage.Drawing>
            <DrawingGroup>
                <ImageDrawing ImageSource="scheme_del.png" Rect="0,0,16,16"/>
            </DrawingGroup>
        </DrawingImage.Drawing>
</DrawingImage>

</ResourceDictionary>


and set to menu item:

                    var icon = new Image();                                        
                    icon.Source = (ImageSource)FindResource("res_scheme_new");
                    MenuItem mi = new MenuItem();
                    mi.Header = "New";
                    mi.Icon = icon;
                    mi.IsEnabled = true;

posted on 2019-03-15 13:21 NET未来之路 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/10536442.html

<think>好的,我现在需要帮助用户解决在WPF中绑定本地PNG图像的问题。根据用户提供的引用资料,尤其是引用[3]和[4],Image控件通常通过Source属性进行绑定,而MVVM模式推荐使用数据绑定而不是后台代码直接操作。 首先,用户需要加载本地图像并进行绑定,所以应该考虑如何设置正确的路径。根据引用[3],绝对路径的格式类似于"Demo;Component/Images/Test.jpg",其中Demo是程序集名称。但如果是本地文件系统中的文件,可能需要使用URI的格式,比如"file:///"开头。 然后,绑定方面,引用[4]中的Model层有一个ImgSource属性,类型是字符串,可能存储图像的路径。但Image控件的Source属性需要ImageSource类型,所以需要将字符串路径换为BitmapImage或其他ImageSource派生类。这时候可能需要使用值换器(IValueConverter)或者在ViewModel中直接处理。 另外,用户提到是本地PNG文件,可能需要检查路径是否正确,权限是否允许访问。同时,确保图像生成操作设置为“内容”或“资源”,如果需要嵌入资源的话。 还需要考虑绑定的模式,比如OneWay,以及是否异步加载以避免界面卡顿,如引用[2]提到的异步加载网络图片的情况,但本地图片可能不需要,但如果图片较大,也可以考虑后台加载。 可能的步骤是:在XAML中定义Image控件,设置Source绑定到ViewModel的属性,该属性返回正确的ImageSource。路径换可能需要URI或者使用pack URI语法。例如,如果图片作为资源嵌入,使用pack URI;如果是外部文件,使用绝对路径。 需要验证路径是否正确,例如使用UriKind.Absolute或Relative,以及确保文件存在。同时,处理可能的异常,比如文件未找到。 最后,提供示例代码,展示如何在XAML中绑定,ViewModel如何设置属性,以及路径的换方法。可能需要包括两种情况的示例:嵌入资源和外部文件。</think>在WPF中绑定本地PNG图像的实现步骤如下,包含两种典型场景: ### 一、绑定嵌入式资源(程序集内图片) 1. 将PNG文件放入项目`Resources/Images`文件夹 2. 文件属性设置为**Resource** 3. ViewModel定义: ```csharp public class ImageViewModel : INotifyPropertyChanged { private ImageSource _imgSource; public ImageSource ImgSource { get => _imgSource; set { _imgSource = value; OnPropertyChanged(); } } public ImageViewModel() { // 使用pack URI格式 var uri = new Uri("pack://application:,,,/YourAssemblyName;component/Resources/Images/demo.png"); ImgSource = new BitmapImage(uri); } } ``` 4. XAML绑定: ```xml <Image Source="{Binding ImgSource}" Stretch="Uniform"/> ``` ### 二、绑定外部文件(本地磁盘路径) ```csharp public class ImageViewModel { public ImageSource LocalImage { get { var path = @"C:\Images\demo.png"; return new BitmapImage(new Uri(path)); } } } ``` ```xml <Image Source="{Binding LocalImage}"/> ``` ### 三、完整MVVM绑定示例 1. 创建值换器处理路径换: ```csharp [ValueConversion(typeof(string), typeof(ImageSource))] public class PathToImageConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { try { return new BitmapImage(new Uri(value?.ToString())); } catch { return DependencyProperty.UnsetValue; } } } ``` 2. XAML中声明换器: ```xml <Window.Resources> <local:PathToImageConverter x:Key="PathConverter"/> </Window.Resources> <Image Source="{Binding ImagePath, Converter={StaticResource PathConverter}}"/> ``` ### 注意事项 1. 文件路径需使用`UriKind.Absolute`或正确的相对路径 2. 外部文件需要确保应用程序有访问权限 3. 大文件建议异步加载: ```csharp var bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.CacheOption = BitmapCacheOption.OnLoad; bitmap.UriSource = new Uri(path); bitmap.EndInit(); bitmap.Freeze(); // 跨线程安全 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值