문제

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

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top