Color From String - CSharp System.Drawing

CSharp examples for System.Drawing:KnownColor

Description

Color From String

Demo Code


using System.Globalization;
using System.Drawing;
using System;// w w  w.  j a v  a2  s .  c  o m

public class Main{
        public static int ColorFromString(this string value)
        {
            var typeHostOfColors = typeof(KnownColor);
            var getColorFromStr = (KnownColor)typeHostOfColors.GetField(value).GetValue(null);

            var knownToArgb = getColorFromStr.KnownToArgb();
            return knownToArgb;
        }
        /// <summary>
        /// Convert known colors to their Argb int32 format
        /// </summary>
        /// <param name="color">Color to be converted</param>
        /// <returns></returns>
        public static int KnownToArgb(this KnownColor color)
        {
            return Color.FromKnownColor(color).ToArgb();
        }
}

Related Tutorials