Graphics Questions

E

eBob.com

I know nothing about graphics, but I am trying to fill a panel with a path
gradient brush. From looking at and playing with an example I found it
appears that I have to establish a Graphics object using Graphics.FromHwnd
for my form. Then the coordinates I specify have to be relative to the
form. But since it is the panel I want to paint on I am wondering if there
is some way to get a graphics object which corresponds to a Control such
that I could then specify coordinates relative to the Control (panel in my
case).

Of course I could use Control.Location to translate the Control coordinates
to Form coordinates. But what if the Control is contained within another
Control, which might itself be contained within another Control etc.?
Handling the general case seems to get messy. (I know that there are other
Graphics.Fromxxxxxxxx methods but none sound like they take a Control as an
argument.)

Also, I use my PathGradientBrush (pgbr) like this:
myGraphics.FillRectangle(pgbr, 100, 100, 1040, 1040).
But in my experiments those last four integer arguments which define the
rectangle don't seem to do anything. I can give them any values and I
always get the same result. The only coordinates which seem to matter are
those specified in defining the path to the PathGradientBrush constructor.
What am I missing? Surely those arguments defining the rectangle must be
there for some puropse.

I'd appreciate any help anyone can offer. Thanks, Bob
 
T

Tom Shelton

I know nothing about graphics, but I am trying to fill a panel with a path
gradient brush. From looking at and playing with an example I found it
appears that I have to establish a Graphics object using Graphics.FromHwnd
for my form. Then the coordinates I specify have to be relative to the
form. But since it is the panel I want to paint on I am wondering if there
is some way to get a graphics object which corresponds to a Control such
that I could then specify coordinates relative to the Control (panel in my
case).

I'm no expert, but typically you might write code that looks something like:

Using pg As Graphics = myPanel.CreateGraphics()
Using bmp As New Bitmap(myPanel.ClientRectangle.Width, myPanel.ClientRectangle.Height)
Using bg As Graphics = Graphics.FromImage(bmp)
bg.Clear(myPanel.BackColor)
' draw stuff you want to draw on bg...

' update the panel
pg.DrawImageUnscaled (bmp, 0, 0)
End Using
End Using
End Using

Unless your in the Panels paint event, then you would drop pg and use the
e.Graphics parameter...
Of course I could use Control.Location to translate the Control coordinates
to Form coordinates. But what if the Control is contained within another
Control, which might itself be contained within another Control etc.?
Handling the general case seems to get messy. (I know that there are other
Graphics.Fromxxxxxxxx methods but none sound like they take a Control as an
argument.)

Also, I use my PathGradientBrush (pgbr) like this:
myGraphics.FillRectangle(pgbr, 100, 100, 1040, 1040).
But in my experiments those last four integer arguments which define the
rectangle don't seem to do anything. I can give them any values and I
always get the same result. The only coordinates which seem to matter are
those specified in defining the path to the PathGradientBrush constructor.
What am I missing? Surely those arguments defining the rectangle must be
there for some puropse.

Not sure on that one - never played with the PathGradientBrush - but, maybe if
I get time I'll see what I can figure out :)
I'd appreciate any help anyone can offer. Thanks, Bob

Well, hope this helps somewhat. I've been doing a couple of little games
lately, just to get the feel for this stuff :)
 
E

eBob.com

Hi Tom,

Thanks so much for your help. Panel.CreateGraphics() is exactly what I was
looking for.

I've found out that PathGradientBrush does not do exactly what I wanted when
used to fill a tall, skinny rectangle. I want light at the outside,
vertical edges and dark in the center. So I am using two
LinearGradientBrushes, the one used for the left side goes from light to
dark and the one used for the right side goes from dark to light.

Thanks again for your help. Bob
 
A

Alex Clark

Bob,
I've found out that PathGradientBrush does not do exactly what I wanted
when used to fill a tall, skinny rectangle. I want light at the outside,
vertical edges and dark in the center. So I am using two
LinearGradientBrushes, the one used for the left side goes from light to
dark and the one used for the right side goes from dark to light.

You can set properties on the LinearGradientBrush to specifically set
gradient-stop points, which are expressed as colors with percentages (value
between 0 and 1). So you could set four colors, red white blue and yellow,
at points 0, 0.3, 0.6, and 1, and it would create a gradient brush that
evenly faded across those four colors.

So for what you want you'd need 3 GradientStops - the first and last would
be Color.White with stop values of 0 and 1, and the middle one would be
Color.Black (or whatever) with a stop value of 0.5.

HTH,
Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top