How do I add a picture in a picturebox in code?
I am trying to make like a little flag guesser and I want the picture to change each time I answer or open the program. How can I do this? My images are now in the debug file.
Image propertyImageprivate void SetRandomPicture()
{
var index = Random.Shared.Next(0, _images.Count);
pictureBox1.Image = _images[index];
}
private void button1_Click(object sender, EventArgs e)
{
SetRandomPicture();
}
private void Form1_Load(object sender, EventArgs e)
{
LoadAllImages();
SetRandomPicture();
}
private void LoadAllImages()
{
foreach (var file in Directory.EnumerateFiles("images"))
{
_images.Add(Image.FromFile(file));
}
}