Darren Young
Darren Young

Reputation: 11100

C# redrawing graphics on winform

I have a bitmap loaded onto a winform via an imagebox. When I want to update the image I try as such:

imagebox.image = null;
draw();  //implements the drawing of the bitmap and assigns to the imagebox/winform

However, this will only work if I hide and then show the form again? How can I get around this?

EDIT: Problem resolved.

Upvotes: 0

Views: 2195

Answers (3)

Ian
Ian

Reputation: 34549

You should check out this link Bob Powell GDI+ FAQ. It's got some clear instructions to some of the GDI basics, this link goes to a PictureBox drawing example.

Basically you want to be attaching to the Paint event for the ImageBox and calling Invalidate() everytime you want a re-draw.

Upvotes: 1

Hans Passant
Hans Passant

Reputation: 942358

Make the image property setter smarter. For example:

private Image mImage;

public Image Image {
    get { return mImage; }
    set { 
        mImage = value;
        Invalidate();
    }
}

Now the visible image refreshes automatically.

Upvotes: 1

Øyvind Bråthen
Øyvind Bråthen

Reputation: 60744

Have you tried making a call to Invalidate(imagebox.Bounds); at the end of the draw() method?

That should force a repaint, so you don't have to hide and show the form again.

Upvotes: 1

Related Questions