Reputation: 348
I want display an image in my picture box but when I run the code everything else works except the image isn't displayed. Here is the relevant code:
Image[] deadWoman = new Image[5]; //this array will hold the images of bit and pieces of katie
deadWoman[0] = Image.FromFile("F:/Jers Hangman Game/Jers Hangman Game/Resources/katie-hopkins.jpeg");
private void MainPic_Paint(object sender, PaintEventArgs e)
{
Graphics katie = e.Graphics; // creates a graphics object for the picture box
if (numWrongGuesses > 0)
{
e.Graphics.DrawImage(deadWoman[0], 20, 20,60,60);
}
}
Upvotes: 0
Views: 1912
Reputation: 156928
I guess the image is never repainted, that's why you don't see it when numWrongGuesses
is updated. You should Invalidate()
the PictureBox
in order to see the update.
I would advise to set the image though, and simply use Visible = true
and Visible = false
for showing and hiding. You could even set the BackgroundImage
if you need to create some overlay effect.
Upvotes: 2
Reputation: 14820
You don't override Paint
in order to put an Image
object into a PictureBox
. Just use the property:
MainPic.Image = deadWoman[0];
You can also do this in the WinForms designer, as long as the Image
is a resource.
Also, you can hide and show your image by the .Visible
property:
MainPic.Visible = numWrongGuesses > 0;
Upvotes: 0