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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Re: Set Color of Pen and Brush

Re: Set Color of Pen and Brush

Scheduled Pinned Locked Moved C#
helpquestion
6 Posts 3 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.
  • A Offline
    A Offline
    ASPnoob
    wrote on last edited by
    #1

    Hi All, The Pen and the Brush objects' Color property needs to be set when they are created. The problem is the color values one can use is very limited. Is there a way to send the color code to the Pen or the Brush object dynamically after choosing a color from a palette? Thanks in advance for your reply.

    B D 2 Replies Last reply
    0
    • A ASPnoob

      Hi All, The Pen and the Brush objects' Color property needs to be set when they are created. The problem is the color values one can use is very limited. Is there a way to send the color code to the Pen or the Brush object dynamically after choosing a color from a palette? Thanks in advance for your reply.

      B Offline
      B Offline
      Bernhard Hiller
      wrote on last edited by
      #2

      You do that by creating a new Pen/Brush with that selected color (do not forget to Dispose() of the old Pen/Brush objects for freeing the resources).

      A 1 Reply Last reply
      0
      • A ASPnoob

        Hi All, The Pen and the Brush objects' Color property needs to be set when they are created. The problem is the color values one can use is very limited. Is there a way to send the color code to the Pen or the Brush object dynamically after choosing a color from a palette? Thanks in advance for your reply.

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        Have a look at the FromArgb[^] method of the Color struct, you can set it to anywhere in the 32bit ARGB range. So, you set the Color and pass that to the Pen... Edit:

        using(Pen pen = new Pen(Color.FromArgb(0)))
        {
        //
        }

        replacing the zero with the value you want

        Dave
        Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

        A 1 Reply Last reply
        0
        • B Bernhard Hiller

          You do that by creating a new Pen/Brush with that selected color (do not forget to Dispose() of the old Pen/Brush objects for freeing the resources).

          A Offline
          A Offline
          ASPnoob
          wrote on last edited by
          #4

          Hi, thanks for responding. I was already aware of setting the color of the pen or the brush object using the Color.color name method. What I would like to do instead of using color names such as blue, green, red, etc. is pass in the RGB code to the property like the following

          Pen myPen = new Pen;
          myPen = System.Drawing.Color.F08080;

          1 Reply Last reply
          0
          • D DaveyM69

            Have a look at the FromArgb[^] method of the Color struct, you can set it to anywhere in the 32bit ARGB range. So, you set the Color and pass that to the Pen... Edit:

            using(Pen pen = new Pen(Color.FromArgb(0)))
            {
            //
            }

            replacing the zero with the value you want

            Dave
            Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

            A Offline
            A Offline
            ASPnoob
            wrote on last edited by
            #5

            Hi, thanks for your reply. The example in the MSDN below suggests that I must manually set the value of FromArgb(): public void FromArgb4(PaintEventArgs e) { Graphics g = e.Graphics; // Transparent red, green, and blue brushes. SolidBrush trnsRedBrush = new SolidBrush(Color.FromArgb(0x78FF0000)); SolidBrush trnsGreenBrush = new SolidBrush(Color.FromArgb(0x7800FF00)); SolidBrush trnsBlueBrush = new SolidBrush(Color.FromArgb(0x780000FF)); } You've just given me an idea; I'm going to allow users to set the color of the brush or pen object by clicking on the color palette object. This will grab the color code then pass it as a parameter to the FromArgb4() method.

            D 1 Reply Last reply
            0
            • A ASPnoob

              Hi, thanks for your reply. The example in the MSDN below suggests that I must manually set the value of FromArgb(): public void FromArgb4(PaintEventArgs e) { Graphics g = e.Graphics; // Transparent red, green, and blue brushes. SolidBrush trnsRedBrush = new SolidBrush(Color.FromArgb(0x78FF0000)); SolidBrush trnsGreenBrush = new SolidBrush(Color.FromArgb(0x7800FF00)); SolidBrush trnsBlueBrush = new SolidBrush(Color.FromArgb(0x780000FF)); } You've just given me an idea; I'm going to allow users to set the color of the brush or pen object by clicking on the color palette object. This will grab the color code then pass it as a parameter to the FromArgb4() method.

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #6

              That's the correct way to do it. Make sure you put all those brushes in using block(s) or explicitly call Dispose() on each one before you exit your method - unless you are caching them in private fields, in which case you need to make your class IDisposable and implement the Dispose pattern.

              Dave
              Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

              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