Autor Beitrag
rijndael
Hält's aus hier
Beiträge: 12



BeitragVerfasst: Mi 28.11.07 12:07 
Hallo,
ich möchte ein Image aus einem Assembly, in dem es als Resource einkompiliert ist, programmatisch laden. Im XAML-Code kann man das bekanntlich so machen:
<Image Source="/MyResourcesAssembly;component/icons/myimage.png" Width="16" Height="16" ... />

Wenn ich das ganze nun programmtisch versuche mit:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
private Image m_Image;
... 
BitmapImage img = new BitmapImage(new Uri("/MyResourcesAssembly;component/icons/myimage.png"));
m_Image = new System.Windows.Controls.Image();
m_Image.Source = img;
...


dann kommt es zur Exception, weil die URI in einen Filesystem-Pfad übersetzt wird. Was ich aber tun möchte, ist das Image aus dem Assembly laden. DAs Image ist nicht im Resourcen-Objekt des Assembly eingetragen, sondern als Datei hinzugefügt und hat den Typ "Resource", wird also mit einkompiliert.

danke
rijn

Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Mi 28.11.07 17:08 
XAML besitzt eine schöne Abkürzung für Pack-URIs, in C# müsste es dann so aussehen:
ausblenden Quelltext
1:
pack://application:,,,/MyResourcesAssembly;Component/icons/myimage.png					
rijndael Threadstarter
Hält's aus hier
Beiträge: 12



BeitragVerfasst: Fr 30.11.07 09:51 
Hallo,
hat super funktioniert. Was auch geht ist folgendes:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
         Assembly asm = Assembly.LoadFile("Myresources.dll");
          BitmapImage bmp = new BitmapImage();
          bmp.BeginInit();
          bmp.StreamSource = asm.GetManifestResourceStream("Myresources.image.jpg");
          bmp.EndInit();
          m_Image = new System.Windows.Controls.Image();
          m_Image.Source = bmp;

dazu muss das Image allerdings eine "Embedded Resource" sein, von daher ist deine Lösung zumindest für mich deutlich besser.

thx
rijn
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Fr 30.11.07 14:28 
user profile iconrijndael hat folgendes geschrieben:
dazu muss das Image allerdings eine "Embedded Resource" sein, von daher ist deine Lösung zumindest für mich deutlich besser.
Genau, denn Embedded stammt noch aus SWF und verträgt sich nicht mit XAML.