WPF Image控件的Source属性是一个ImageSource对象

1.固定的图片路径是可以的,如下:

<Image Source="../test.png" />

2.绑定的Path是一个String类型的图片路径,而实际情况它需要的是一个ImageSource,所以只需在前面标记它是一个Path类型的值就OK了!

<DataTemplate>
<Image Source="{Binding Path= IconPath}" /> </DataTemplate>

----------------------------------------------------------------------------------------------------------------------------------

很多时候,我们会使用图片来装饰UI,比如作为控件背景等。

而这些图片可以分为两种形式,即存在于本地文件系统中的图片和存在于内存中的图片

对于这两种形式的图片,在WPF中,使用方法不同,下面主要说明针对这两种形式图片的使用方法

一、存在于本地文件系统中的图片文件
对于此类图片,使用非常简单,在xaml中直接指定路径即可,如:
<Button>
<Button.Background>
<ImageBrush ImageSource="bg.jpg"/>
</Button.Background>
</Button>
对应的的C#代码为
ImageBrush imageBrush = new ImageBrush();
imageBrush.ImageSource = new BitmapImage(new Uri("bg.jpg", UriKind.Relative));
button.Background = imageBrush;
其中imageBrush.ImageSource的类型为ImageSource,而ImageSource是个抽象类,
因此我们不能直接使用它,而是使用它的子类来代替,查阅MSDN,可以看到它们的继承关系:
System.Windows.Media.ImageSource
System.Windows.Media.DrawingImage
System.Windows.Media.Imaging.BitmapSource

二、存在于内存中的图片
对于只存在于内存中的图片,用以上方法就显得无能为力了,我们应该另寻他法,下面介绍一种方法:
先看代码:

//此处图片从文件中读入用以模拟内存中的图片
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap("bg.jpg");
MemoryStream stream = new MemoryStream();
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
ImageBrush imageBrush = new ImageBrush();
ImageSourceConverter imageSourceConverter = new ImageSourceConverter();
button.Background = imageBrush;
其中bitmap即是存在于内存中的Bitmap类型图片,此处使用直接加载本地图片文件模拟。
步骤是先将它保存到流中,再使用ImageSourceConverter 类的ConvertFrom方法从流中得到我们需要的图片
OK,本文到此结束,以上方法都是自己在使用中探索所得,如果有更好的方法,本人非常愿意和各位交流。

---------------------------------------------------------------------------------------------------------------------------------

<StackPanel>
<StackPanel.Resources>
<local:ImageConverter x:Key="cvt_image" />
</StackPanel.Resources>
<TextBox x:Name="txtPath" Text="E:\.....\aaa.jpg" />
<Image Source="{Binding ElementName=txtPath, Path=Text}"/>
<Image Source="{Binding ElementName=txtPath, Path=Text, Converter={StaticResource cvt_image}}" />
</StackPanel>

这里只是一个示范,其实是可以将数据库中取出来的二进制数据,直接转换成一个BitmapImage对象返回的:
public class ImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return new BitmapImage(new Uri(value.ToString()));
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}

图片的source是ImageSource类型,而继承该类型的class有:BitmapImage (继承自BitmapSource)和DrawingImage(继承自ImageSource)。BitmapImage可以显示一个普通位图,DrawingImage可以显示矢量图

时间: 2024-08-05 15:57:14

WPF Image控件的Source属性是一个ImageSource对象的相关文章

WPF RichTextBox 控件常用方法和属性

以下内容转自 http://blog.csdn.net/yulongguiziyao/article/details/25330551. 1. 取得已被选中的内容: (1)使用 RichTextBox.Document.Selection属性(2)访问RichTextBox.Document.Blocks属性的“blocks”中的Text2. 在XAML中增加内容给RichTextBox:<RichTextBox IsSpellCheckEnabled="True">  

WPF系列 —— 控件添加依赖属性

依赖属性的概念,用途 ,如何新建与使用.本文用做一个自定义TimePicker控件来演示WPF的依赖属性的简单应用. 先上TimePicker的一个效果图. 概念 和 用途:依赖属性是对传统.net 属性的一种封装,使一个传统.net属性支持 WPF 中的 数据绑定.动画.样式 等功能. 新建:任意代码代码文件中 ,输入 propdp 再双击tab键.生成如下的代码块. MyProperty: 依赖属性的名称: ownerclass: 当前依赖属性绑定的所有类; new PropertyMeta

WPF中PasswordBox控件的Password属性的数据绑定

原文:WPF中PasswordBox控件的Password属性的数据绑定 英文原文:http://www.wpftutorial.net/PasswordBox.html 中文原文:http://blog.csdn.net/oyi319/article/details/6551532 WPF的PasswordBox控件的Password属性不是依赖属性,无法直接进行数据绑定,为使其在MVVM模式中正常使用,可以为PasswordBox增加一个助手类,代码如下: 注:代码摘自:http://www

WPF中的image控件的Source赋值

image控件的Source设置为相对路径后(Source="haha.png")运行不能显示 解决方案:当Source设置为相对路径后(Source="haha.png")Visual Studio 2013会在属性栏的Source中生成“/WpfApplication1;component/haha.png”. 不知道在其他Visual Studio中会不会这样,但是可以按照这样写就不会出现不能显示的问题. img.Source = new BitmapImag

WPF常用控件总结及其应用demo

WPF常用控件总结及其应用 一.控件 1.WrapPanel布局控件:可以实现当空间不足时子控件自动往下一行布局,空间充足时又会自动调整行布局.常用布局控件还有StackPanel(设置其子元素是垂直排列还是水平排列).Grid(通过定义行和列来绘制出一个表格).Canvas(通过指定相对于其的坐标来指定子控件的位置).DockPanel(设置其子元素如何停靠,DockPanel.Left.DockPanel.Right.DockPanel.Top.DockPanel.Bottom). 2.Sc

WPFのImage控件souce引入的方法总结

原文:WPFのImage控件souce引入的方法总结 1.后台代码相对路径添加(若为绝对路径,换UriKind的属性即可) BitmapImage testBitmapImage = new BitmapImage(new Uri(@"\bin\Sources\ON_btn_AutoDetect.bmp", UriKind.Relative)); image1.Source = imagetemp; 2.后台代码二进制添加 private string path = @"F:

WPF 布局控件 之 DockPanel

DockPanel为容器控件 主要了解其Dock属性和LastChildFill属性的使用 一.LastChildFill="True" 时 代码: <DockPanel LastChildFill="True"> <Button DockPanel.Dock="Top">Top</Button> <Button DockPanel.Dock="Bottom">Bottom<

WPF Popup 控件导致被遮挡内容不刷新的原因

WPF Popup 控件导致被遮挡内容不刷新的原因 周银辉 今天在写一个WPF控件时用到了Popup控件,很郁闷的情况是:当popup关闭时,原来被popup挡住的界面部分不刷新,非要手动刷新一下(比如最大最小化一下窗口),就连网上传说的这个方法也不行 ? 1 2 3 4 5 6 7 8 9 10 public static class UiHelper {     private delegate void NoArgDelegate();     public static void Ref

WPF布局控件常用属性介绍

WPF布局控件常用属性介绍 其它 | 作者:慧都控件网 | 2011-04-06 13:41:57| 阅读 0次 有用(0) 评论(0) 概述:WPF布局控件都是派生自System.Windows.Controls.Panel抽象类的面板,Panel类继承自 FrameworkElement,Panel类本身并没有什么特别的,但是WPF中提供了许多用于布局的控件都继承自Panel类,如 StackPanel控件,WrapPanel,DockPanel,Grid,UniformGrid,Canva