Page 1 of 1

NoesisGUI For C# Desktop Application Development

Posted: 25 Apr 2018, 15:43
by SannanKhan
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.

Re: NoesisGUI For C# Desktop Application Development

Posted: 26 Apr 2018, 09:21
by cleik
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!

Re: NoesisGUI For C# Desktop Application Development

Posted: 26 Apr 2018, 10:26
by SannanKhan
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. :)

Re: NoesisGUI For C# Desktop Application Development

Posted: 26 Apr 2018, 11:26
by sfernandez
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.

Re: NoesisGUI For C# Desktop Application Development

Posted: 27 Apr 2018, 11:17
by jsantos
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.