访问嵌入的资源----图片实例-------C#

1.C#中的文件属性可以设置为嵌入的资源,并且不复制到本地

 

这时候就不能用普通的相对路径来引用了

 

 Assembly assembly = GetType().Assembly;
                                                                   //这时候的路径是:命名空间+文件路径,以点相隔
            System.IO.Stream streamSmall = assembly.GetManifestResourceStream("mytest.leftbutton.png");

            //这个BitmapImage是wpf中的类
            BitmapImage bitmap = new BitmapImage();
            bitmap.BeginInit();//开始初始化
            bitmap.StreamSource = streamSmall;
            bitmap.EndInit();//结束初始化

            this.nininini.Source = bitmap;

 

2.如果是resource文件。就和wpf绑定其他项目里的资源文件xaml的路径一样了

new BitmapImage(new Uri("/MEFA.Controls;component/Resources/Images/leftbutton_紫色.png", UriKind.Relative));

 

posted on 2015-11-11 17:16  你的乐哥哥  阅读(984)  评论(0编辑  收藏  举报