我有一个项目,它的名称是“xx”。
我创建一个具有以下路径的文件夹“images”:
xx\bin\调试\图像\
图像仅包含一张照片,其名称为“1.jpg”
主窗口包含图像控件;
我设置此代码来加载图像源,但它不起作用,为什么?:
private void Image_MouseDown(object sender, MouseButtonEventArgs e)
{
Image i = sender as Image; ;
BitmapImage b = new BitmapImage(new Uri(@"images\1.jpg",UriKind.Relative));
i.Source=b;
}
如何通过代码加载图像源?
提前致谢 :)
你需要添加1.jpg
到您的项目images
文件夹和设置属性 http://msdn.microsoft.com/en-us/library/0c6xyb66.aspx of 1.jpg
to Resource。要加载资源,请使用packURI http://msdn.microsoft.com/en-us/library/aa970069惯例。
private void Image_MouseDown(object sender, MouseButtonEventArgs e)
{
Image i = sender as Image; ;
BitmapImage b = new BitmapImage(new Uri(@"pack://application:,,,/"
+ Assembly.GetExecutingAssembly().GetName().Name
+ ";component/"
+ "Images/1.jpg", UriKind.Absolute));
i.Source=b;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)