Click here to Skip to main content
15,922,584 members
Home / Discussions / C#
   

C#

 
AnswerRe: removing the changes done to an image Pin
Richard MacCutchan14-May-10 21:34
mveRichard MacCutchan14-May-10 21:34 
AnswerRe: removing the changes done to an image Pin
riced15-May-10 0:43
riced15-May-10 0:43 
AnswerRe: removing the changes done to an image Pin
Sandeep Mewara15-May-10 19:27
mveSandeep Mewara15-May-10 19:27 
Questionproblem with attaching an existing project to another project Pin
prasadbuddhika14-May-10 20:42
prasadbuddhika14-May-10 20:42 
AnswerRe: problem with attaching an existing project to another project Pin
#realJSOP14-May-10 23:21
professional#realJSOP14-May-10 23:21 
AnswerRe: problem with attaching an existing project to another project Pin
PIEBALDconsult15-May-10 5:23
mvePIEBALDconsult15-May-10 5:23 
AnswerRe: problem with attaching an existing project to another project Pin
Luc Pattyn15-May-10 7:04
sitebuilderLuc Pattyn15-May-10 7:04 
AnswerRe: problem with attaching an existing project to another project Pin
prasadbuddhika16-May-10 22:02
prasadbuddhika16-May-10 22:02 
QuestionSo I Tried Graphics Tonight For The First Time Pin
Roger Wright14-May-10 19:42
professionalRoger Wright14-May-10 19:42 
AnswerRe: So I Tried Graphics Tonight For The First Time Pin
AspDotNetDev14-May-10 20:09
protectorAspDotNetDev14-May-10 20:09 
AnswerRe: So I Tried Graphics Tonight For The First Time Pin
AspDotNetDev14-May-10 20:14
protectorAspDotNetDev14-May-10 20:14 
AnswerRe: So I Tried Graphics Tonight For The First Time Pin
Som Shekhar14-May-10 20:30
Som Shekhar14-May-10 20:30 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
AspDotNetDev14-May-10 21:01
protectorAspDotNetDev14-May-10 21:01 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
Pete O'Hanlon14-May-10 22:00
mvePete O'Hanlon14-May-10 22:00 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
OriginalGriff14-May-10 22:22
mveOriginalGriff14-May-10 22:22 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
Som Shekhar14-May-10 22:41
Som Shekhar14-May-10 22:41 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
OriginalGriff14-May-10 23:40
mveOriginalGriff14-May-10 23:40 
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)

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.