Click here to Skip to main content
15,896,315 members
Home / Discussions / C#
   

C#

 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
Som Shekhar15-May-10 0:22
Som Shekhar15-May-10 0:22 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
Pete O'Hanlon15-May-10 5:55
mvePete O'Hanlon15-May-10 5:55 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
PIEBALDconsult15-May-10 5:00
mvePIEBALDconsult15-May-10 5:00 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
AspDotNetDev17-May-10 17:21
protectorAspDotNetDev17-May-10 17:21 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
OriginalGriff17-May-10 21:26
mveOriginalGriff17-May-10 21:26 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
AspDotNetDev17-May-10 21:33
protectorAspDotNetDev17-May-10 21:33 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
OriginalGriff17-May-10 21:49
mveOriginalGriff17-May-10 21:49 
AnswerRe: So I Tried Graphics Tonight For The First Time Pin
DaveyM6914-May-10 23:18
professionalDaveyM6914-May-10 23:18 
Hi Roger,

All the advice you've been given is correct. I just thought I'd put my 2c in...

Graphics objects, Pens & Brushes should be disposed of unless they are available already as a property/parameter. If you draw in OnPaint you can use the e.Graphics property and there is no need to dispose it. If however you are drawing elsewhere and using CreateGraphics then you should dispose of it either explicitly, or implicitly by using a using block. The same for Pens and Brushes, if you are using the System ones as in your code then there is no need to dispose, but if you create your own then you must dispose of them.

If it's a one off drawing I normally drop it into the OnPaint handler as has been suggested. If it's likely to be used several times I create a class/struct with a Draw method that takes any useful parameters including a Graphics instance and call that from OnPaint. Triggering OnPaint is easily done by calling Invalidate.

Something like this gives you a reusable Rectangle/Ellipse that can be drawn easily and no memory leaks.
C#
using System;
using System.Drawing;
using System.Windows.Forms;

public partial class FormFlowCalcs : Form
{
    private RectangleWithEllipseUnfilled rectangleWithEllipseUnfilled;
    private Point rectangleWithEllipseUnfilledLocation;
    private bool rectangleWithEllipseUnfilledVisible;

    public FormFlowCalcs()
    {
        InitializeComponent();
        Size = new Size(800, 600);
        rectangleWithEllipseUnfilled = new RectangleWithEllipseUnfilled(
            Color.Black, Color.Red, new Size(200, 200));
        rectangleWithEllipseUnfilledLocation = new Point(200, 200);
        rectangleWithEllipseUnfilledVisible = false;
    }

    private void buttonToggle_Click(object sender, EventArgs e)
    {
        rectangleWithEllipseUnfilledVisible = !rectangleWithEllipseUnfilledVisible;
        Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        if (rectangleWithEllipseUnfilledVisible)
            rectangleWithEllipseUnfilled.Draw(rectangleWithEllipseUnfilledLocation, e.Graphics);
    }
}

public struct RectangleWithEllipseUnfilled
{
    private Color ellipseColor;
    private Color rectangleColor;
    private Size size;

    public RectangleWithEllipseUnfilled(Color ellipseColor, Color rectangleColor, Size size)
    {
        this.ellipseColor = ellipseColor;
        this.rectangleColor = rectangleColor;
        this.size = size;
    }

    public Color EllipseColor
    {
        get { return ellipseColor; }
    }
    public Color RectangleColor
    {
        get { return rectangleColor; }
    }
    public Size Size
    {
        get { return size; }
    }

    public void Draw(Point location, Graphics graphics)
    {
        Rectangle drawBounds = new Rectangle(location, size);
        using (Pen ellipsePen = new Pen(ellipseColor))
        {
            graphics.DrawEllipse(ellipsePen, drawBounds);
        }
        using (Pen rectanglePen = new Pen(rectangleColor))
        {
            graphics.DrawRectangle(rectanglePen, drawBounds);
        }
    }
}

Dave

If this helped, please vote & accept answer!


Binging is like googling, it just feels dirtier. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

AnswerRe: So I Tried Graphics Tonight For The First Time Pin
Luc Pattyn15-May-10 2:18
sitebuilderLuc Pattyn15-May-10 2:18 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
Roger Wright15-May-10 4:46
professionalRoger Wright15-May-10 4:46 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
Luc Pattyn15-May-10 5:24
sitebuilderLuc Pattyn15-May-10 5:24 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
Som Shekhar15-May-10 5:04
Som Shekhar15-May-10 5:04 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
Luc Pattyn15-May-10 5:28
sitebuilderLuc Pattyn15-May-10 5:28 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
Roger Wright15-May-10 6:10
professionalRoger Wright15-May-10 6:10 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
Pete O'Hanlon15-May-10 5:57
mvePete O'Hanlon15-May-10 5:57 
AnswerRe: So I Tried Graphics Tonight For The First Time Pin
PIEBALDconsult15-May-10 5:06
mvePIEBALDconsult15-May-10 5:06 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
Luc Pattyn15-May-10 5:25
sitebuilderLuc Pattyn15-May-10 5:25 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
Som Shekhar15-May-10 5:36
Som Shekhar15-May-10 5:36 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
PIEBALDconsult15-May-10 13:09
mvePIEBALDconsult15-May-10 13:09 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
PIEBALDconsult15-May-10 13:09
mvePIEBALDconsult15-May-10 13:09 
QuestiondataGridView filtering with IList. Pin
mprice21414-May-10 10:50
mprice21414-May-10 10:50 
AnswerRe: dataGridView filtering with IList. Pin
Henry Minute15-May-10 1:22
Henry Minute15-May-10 1:22 
GeneralRe: dataGridView filtering with IList. Pin
mprice21415-May-10 15:46
mprice21415-May-10 15:46 
GeneralRe: dataGridView filtering with IList. Pin
Henry Minute16-May-10 0:23
Henry Minute16-May-10 0:23 
GeneralRe: dataGridView filtering with IList. Pin
mprice21416-May-10 15:57
mprice21416-May-10 15:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.