简体   繁体   中英

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

I have this code

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

I need to use _color in Microsoft.Xna.Framework

using previous code I receive this error:

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

any idea how to solve this?

Is this what you need?

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

Solved with this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM