none
system.drawing.drawing2d and lineargradientbrush RRS feed

  • Question

  • I've been reading and learning about graphics and vb and have found multiple references to a .net dll called system.drawing.drawing2d that does not exist on any computer I have looked at. To use the function LinearGradientBrush according to MSDN it resides in system.drawing.drawing2d and they talk about all over the site but no such .dll exists on any of my computers?? When i call the function LinearGradientBrush I get a 

    Type 'LinearGradientBrush' is not defined

    error. I have included the reference system.drawing but it does not seem to be there so I go to MSDN which tells me is system.drawing.drawing2d but no such animal can be found

    Cannot seem to locate this for download either?

    Please help


    • Edited by Rick -D Tuesday, July 21, 2015 7:54 PM
    Tuesday, July 21, 2015 7:51 PM

Answers

  • I see, you're using VB, not C#. VB needs Imports instead of using:

    Imports System.Drawing.Drawing2D
    
    Public Class Form1
        Protected Overrides Sub OnPaint(e As PaintEventArgs)
            MyBase.OnPaint(e)
            Using brush As New LinearGradientBrush(New Point(0, 0), New Point(0, 100), Color.Black, Color.Red)
                e.Graphics.FillRectangle(brush, ClientRectangle)
            End Using
        End Sub
    End Class
    
    Sure, you could also write Drawing2D.LinearGradientBrush as you discovered but usually it's less convenient.

    • Marked as answer by Rick -D Tuesday, July 21, 2015 8:58 PM
    Tuesday, July 21, 2015 8:55 PM
    Moderator

All replies

  • System.Drawing.Drawing2D is not a dll, it's a namespace. All you need is to have a reference to System.Drawing.dll (which in a WinForms project already exists) and add a "using System.Drawing.Drawing2D;" to your code. Quick example, note the "using":

    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;
    
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }
    
        protected override void OnPaint(PaintEventArgs e) {
            base.OnPaint(e);
    
            using (var brush = new LinearGradientBrush(new Point(0, 0), new Point(0, 100), Color.Black, Color.Red)) {
                e.Graphics.FillRectangle(brush, ClientRectangle);
            }
        }
    }
    

    Tuesday, July 21, 2015 8:02 PM
    Moderator
  • I'm using Visual Studio 2008 to create a form based app. I was unable to use the above reference anywhere on in my forms code without error.

    However I was able to make is work by fully declaring the function. Yours and something else I found led me to the solution. So thank you for your help and quick response.

    Here is what worked for me.

    Using br As New Drawing2D.LinearGradientBrush(

    Apparently I needed to preface the function withe Drawing2D. I also found that I needed to do this for a lot of brush functions in the Drawing2D namespace. Not sure why my system is working different than the references I see on MSDN and yours as well but at least I found my solution. Thanks again


    Tuesday, July 21, 2015 8:48 PM
  • I see, you're using VB, not C#. VB needs Imports instead of using:

    Imports System.Drawing.Drawing2D
    
    Public Class Form1
        Protected Overrides Sub OnPaint(e As PaintEventArgs)
            MyBase.OnPaint(e)
            Using brush As New LinearGradientBrush(New Point(0, 0), New Point(0, 100), Color.Black, Color.Red)
                e.Graphics.FillRectangle(brush, ClientRectangle)
            End Using
        End Sub
    End Class
    
    Sure, you could also write Drawing2D.LinearGradientBrush as you discovered but usually it's less convenient.

    • Marked as answer by Rick -D Tuesday, July 21, 2015 8:58 PM
    Tuesday, July 21, 2015 8:55 PM
    Moderator
  • That's the magic right there and you are correct it's much more convenient to specify the import rather than write it the other way for every function you want to call,

    Thanks again and the response time was incredible. 

    You Rock Mike!

    Tuesday, July 21, 2015 8:59 PM