Re: Set Color of Pen and Brush
-
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.
-
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.
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). -
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.
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) -
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).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; -
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)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.
-
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.
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)