Hi..........

I want to clear the image inside the picturebox on button click......
Can anyone tell me how can i do this.............?:)

can be something like below, check the attached project too :

Form1.cs :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ClearPictureBox
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			pictureBox1.Hide();
		}

		private void button2_Click(object sender, EventArgs e)
		{
			pictureBox1.Show();
		}
	}
}

Form1.Designer.cs :

namespace ClearPictureBox
{
	partial class Form1
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.button1 = new System.Windows.Forms.Button();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.button2 = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(68, 224);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(75, 23);
			this.button1.TabIndex = 1;
			this.button1.Text = "Clear Image";
			this.button1.UseVisualStyleBackColor = true;
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// pictureBox1
			// 
			this.pictureBox1.Image = global::ClearPictureBox.Properties.Resources._0012;
			this.pictureBox1.Location = new System.Drawing.Point(33, 12);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(280, 189);
			this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
			this.pictureBox1.TabIndex = 0;
			this.pictureBox1.TabStop = false;
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(169, 224);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(75, 23);
			this.button2.TabIndex = 2;
			this.button2.Text = "Show Image";
			this.button2.UseVisualStyleBackColor = true;
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// Form1
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(352, 277);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.pictureBox1);
			this.Name = "Form1";
			this.Text = "Form1";
			((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
			this.ResumeLayout(false);

		}

		#endregion

		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
	}
}

One other way:

private void button5_Click(object sender, EventArgs e)
    {
      pictureBox1.Image = null;
    }

sknake your obviously more pro than I but this could work out as well, right? =]

{
   pictureBox1.Dispose();
}

sorry my internet was laggy and it didnt go as fast so i pressed submit twice, i don't know how to delete this post so srry

sorry if this is double post but try this as well:

to make it go ''away'' or inivisble

{
   pictureBox1.Visible = false;
}

and this to make it visible again ;D

{
   pictureBox1.Visible = true;
}

Try it, but I know the post above this one works =]
the one with dispose() unless it just deletes it ;o

Thank you all for your replies..................
well i have one picturebox on form1 and another on form2, and i want to open both picturebox on form one on top and bottom panel respectively...And want to clear both picturebox simultaneosly on button click.
but the code you gave to me only valid and clear one picturebox, which is on form1...it is not applicable when form2's picturebox also open in form1.........
Can you please help me in this.............:pretty:

stick a public method on Form2 to clear the picture box:

public void ClearPictureBox()
{
  this.PictureBox.Image = null;
}

Then call the public method on Form2 from Form1.

sknake your obviously more pro than I but this could work out as well, right? =]

{
   pictureBox1.Dispose();
}

Not really. That frees the picture box. He asked how to hide it, not destroy it. The picture box will be unavailable after you dispose it.

This article has been dead for over six months. Start a new discussion instead.