Question

I am converting a System.Media.Brush to a System.Drawing.Brush, but after I change the color. It throws a "Token is not valid" error on the converter.

    private Brush DrawingColorToBrush(System.Drawing.Color color)
    {
        Brush ret;

        BrushConverter m;


        m = new BrushConverter();
        ret = (Brush)m.ConvertFromString(color.ToArgb().ToString("X8"));

        return ret;
    }

The color is coming from a System.Windows.Forms.ColorDialog enter image description here

Was it helpful?

Solution

Your code will work if you change your method to this...

    private Brush DrawingColorToBrush(System.Drawing.Color color)
    {
        Brush ret = null;
        BrushConverter m = new BrushConverter();
        string s = "#" + color.ToArgb().ToString("X8");
        if (m.CanConvertFrom(typeof (string)))
        {
            ret = (Brush) m.ConvertFromString(s);
        }
        return ret;
    }

The key is to prepend the string with the '#' character.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top