WPF 从程序集中检索图片资源stream给Image控件使用

原文: WPF 从程序集中检索图片资源stream给Image控件使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nihang1234/article/details/82656076
            // 获取当前程序集
            Assembly assembly = Assembly.GetAssembly(GetType());
            // 获取程序集中资源名称
            string resourceName = assembly.GetName().Name + ".g";
            // 资源管理器
            ResourceManager rm = new ResourceManager(resourceName, assembly);
            BitmapImage bitmap = new BitmapImage();
            using (ResourceSet set = rm.GetResourceSet(CultureInfo.CurrentCulture, true, true))
            {
                UnmanagedMemoryStream s;
                s = (UnmanagedMemoryStream)set.GetObject("images/leaf.jpg", true);

           // img在XAML声明的空间      
           img.Source=BitmapFrame.Create(s,BitmapCreateOptions.None,BitmapCacheOption.OnLoad);
            }

猜你喜欢

转载自www.cnblogs.com/lonelyxmas/p/10167911.html