473,325 Members | 2,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Known problem with LinearGradientBrush?

_DD

It seems that there is some odd interaction with LinearGradientBrush
that depends on positioning. A simple test:

---------

Rectangle Square = new Rectangle(.....);

in Paint:

LinearGradientBrush ShadeBrush = new LinearGradientBrush(
Square, Color.White, Color.Black, 90.0F, true);

g.FillRectangle(ShadeBrush, Square);

---------

This should draw a rectangle with White at the top, blending into
Black at the bottom.

This sometimes works as expected, but sometimes draws a one-
pixel BLACK line across the top edge.

Since the same rect is used for the gradient and for the Fill, you'd
think that nothing could go wrong here. Any ideas?

PS: To top it off, It seems position-sensitive. Changing the Y
position of the Square can cause this wto work correctly.
Also changing the vertical size of the Square object can make thiis
display correctly.
Jan 11 '07 #1
8 6459
It seems that there is some odd interaction with LinearGradientBrush
that depends on positioning.
Try turning on the antialiasing. Are you using a rectangle represented
in float? How about a transform on the Graphics object in float? Those
could easily have a rounding/truncation issue, especially without
antialiasing enabled.

Jan 11 '07 #2
This is a known problem. A workaround is to make the brush area slightly
larger than the area to be filled. (1 pixel bigger)
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

_DD wrote:
It seems that there is some odd interaction with LinearGradientBrush
that depends on positioning. A simple test:

---------

Rectangle Square = new Rectangle(.....);

in Paint:

LinearGradientBrush ShadeBrush = new LinearGradientBrush(
Square, Color.White, Color.Black, 90.0F, true);

g.FillRectangle(ShadeBrush, Square);

---------

This should draw a rectangle with White at the top, blending into
Black at the bottom.

This sometimes works as expected, but sometimes draws a one-
pixel BLACK line across the top edge.

Since the same rect is used for the gradient and for the Fill, you'd
think that nothing could go wrong here. Any ideas?

PS: To top it off, It seems position-sensitive. Changing the Y
position of the Square can cause this wto work correctly.
Also changing the vertical size of the Square object can make thiis
display correctly.
Jan 11 '07 #3
_DD
On 11 Jan 2007 08:12:39 -0800, "not_a_commie" <no********@gmail.com>
wrote:
>It seems that there is some odd interaction with LinearGradientBrush
that depends on positioning.

Try turning on the antialiasing. Are you using a rectangle represented
in float? How about a transform on the Graphics object in float? Those
could easily have a rounding/truncation issue, especially without
antialiasing enabled.
I've tried float and int versions of the same. Same result. You'd
think that the int version should work.
Jan 11 '07 #4
_DD
>_DD wrote:
>It seems that there is some odd interaction with LinearGradientBrush
that depends on positioning. A simple test:

Rectangle Square = new Rectangle(.....);

in Paint:
LinearGradientBrush ShadeBrush = new LinearGradientBrush(
Square, Color.White, Color.Black, 90.0F, true);
g.FillRectangle(ShadeBrush, Square);

This sometimes works as expected, but sometimes draws a one-
pixel BLACK line across the top edge.
On Thu, 11 Jan 2007 20:40:24 +0100, "Bob Powell [MVP]"
>This is a known problem
It's a Microsoft bug? Well, at least I only wasted a day and a half on
it (damn, damn, damn). Has anyone filed the bug with Microsoft? (They
don't seem to take those very seriously)

Having assumed that the MS library would have been debugged by now, I
ended up writing lots of test code, converting back and forth from
float to int, etc. Pretty tough debugging, given that the app uses
animation, and it was turning up seemingly at random.

From one test, I found that moving a drawn object from Y=249 to Y=250
introduced the black line. That magic number also changed when the
object's size was changed. Since the graphics in this case require
smooth animation of object position and size, the lines were popping
in an out all over the screen as the objects crossed these magical
borders (what determines that?). It looked horrible, of course.

I didn't turn up any mention of this artifact in google searches. (I
even checked your FAQ, Bob. You may want to add this)
>. A workaround is to make the brush area slightly
larger than the area to be filled. (1 pixel bigger)
I had tried inflating the brush, and it seemed to eliminate the random
line. But it introduced its own problems:

Some of the shaded areas get as narrow as 3 pixels (perspective-shrink
during animation). That means that a 1-pixel miss results in a very
visible 33% change in the outer color. This is not as ugly as the
black lines, but still a noticeable glitch.

And with the inflated brush, there's now no way to get a pure white or
pure black row of pixels on the outer edge of a 3 pixel wide object,
as the crossfade is already at 20% (1 pixel out of 5) when the brush
hits the object edge. But then the bug will pop in and randomly draw a
pure white or black edge.

--

Bob, have you worked with this enough to know:

Does this occur with other, nonlinear, gradient types? That could be
a workaround.

Does antialiasing fix this?

Is this a deeper problem than just two MS guys who didn't round in the
same direction? Early on, I tried writing my own gradient using a
series of Pens, but that seemed to do the same thing. That would
indicate a bug inherent to the internal coordinate mechanism.
Jan 12 '07 #5
_DD
On 11 Jan 2007 08:12:39 -0800, "not_a_commie" <no********@gmail.com>
wrote:
>It seems that there is some odd interaction with LinearGradientBrush
that depends on positioning.

Try turning on the antialiasing. Are you using a rectangle represented
in float? How about a transform on the Graphics object in float? Those
could easily have a rounding/truncation issue, especially without
antialiasing enabled.
I simplified the test to just one integer rectangle and a gradient
brush that used that rectangle. The same integer rectangle is handed
to the gradient brush and to the FillRect. Pretty much what I quoted
in the original post. That's about as simple as you can get. No
transforms. No floats in the test.

I hooked up NumericUpDowns to change the position of the rectangle.
As the rectangle's position changes by one pixel, the black lines (or
bright white lines, depending) pop in an out of existence.

It's like quantum physics or something! Heisenberg's LinearGradient
theory. <g>

It does look like an odd bug in GDI+ internals. I'm surprised that
this is not documented somewhere. I'll check into antialiasing, but it
doesn't seem like it will help, given the simplicity of the test case.

Does anyone know if this happened in earlier GDI (non +)?
Jan 12 '07 #6
Hi,

_DD wrote:
>
It's a Microsoft bug? Well, at least I only wasted a day and a half on
it (damn, damn, damn). Has anyone filed the bug with Microsoft? (They
don't seem to take those very seriously)
In the contrary, there is an excellent response from the Microsoft team
to these inputs from users. Please remember that WPF is a brand new
technology, released in November 2006, and that you must count with
childhood problems.

For bug reports, please refer to the MSDN WPF forum here:
http://forums.microsoft.com/MSDN/Sho...eID=1&PageID=0

Also of value, the WPF community site:
http://wpf.netfx3.com/

You may also want to contact people at Microsoft through their blog, for
example Karsten
http://blogs.msdn.com/karstenj/default.aspx

Greetings,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Jan 12 '07 #7
_DD
On Fri, 12 Jan 2007 10:31:01 +0100, "Laurent Bugnion [MVP]"
<ga*********@bluewin.chwrote:
>Hi,

_DD wrote:
>>
It's a Microsoft bug? Well, at least I only wasted a day and a half on
it (damn, damn, damn). Has anyone filed the bug with Microsoft? (They
don't seem to take those very seriously)

In the contrary, there is an excellent response from the Microsoft team
to these inputs from users. Please remember that WPF is a brand new
technology, released in November 2006, and that you must count with
childhood problems.
Hi Laurent,

Thanks for your reply. I am glad to hear that the WPF team is on top
of things. However I have filed about 10 bug reports in the past,
mostly about VS2005. I have not yet seen a single one resolved. They
are usually dismissed as 'won't fix' or 'by design' (It's not a bug,
it's a feature?).

Some of the reported bugs were easy to reproduce, and were
subsequently reported by others--with similar dismissals.

So it probably depends on which team is handling fixes. I do hope MS
changes their policy though. After the frustration, I have stopped
filing bug reports. I just try to avoid the trouble spots. I suspect
that other developers have arrived at the same conclusion. I can't see
how that helps Microsoft in the long run.
Jan 12 '07 #8
Hi,

_DD wrote:
Hi Laurent,

Thanks for your reply. I am glad to hear that the WPF team is on top
of things. However I have filed about 10 bug reports in the past,
mostly about VS2005. I have not yet seen a single one resolved. They
are usually dismissed as 'won't fix' or 'by design' (It's not a bug,
it's a feature?).

Some of the reported bugs were easy to reproduce, and were
subsequently reported by others--with similar dismissals.

So it probably depends on which team is handling fixes. I do hope MS
changes their policy though. After the frustration, I have stopped
filing bug reports. I just try to avoid the trouble spots. I suspect
that other developers have arrived at the same conclusion. I can't see
how that helps Microsoft in the long run.
Totally understand your frustration. I must say, we were lucky to have
rather good results with bug submissions (not WPF, in this case it was
IE). What we did was going through our regional Microsoft representative
(I work in german Switzerland, we went through Germany). We got a bug
fix a few weeks later. That was pretty good. Of course, working for
Siemens is an advantage.

That said, the WPF team is very very enthusiastic about their work
(well, they should ;-) so the response is excellent. I first contacted
them privately, and got answers to all my queries. If you follow the WPF
developers' blogs, and see what they work on, it's a great way to post
questions and follow-up.

Courage!
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Jan 12 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Daniel | last post by:
Hi, I use paint event to draw gradient on a Panel. But how to get the label to be transaparent on a gradient draw Panel? Thank you. Regards, Daniel
4
by: Adam Maltby | last post by:
Hi, I need to do a transparent label but with a difference. I have drawn on my form a custom background using CreateGraphics as I needed an ellipse looking fill in a rectangle - so the actual...
1
by: Kenneth Siewers Møller | last post by:
Hi there I have a custom label control (GradientLabel) which enherits from Label and basically just paints the background of a label in a gradient. In my code I have the following for the...
1
by: DigitalMythic | last post by:
Just to set the scene, I have a listbox in an application which im working on. The List box will have owner drawn items in it when it has items. To start with the listbox is empty. What Im...
0
by: David Gouge | last post by:
Hi All, appreciate that this is more gdi+ than c# but always seem to get good answers here, so here goes... In a nutshell, all i want to do is add a gradient background to a ListView. I have...
2
by: Robinson | last post by:
Haven't had much luck googling this one: I want to draw a line with a gradient fill, that is a line connecting two shapes in a graphics program, where each shape is a different colour (say, Red...
5
by: cblackburn | last post by:
Hi all, I am in the situation where I have a GraphicsPath that I want to draw onto a System.Drawing.Graphics. I want the line to be coloured in a gradient from one end to the other. I have tried...
2
by: =?Utf-8?B?Y3JlYXZlczA2MjI=?= | last post by:
I have a nested datagrid in a xaml file, the parent datagrid loads the vendor information and the details loads the documents for that vendor in a datagrid. Everything is working fine until I click...
7
by: prpradip | last post by:
I have used ListView in my Form. I have set OwnerDraw = true. And I have the DrawItem Function as private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e) ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.