读书人

wpf上Bit地图ImageImage类的有关问题

发布时间: 2012-09-09 09:27:54 作者: rapoo

wpf下BitmapImage,Image类的问题
在CODE-BEHIND中初始化Image能不能不用BitmapImage,而直接设置Image.Source,如果能的话因为该属性不能直接设置为字符串,应该怎么设置。如果必须通过BitmapImage的话,初始化BitmapImage的时候需要指定Uri,但是我试了很多次,都无法用相对路径,只能用绝对路径,有没有什么解决办法?(问题来源于下面的代码)
string PATH = "file://D:\\try-blend\\1stMahjong\\Source\\Mjsource\\" + MjName + ".bmp";
Uri uri = new Uri(PATH);
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = uri;
bitmap.EndInit();

System.Windows.Controls.Image img = new System.Windows.Controls.Image();

img.Source = bitmap;

//在布局控件中添加IMG canv.Children.Add(img);
Canvas.SetLeft(img, toLeft);
Canvas.SetTop(img, toTop);


[解决办法]
BitmapImage只是ImageSource能接受的一个类型而已。
查MSDN, 还有一些相关的对象可以。
用Uri的时候可以使用相对路径。你要使用UriKind.Relative 这个参数。

读书人网 >CAD教程

热点推荐