Skip to main content

Microsoft Silverlight

Answered Question Canvas ColorRSS Feed

(0)

vicraze
vicraze

Member

Member

70 points

111 Posts

Canvas Color

hi

I want to change my canvas color dynamically so im writing like this

Canvas.Background = new SolidColorBrush(Colors.Red);

but i dont want Red color or any other built in colors ...i want to use my own color like #FF0000 ,#F2F2F2

how can use like this??????any idea

thanks in advance

vijay

ccoombs
ccoombs

Contributor

Contributor

5122 points

755 Posts

Re: Canvas Color

Color.FromArgb will give you what you need.  just provide alpha, red, green, blue values.

#FF0000 = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0))

#F2F2F2 = new SolidColorBrush(Color.FromArgb(255, 242, 242, 242))

vicraze
vicraze

Member

Member

70 points

111 Posts

Re: Canvas Color

hi thanks for your quick reply

i have a problm here....

i dont know what color im goin to get,  it comes from my xml as a string.....so im unable to use Color.FromArgb...

any suggestion pls..

thanks

ccoombs
ccoombs

Contributor

Contributor

5122 points

755 Posts

Answered Question

Re: Re: Canvas Color

you just need to convert from your hex string to the appropriate byte values.  create a helper function if you want...

         private Color hexToColor(string hexString) {
            hexString = hexString.Replace("#", "");
            Byte a = 255;
            Byte r = Convert.ToByte(hexString.Substring(0, 2), 16);
            Byte g = Convert.ToByte(hexString.Substring(2, 2), 16);
            Byte b = Convert.ToByte(hexString.Substring(4, 2), 16);
            return Color.FromArgb(a, r, g, b);
        }

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities