2

The issue I am having is, I cannot get the "e.Graphics" lines of code to work if I try to use them in a button1_Click method. So, I have constructed "Paint" method. Now I can't seem to call the Paint method from the button Click event. Is there a way to do this? I have not been able to find a way to make this work. Or, is there a way to do the e.Graphics functions in the "button1_Click" method?

Thank you

    public void button1_Click(object sender, EventArgs e)
    {
        CrochetPtrnDesign_Paint(this, ?);
    }

    private void CrochetPtrnDesign_Paint(object sender, PaintEventArgs e)
    {
        Rectangle rect = new Rectangle(5, 5,
            ClientSize.Width - 10, ClientSize.Height - 10);
        e.Graphics.DrawRectangle(Pens.Red, rect);

        using (Font font = new Font("Times New Roman", 16, GraphicsUnit.Pixel))
        {
            using (StringFormat sf = new StringFormat())
            {
                // Middle.
                sf.LineAlignment = StringAlignment.Center;  // Middle.

                // Middle/Center.
                sf.Alignment = StringAlignment.Center;      // Center.
                e.Graphics.DrawString("Middle/Center", font, Brushes.Black, rect, sf);
            }
        }
Sean Connelly
  • 135
  • 3
  • 13
  • Can you do something like: `CrochetPtrnDesign_Paint(this, new PaintEventArgs(...))`? – Kidiskidvogingogin Sep 01 '16 at 19:25
  • @Kidiskidvogingogin No this will not work as it will never have a truely valid Graphics object.. Instead he must trigger the `Paint` by doing this: `CrochetPtrnDesign.Invalidate();` – TaW Sep 01 '16 at 19:28

2 Answers2

4

I cannot get the "e.Graphics" lines of code to work if I try to use them in a button1_Click method

That's by design. You're not supposed to be drawing anything while handling the Click event. So calling the Paint event handler is, at the very least, inconvenient.

The right way to do this would be to call Invalidate() while handling Click, and then let Windows decide when to call your Paint event handler. If something should be drawn differently as a result of the Click event, then the Click event handler should update your data to indicate this, so that later when the Paint event handler is called, it knows exactly what to draw at that time.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
4

Case One: Drawing onto Controls:

Provided the Paint event is actually hooked up and not just a piece of code you have copied, this will do the job:

public void button1_Click(object sender, EventArgs e)
{
    CrochetPtrnDesign.Invalidate();
}

Update: since we now know that CrochetPtrnDesign is the Form simply write: this.Invalidate();!

If in doubt about hooking up read this!

Note that creating truely valid PaintEventArgs should only be done by the system..

Case Two: Drawing into Bitmaps:

To draw into a Bitmap you also need a valid Graphics object, but you have to create it yourself:

void DrawStuff(Bitmap bmp)
{
    using (Graphics g = Graphics.FromImage(bmp))
    {
        // draw your stuff
        g.DrawRectangle(...);
    }
    // when done maybe assign it back into a Control..
    pictureBox.Image = bmp;
}

Update:

Assigning the bitmap to the pictureBox.Image will work but runs the risk of leaking the previous image. To avoid this you can use a faile-safe method, maybe like this:

void SetImage(PictureBox pb, Bitmap bmp)
{
    if (pb.Image != null)
    {
        Bitmap tmp = (Bitmap)pb.Image;
        pb.Image = null;
        tmp.Dispose();
    }
    pb.Image = bmp;

}
TaW
  • 53,122
  • 8
  • 69
  • 111