0

Here is my auto generated resource class which i modify to use the UnityEngine.Texture2D class instead of System.Drawing.Bitmap

namespace DaiMangou.Properties {
    using System;
    using UnityEngine;

    /// <summary>
    ///   A strongly-typed resource class, for looking up localized strings, etc.
    /// </summary>
    // This class was auto-generated by the StronglyTypedResourceBuilder
    // class via a tool like ResGen or Visual Studio.
    // To add or remove a member, edit your .ResX file then rerun ResGen
    // with the /str option, or rebuild your VS project.
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]


    internal class GeneralImageResources {


     internal static Texture2D ScaleHandle {
                get {
                    object obj = ResourceManager.GetObject("ScaleHandle", resourceCulture);
                    return ((Texture2D)(obj));
                }
            }

}

Here is my other class in another script

public class ScaleHandle
{

public Texture2D scaleHandleTexture = DaiMangou.Properties.GeneralImageResources.ScaleHandle;

}

This, however throws an error

"InvalidCastException: Cannot cast from source type to destination type."

How do I pass the ScaleHandle texture to my scaleHandleTexture field ?

The Unity Engine does not support the use of System.Drawing so I really cant use Bitmap

  • 3
    obviously the cast exception is thrown from the line `return ((Texture2D)(obj));` (your resource should not be of the `Texture2D` type) – Sylvain P. Mar 25 '16 at 12:57
  • thanks for letting me know. +1 I do not know how to get around that. There are comparability issues with certain platform that will use the dll. making the resource a Texture2D type was the only way around it. How do i go about successfully passing the ScaleHandle texture to the scaleHandleTexture field. –  Mar 25 '16 at 13:07

1 Answers1

0

First, don't modify your autogenerated resource file, let them to BitmapImage type.

Then here is the code to convert BitmapImage to byte[] (warning with the part on using the right encoder for your image type):

byte[] data;
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(DaiMangou.Properties.GeneralImageResources.ScaleHandle));
using(MemoryStream ms = new MemoryStream())
{
    encoder.Save(ms);
    data = ms.ToArray();
}

And Finally here is the code to convert to Texture2D:

Texture2D.LoadImage(data);

In Sum:

public class ScaleHandle
{
    protected static Texture2D ImageToTexture(Image srcImage)
    {
        byte[] data;
        JpegBitmapEncoder encoder = new JpegBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(srcImage));
        using(MemoryStream ms = new MemoryStream())
        {
            encoder.Save(ms);
            data = ms.ToArray();
        }
        Texture2D tex = new Texture2D(2, 2);
        tex.LoadImage(data);
        return tex;
    }

    public Texture2D scaleHandleTexture = ImageToTexture(DaiMangou.Properties.GeneralImageResources.ScaleHandle);
}

PS: I don't even know what is Unity, so take it for what it's worth.

Community
  • 1
  • 1
Sylvain P.
  • 153
  • 6
  • +1 for helping out so far. I will test the code a little later on and let you know how it goes. –  Mar 25 '16 at 15:58
  • I could not get this to work for me , but thank you. –  Apr 05 '16 at 00:46