In this tutorial, i’ll show you How to set an image to picturebox when button clicked.
You can programmatically set the image displayed by a Windows Forms PictureBox control.
Set the Image property using the FromFile method of the Image class.
Solution:
C# Code:
1 2 3 4 5 6 7 |
private void button1_Click(object sender, EventArgs e) { pictureBox1.Image = Image.FromFile(@"../../Images/android.jpg"); pictureBox1.SizeMode=PictureBoxSizeMode.StretchImage; } |
To clear a graphic
First, release the memory being used by the image, and then clear the graphic. Garbage collection will free up the memory later if memory management becomes a problem.
Example:
1 2 3 4 5 6 7 8 9 10 |
private void button2_Click(object sender, EventArgs e) { if (pictureBox1.Image != null) { pictureBox1.Image.Dispose(); pictureBox1.Image = null; } } |