C# &引用;无法从源类型强制转换为目标类型“;埃罗投掷;尝试将数据从嵌入式dll资源传递到字段时

C# &引用;无法从源类型强制转换为目标类型“;埃罗投掷;尝试将数据从嵌入式dll资源传递到字段时,c#,dll,unity3d,embedded-resource,C#,Dll,Unity3d,Embedded Resource,这是我自动生成的资源类,我修改它以使用UnityEngine.Texture2D类而不是System.Drawing.Bitmap namespace DaiMangou.Properties { using System; using UnityEngine; /// <summary> /// A strongly-typed resource class, for looking up localized strings, etc.

这是我自动生成的资源类,我修改它以使用
UnityEngine.Texture2D
类而不是
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));
                }
            }

}
然而,这会抛出一个错误

InvalidCastException:无法从源类型强制转换为目标类型。

如何将
ScaleHandle
纹理传递给我的
scaleHandleTexture
字段


Unity引擎不支持使用System.Drawing,因此我确实无法使用位图。首先,不要修改自动生成的资源文件,让它们使用BitmapImage类型

然后是将
位图图像
转换为
字节[]
的代码(警告部分是关于对图像类型使用正确的编码器):

最后是要转换为
Texture2D
的代码:

Texture2D.LoadImage(data);
总之:

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);
}

附言:我甚至不知道什么是团结,所以请接受它的价值

显然,强制转换异常是从
return((Texture2D)(obj))行抛出的(您的资源不应该是
Texture2D
类型)感谢您让我知道+1我不知道如何避开这一点。使用dll的特定平台存在可比性问题。使资源成为Texture2D类型是唯一的解决方法。如何成功地将ScaleHandle纹理传递到scaleHandleTexture字段。+1,感谢迄今为止的帮助。稍后我将测试代码,并让您知道它是如何运行的。我无法让它为我工作,但谢谢您。
Texture2D.LoadImage(data);
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);
}