如何修复无法将类型'System.Drawing.Color'隐式转换为'Microsoft.Xna.Framework.Color'

时间:2014-03-15 16:00:45

标签: c# .net xna xna-4.0

我有这段代码

string hex = "#FFFFFF";
Color _color = System.Drawing.ColorTranslator.FromHtml(hex);

我需要在Microsoft.Xna.Framework

中使用_color

使用以前的代码我收到此错误:

Cannot implicitly convert type 'System.Drawing.Color' to 'Microsoft.Xna.Framework.Color'

any idea how to solve this?

2 个答案:

答案 0 :(得分:3)

这是你需要的吗?

public Microsoft.Xna.Framework.Graphics.Color XNAColor(System.Drawing.Color color)
{
    return new Microsoft.Xna.Framework.Graphics.Color(color.R, color.G, color.B, color.A) 
}

答案 1 :(得分:1)

解决了这个

  private Microsoft.Xna.Framework.Color ConvertFromHex(string s)
    {
        if (s.Length != 7)
            return Color.Gray;

        int r = Convert.ToInt32(s.Substring(1, 2), 16);
        int g = Convert.ToInt32(s.Substring(3, 2), 16);
        int b = Convert.ToInt32(s.Substring(5, 2), 16);
        return new Color(r, g, b);
    }