1. 程式人生 > >WPF 從程式集中檢索圖片資源stream給Image控制元件使用

WPF 從程式集中檢索圖片資源stream給Image控制元件使用

原文: WPF 從程式集中檢索圖片資源stream給Image控制元件使用

            // 獲取當前程式集
            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); }