Custom Brushes

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi!
Can anyone give me some hints on how to create a custom brush deriving from
the Brush class?

Thanks
Cristian Mori
 
Hi Cristian,

Thanks for your post.

Based on my understanding, you want to inherit from System.Drawing.Brush
class to implement a customized brush.

However, we can not do this. This is because the designer of
System.Drawing.Brush class do not want us to inherit from it. If you use
Reflector to view the source code of System.Drawing.Brush class. You will
see that its constructor is defined like this:
internal Brush()
{
this.nativeBrush = IntPtr.Zero;
}

Yes, it is marked with "internal", which means that only classes in the
same assembly can access it. That is why the following classes inherited
from System.Drawing.Brush class:
System.Drawing.Drawing2D.HatchBrush
System.Drawing.Drawing2D.LinearGradientBrush
System.Drawing.Drawing2D.PathGradientBrush
System.Drawing.SolidBrush
System.Drawing.TextureBrush

Because they all reside in the System.Drawing.dll assembly.

But if we want to inherit from Brush class, we will get the following
compile time error:
"'System.Drawing.Brush.Brush()' is inaccessible due to its protection level"

This issue is documented in the KB below:
"You receive error messages when you try to inherit from a class that
contains only private constructors in Visual Studio .NET"
http://support.microsoft.com/?kbid=833898

Hope this is clarify and helpful, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
As Jeffry points out, Brushes are not good derivation targets.

Perhaps if you explain exactly what it is you want to do we can advise
you...

--
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.
 
Since as stated by Jeffrey the gradientbrush is flawed, I just wanted to
create another gradient brush that simulate a goraud shading , without the
centercolor as in the gradientbrush.

If you have any advise....

Thanks!
Cristian Mori
 
The only solution I can think of would be to generate a bitmap with the
shading on it and then draw that into an area clipped by a region.

--
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.
 
Back
Top