Smoothing a linear gradient

  • Thread starter Thread starter Jon Slaughter
  • Start date Start date
J

Jon Slaughter

I'm drawing a linear gradient and its very "banded". The colors are far
enough part that it should be plenty smooth enough but I always get the same
number of bands regardless.

I've tried to set

g.InterpolationMode = InterpolationMode.HighQualityBicubic;

g.CompositingQuality = CompositingQuality.HighQuality;

g.SmoothingMode = SmoothingMode.HighQuality;



but doesn't help.



Any ideas?



Thanks,

Jon
 
Jon said:
I'm drawing a linear gradient and its very "banded". The colors are far
enough part that it should be plenty smooth enough but I always get the same
number of bands regardless.

I've tried to set

g.InterpolationMode = InterpolationMode.HighQualityBicubic;

g.CompositingQuality = CompositingQuality.HighQuality;

g.SmoothingMode = SmoothingMode.HighQuality;

I placed the following code in my form's OnPaint method and the
gradient produced was smooth. I even switched my color depth on my
graphics adapter to medium quality and it was still smooth, so I'm not
sure what your problem may be. When creating the gradient brush, is
the width of the brush the same as the width of the area you are
painting? Can you post the code where you create the gradient brush?
Here is my sample code (in the OnPaint method):

LinearGradientBrush lgb = new LinearGradientBrush(new Point(0, 0), new
Point(this.ClientRectangle.Width, 0), Color.Black, Color.White);

e.Graphics.FillRectangle(lgb, this.ClientRectangle);
 
Chris Dunaway said:
I placed the following code in my form's OnPaint method and the
gradient produced was smooth. I even switched my color depth on my
graphics adapter to medium quality and it was still smooth, so I'm not
sure what your problem may be. When creating the gradient brush, is
the width of the brush the same as the width of the area you are
painting? Can you post the code where you create the gradient brush?
Here is my sample code (in the OnPaint method):

LinearGradientBrush lgb = new LinearGradientBrush(new Point(0, 0), new
Point(this.ClientRectangle.Width, 0), Color.Black, Color.White);

e.Graphics.FillRectangle(lgb, this.ClientRectangle);

This is my code. I believe it is essentially the same as yours.
Graphics g = e.Graphics;

g.InterpolationMode = InterpolationMode.HighQualityBicubic;

g.CompositingQuality = CompositingQuality.HighQuality;

g.SmoothingMode = SmoothingMode.HighQuality;

Brush B = new LinearGradientBrush(new Rectangle(Point.Empty, this.Size), c1,
c2, 45f);

g.FillRectangle(B, this.ClientRectangle);

B.Dispose();



The bands look like they are about 32 pixels wide.



Thanks,

Jon
 
Jon said:
This is my code. I believe it is essentially the same as yours.
Graphics g = e.Graphics;

g.InterpolationMode = InterpolationMode.HighQualityBicubic;

g.CompositingQuality = CompositingQuality.HighQuality;

g.SmoothingMode = SmoothingMode.HighQuality;

Brush B = new LinearGradientBrush(new Rectangle(Point.Empty, this.Size), c1,
c2, 45f);

g.FillRectangle(B, this.ClientRectangle);

B.Dispose();

The bands look like they are about 32 pixels wide.

Does each band go from the start color to the end color? Is this.Size
what you expect it to be?

When I first created my sample code above, my second Point made the
width of the gradient small so when I painted the entire form, I saw
the gradient repeat across the form. That's when I changed the second
Point to have an X coordinate that equalled the width of the client
area. Could that be what is happening?
 
Chris Dunaway said:
Does each band go from the start color to the end color? Is this.Size
what you expect it to be?

When I first created my sample code above, my second Point made the
width of the gradient small so when I painted the entire form, I saw
the gradient repeat across the form. That's when I changed the second
Point to have an X coordinate that equalled the width of the client
area. Could that be what is happening?

No, Becaues the top right corner is the proper color and the bottom right
ist he proper color and there is no repeat of those colors inbetween. It
looks like the title bar gradient in XP with no themes(I suppose it happens
in 2k too). But I cannot see any bands in the title bar(or barely) but its
obvious in my app.

this.Size returns the width and height of the form(Since I'm doing it in the
form) so its pretty large.

Thanks,
Jon
 

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

Back
Top