Connor Albright
Connor Albright

Reputation: 743

Converting from a color to a brush

How do I convert from system.drawing.color to system.drawing.brushes in vb.net?

Meta question: What is/why the difference between a brush/color/pen?

Upvotes: 10

Views: 36340

Answers (5)

Rob
Rob

Reputation: 3863

Dim myColor As Color    
Dim myBrush As Brush    
Dim myPen As Pen

'From Color to brush/pen    
myBrush = New SolidBrush(myColor)    
myPen = New Pen(myColor)


'From Brush to color/pen    
myPen = New Pen(myBrush)    
myColor = New Pen(myBrush).Color


'From Pen to color/brush    
myColor = myPen.Color    
myBrush = New SolidBrush(myPen.Color)

Upvotes: 5

Rob P.
Rob P.

Reputation: 15091

They are entirely different things.

Here is an article entitled 'Pens, Brushes and Colors' http://msdn.microsoft.com/en-us/library/aa983677(v=vs.71).aspx

Pens
A pen is used to draw lines, curves, and to outline shapes

Brushes
Brushes are objects that are used with a Graphics object to create solid shapes and to render text.

Both Pens and Brushes have a 'Color' that they are using...but you can't turn a Color into a brush. It's like a car. You can't turn 'Red' into a car, but a car can be red.

Upvotes: 5

KeithS
KeithS

Reputation: 71591

A "Brush" is a style of fill drawing, incorporating both a Color and a Pattern. A Pen is similar to a Brush but defines a style of line drawing. To go from a Color to a Brush, you need to create a new Brush and give it the Color. The Brush class itself is abstract; its child classes specify various basic, customizable patterns of drawing. Pens are similar, but as lines are drawn as if they were filled rectangles, a Brush may be necessary to customize the "fill" of the line. The Pen object then has additional properties governing style that are specific to drawing a line. Take a look on MSDN: http://msdn.microsoft.com/en-us/library/d78x2d7s%28v=VS.71%29.aspx

Upvotes: 6

Javed Akram
Javed Akram

Reputation: 15354

pen is used to draw outlines of figure.
brush is used to fill inside area of closed figure.
Color is the look of color.

Brush and Pen may have same color but their role are diffrent

Upvotes: 1

Pondidum
Pondidum

Reputation: 11637

This should do it for you:

'just a solid brush:
Using br = New SolidBrush(Colors.Black)
     e.Graphics.FillRectangle(br, New Rectangle(50, 50, 10, 10))
End Using

'A red -> orange gradient, at 45 degrees:
Using br = New LinearGradientBrush(new Rectangle(50, 50, 10, 10), Color.Red, Color.Orange, 25)
     e.Graphics.FillRectangle(br, New Rectangle(50, 50, 10, 10))
End Using

Upvotes: 15

Related Questions