SannanKhan
Topic Author
Posts: 3
Joined: 24 Apr 2018, 18:05

NoesisGUI For C# Desktop Application Development

25 Apr 2018, 15:43

I am having troubles assigning a png image to Image element in XAML. At runtime the application crashes with exception
InvalidCastException: Unable to cast object of type 'System.Drawing.Bitmap' to type 'System.Byte[]'.
My steps are as follows:

1. App.xaml.cs
protected override TextureProvider GetTextureProvider()
        {
            EmbeddedTexture[] textures =
            {
                new EmbeddedTexture {filename="icon.png", resource="icon"}
            };
            return new EmbeddedTextureProvider(textures, Properties.Resources.ResourceManager);
        }
        
2. MainWindow.xaml
<Image Source="icon.png"/>
3. I added icon.png in project dir with build action "Resource" and added the same in Resources.resx file.


I ran the Menu3D sample from Github which loaded png images without any trouble. Can someone please point out what I might be doing wrong? I'd appreciate the help.
Thanks.
 
cleik
Posts: 4
Joined: 03 Apr 2018, 14:45

Re: NoesisGUI For C# Desktop Application Development

26 Apr 2018, 09:21

Hi, I got this problem too, I solved it by changing "System.Drawing.Bitmap, System.Drawing" with "System.Byte[], mscorlib" in the Resources.resx file for each image. I also modified the getters in Resources.Designer.cs. That's a workaround but if someone have a better solution I'll take it!
 
SannanKhan
Topic Author
Posts: 3
Joined: 24 Apr 2018, 18:05

Re: NoesisGUI For C# Desktop Application Development

26 Apr 2018, 10:26

Hi, I got this problem too, I solved it by changing "System.Drawing.Bitmap, System.Drawing" with "System.Byte[], mscorlib" in the Resources.resx file for each image. I also modified the getters in Resources.Designer.cs. That's a workaround but if someone have a better solution I'll take it!
That solved my problem! Thank you so much Cleik. :)
 
User avatar
sfernandez
Site Admin
Posts: 1912
Joined: 22 Dec 2011, 19:20

Re: NoesisGUI For C# Desktop Application Development

26 Apr 2018, 11:26

There is no direct way to change how an image resource is stored in the Resources.resx through Visual Studio interface (a combo box for selecting the resource Type in the resource properties, as it occurs with text files, would have been great).

So the unique and correct way I found to change it is right click on Resources.resx 'View Code' and modify the type in the xml definition:
<data name="BackgroundImage" type="System.Resources.ResXFileRef, System.Windows.Forms">
  <value>..\BackgroundImage.png;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
Once you rebuild the project, Resources.Designer.cs getters and setters for the image resources will be updated accordingly.
 
User avatar
jsantos
Site Admin
Posts: 2904
Joined: 20 Jan 2012, 17:18
Contact:

Re: NoesisGUI For C# Desktop Application Development

27 Apr 2018, 11:17

Remember, this is the way we decided to pack our resources in our samples. But you don't need to do it that way, you can reimplement the providers and return Stream to your content. For example, just a provider that opens files from disk should be trivial to implement.

Who is online

Users browsing this forum: No registered users and 1 guest