WPF中的image控件的Source赋值

时间:2023-12-06 10:19:20

WPF中的Image控件Source的设置

1.XAML中

简单的方式(Source="haha.png");

image控件的Source设置为相对路径后(Source="haha.png")运行不能显示

解决方案:当Source设置为相对路径后(Source="haha.png")改成“/WpfApplication1;component/haha.png”。

2.逻辑代码中

img.Source = new BitmapImage(new Uri("pack://application:,,,/Images/haha.jpg"));
//或
img.Source = new BitmapImage(new Uri("pack://SiteOfOrigin:,,,/Images/haha.jpg"));

下面讲解这两种用法(参考博客http://www点suchso.com/projecteactual/wpf-jiaocheng-tupian-lujing-uri.html)

今天就来详细说明wpf图片资源的路径问题和如何设置?

一开始我在后台直接复制图片路径是物理路径:

b.ImageSource = new BitmapImage(new Uri("../Themes/ZCThemes/skin/icon/card_unchoose.png", UriKind.RelativeOrAbsolute));

在visual studio 2013中是没有问题的。但是发布后,单独运行exe就出错了。

这个就涉及到wpf的新协议:   WPF引入了统一资源标识Uri(Unified Resource Identifier)来标识和访问资源。

其中较为常见的情况是用Uri加载图像。Uri表达式的一般形式为:协议+授权+路径

协议:pack://
     授权:有两种。一种用于访问编译时已经知道的文件,用application:///。一种用于访问编译时不知道、运行时才知道的文件,用siteoforigin:///。在这里加载图片时,我们选用前者,即application:///,但是书写时候,我们一般用逗号代替斜杠,也就是改写作application:,,,。
     路径:分为绝对路径和相对路径。这里我们选用相对路径,普适性更强。

下面,我们举一个简单的例子:
          pack://application:,,,/images/my.jpg
     当然,WPF默认Uri设置有pack://application:,,,,所以我们也可以直接将其写作:
          /images/my.jpg
     后边写例子程序时,为了让读者更好的了解Uri,我们都采用完整的Uri写法。

下面在讲讲装载图片的两种方式,一种用XAML引用资源,一种用代码引用资源。

用XAML引用资源:
          <Image Source="pack://application:,,,/images/my.jpg"/>

用代码引用资源:
          Image img;
          img.Source=new BitmapImage(new Uri("pack://application:,,,/images/my.jpg"),UriKind.Relative);

删除XAML中的Source属性,在后台为两个图片控件设置Source属性。有如下三种WPF资源路径方式。

第一种

imgContent.Source = new
BitmapImage(new Uri("Content.jpg", UriKind.Relative)); imgResource.Source = new
BitmapImage(new Uri("Resource.jpg", UriKind.Relative));

第二种

imgContent.Source = new
BitmapImage(new Uri("pack://application:,,,/Content.jpg")); imgResource.Source = new
BitmapImage(new Uri("pack://application:,,,/Resource.jpg"));

第三种

imgContent.Source = new
BitmapImage(new Uri("pack://SiteOfOrigin:,,,/Content.jpg"));

第一种和第二种都可以访问相对WPF资源路径的Resource和Content资源。第三种方式可以访问网站运行目录下的Content资源文件以及完全松散的文件。完全松散的文件指那些没有添加到项目中,只是拷贝在程序目录中的文件。

应用程序根本不知道它的存在。pack://application:,,,/Content.jpg表示当前项目的资源。它是pack://application:,,,/DllName;Component/Content.jpg的简写。将DllName替换成其他程序集,就可以访问其他程序集的资源。

pack://SiteOfOrigin:,,,/Content.jpg表示从部署位置访问文件。

pack URI格式是XML文件规范的一部分,具体格式如下 pack://packageURI/partPath。PackageURI实际上是在URI中放一个URI,它是把反斜杠都变成了逗号。packageURI的WPF资源路径可以志向一个XPS文档,例如file : /// c: /Document . xps会被编码为file:...c:,Document.xps。在WPF程序中有两种URI系统是特别处理的:

siteOfOrigin:/// 编码后siteOfOrigin:,,,

application:/// 编码后application:,,,

3个逗号其实是反斜杠编码过来的。

下面为参考的另外一篇博客(原文

BitmapImage imagetemp = new BitmapImage(new Uri("imgPath", UriKind.Absolute));

image1.Source = imagetemp;

其中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();

imageBrush.ImageSource = (ImageSource)imageSourceConverter.ConvertFrom(stream);

button.Background = imageBrush;

其中bitmap即是存在于内存中的Bitmap类型图片,此处使用直接加载本地图片文件模拟。 步骤是先将它保存到流中,再使用ImageSourceConverter 类的ConvertFrom方法从流中得到我们需要的图片