Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. System.Drawing.Color to System.Windows.Media.Brush

System.Drawing.Color to System.Windows.Media.Brush

Scheduled Pinned Locked Moved WPF
csharpwpfwinformsgraphicsquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KBou
    wrote on last edited by
    #1

    Hi I'm creating a Windows Forms Application with an WPF Element Host. I want to the user to set the background of an WPF element with use of a ColorDialog. So I need to convert the System.Drawing.Color to a System.Windows.Media.Brush. I got it to work when the Color is a NamedColor (isNamedColor) but else I cannot get it to work.

                System.Windows.Media.BrushConverter bc = new System.Windows.Media.BrushConverter();
                if (\_color.IsNamedColor)
                {
                    ColorBrush = (System.Windows.Media.Brush)bc.ConvertFromString(\_color.Name);
                }
                else
                {
                    ???????????
                }
    

    What should I put at the ?????????

    P K A 3 Replies Last reply
    0
    • K KBou

      Hi I'm creating a Windows Forms Application with an WPF Element Host. I want to the user to set the background of an WPF element with use of a ColorDialog. So I need to convert the System.Drawing.Color to a System.Windows.Media.Brush. I got it to work when the Color is a NamedColor (isNamedColor) but else I cannot get it to work.

                  System.Windows.Media.BrushConverter bc = new System.Windows.Media.BrushConverter();
                  if (\_color.IsNamedColor)
                  {
                      ColorBrush = (System.Windows.Media.Brush)bc.ConvertFromString(\_color.Name);
                  }
                  else
                  {
                      ???????????
                  }
      

      What should I put at the ?????????

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      You can use Color.FromArgb to generate the appropriate values.

      ColorBrush.Color.FromArgb(_color.ToArgb());

      I'm doing this off the top of my head, so you may need to tweak it slightly but this should work.

      Deja View - the feeling that you've seen this post before.

      My blog | My articles

      K 1 Reply Last reply
      0
      • P Pete OHanlon

        You can use Color.FromArgb to generate the appropriate values.

        ColorBrush.Color.FromArgb(_color.ToArgb());

        I'm doing this off the top of my head, so you may need to tweak it slightly but this should work.

        Deja View - the feeling that you've seen this post before.

        My blog | My articles

        K Offline
        K Offline
        KBou
        wrote on last edited by
        #3

        The System.Windows.Media.Brush ColorBrush doesn't contain anything like Color or FromArgb. Neither does the BrushConverter.

        I 1 Reply Last reply
        0
        • K KBou

          Hi I'm creating a Windows Forms Application with an WPF Element Host. I want to the user to set the background of an WPF element with use of a ColorDialog. So I need to convert the System.Drawing.Color to a System.Windows.Media.Brush. I got it to work when the Color is a NamedColor (isNamedColor) but else I cannot get it to work.

                      System.Windows.Media.BrushConverter bc = new System.Windows.Media.BrushConverter();
                      if (\_color.IsNamedColor)
                      {
                          ColorBrush = (System.Windows.Media.Brush)bc.ConvertFromString(\_color.Name);
                      }
                      else
                      {
                          ???????????
                      }
          

          What should I put at the ?????????

          K Offline
          K Offline
          KBou
          wrote on last edited by
          #4

          I Found It.

                      System.Windows.Media.BrushConverter bc = new System.Windows.Media.BrushConverter();
                      if (\_color.IsNamedColor)
                      {
                          ColorBrush = (System.Windows.Media.Brush)bc.ConvertFromString(\_color.Name);
                      }
                      else
                      {
                          ColorBrush = (System.Windows.Media.Brush)bc.ConvertFromString("#"+\_color.Name);
                      }
          

          When it is not a NamedColor it returns for example ffffff80. When you add a # it recognizes and converts it. Thanks Anyway

          1 Reply Last reply
          0
          • K KBou

            The System.Windows.Media.Brush ColorBrush doesn't contain anything like Color or FromArgb. Neither does the BrushConverter.

            I Offline
            I Offline
            Insincere Dave
            wrote on last edited by
            #5

            Colours and Brushes are two different things. As mentioned above you can access the components of the colour individually to swap between a System.Drawing and a WPF color.

            using (System.Windows.Forms.ColorDialog d = new System.Windows.Forms.ColorDialog())
            {
            d.ShowDialog();
            System.Drawing.Color c = d.Color;
            Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(c.A,c.R,c.G,c.B));
            }

            1 Reply Last reply
            0
            • K KBou

              Hi I'm creating a Windows Forms Application with an WPF Element Host. I want to the user to set the background of an WPF element with use of a ColorDialog. So I need to convert the System.Drawing.Color to a System.Windows.Media.Brush. I got it to work when the Color is a NamedColor (isNamedColor) but else I cannot get it to work.

                          System.Windows.Media.BrushConverter bc = new System.Windows.Media.BrushConverter();
                          if (\_color.IsNamedColor)
                          {
                              ColorBrush = (System.Windows.Media.Brush)bc.ConvertFromString(\_color.Name);
                          }
                          else
                          {
                              ???????????
                          }
              

              What should I put at the ?????????

              A Offline
              A Offline
              appxdev
              wrote on last edited by
              #6
              System.Drawing.Color c1 = new System.Drawing.Color();
                      c1 = System.Drawing.Color.FromName(mycolorString);
                      System.Windows.Media.Color c2 = new Color();
                      c2 = Color.FromArgb(c1.A, c1.R, c1.G, c1.B);
              

              modified on Saturday, April 16, 2011 11:21 AM

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups