Click here to Skip to main content
15,898,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,
I am Developing a windows application,where i need to change image dynamically in the picture box.i have added a custom folder named ImagesNew.in that folder i have 2 images,on button click i need to change the image.How do i do that.All your Suggestions are appreciable..
Posted

You need to set the ImageLocation property of the PictureBox in your button click handler:

C#
protected void Button_Click(object sender, EventArgs e)
{
    this.PictureBox1.ImageLocation = "PATH TO IMAGE HERE";
}


http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox%28v=vs.100%29.aspx[^]
 
Share this answer
 
Assuming you are using a PictureBox control to display the image, in your button click event handler you would update the Image property to the desired image.
 
Share this answer
 
Well, start like this, on form load or other event get all the files in given dir:

C#
// Get files from directory (with specified extension)
string[] filePaths = Directory.GetFiles(@"c:\MyDir\", "*.bmp");


set the first item of the array in the picture box and the current item index in the page.
C#
int currentItem = 0;
int maxItems;
...
maxItems = filePaths.Length; 

then set the image in picture box
C#
Image image = Image.FromFile(filePaths[maxItems]);

pictureBox1.Image = image;
        pictureBox1.Height = image.Height;
        pictureBox1.Width = image.Width;


Set the next image on every click:

C#
Image image = Image.FromFile(filePaths[maxItems]);

pictureBox1.Image = image;
        pictureBox1.Height = image.Height;
        pictureBox1.Width = image.Width;
maxItems++;


This is a rough explanation, it is on you to experiment a bit.

Cheers
 
Share this answer
 
C#
protected void Button_Click(object sender, EventArgs e)
{
    this.PictureBox1.ImageLocation = getrandomfile("C:\images")
}


private string getrandomfile(string path)
        {
            ArrayList al = new ArrayList();
            DirectoryInfo di = new DirectoryInfo(path);
            FileInfo[] rgFiles = di.GetFiles("*.*");
            foreach (FileInfo fi in rgFiles)
            {
                al.Add(fi.FullName);
            }
            Random r = new Random();
            int x = r.Next(0,al.Count);

            return al[x].ToString();
        }

will actually gives random image each time.
 
Share this answer
 
v2
in your page declare a variable to track which no image you are displying. here is the code

C#
public partial class Form1 : Form
    {
        int imgNum = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            setImage();
        }

protected void setImage()
        {
            int Maxitem=0;
            string[] filePaths = Directory.GetFiles(@"D:\MyDir\", "*.jpg");
            Maxitem=filePaths.Length;

            if (imgNum >= Maxitem) imgNum = 0;
            pictureBox1.ImageLocation = filePaths[imgNum].ToString();
            imgNum += 1;
        }
}
 
Share this answer
 
v2
i give you a link here which will solve your problem.

C# Save and Load Image from Database[^]
 
Share this answer
 
VB
Private MyImage As Bitmap

 Public Sub ShowMyImage(fileToDisplay As String, xSize As Integer, _
                        ySize As Integer)
     ' Sets up an image object to be displayed.
     If (MyImage IsNot Nothing) Then
         MyImage.Dispose()
     End If

     ' Stretches the image to fit the pictureBox. 
     pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
     MyImage = New Bitmap(fileToDisplay)
     pictureBox1.ClientSize = New Size(xSize, ySize)
     pictureBox1.Image = CType(MyImage, Image)
 End Sub
 
Share this answer
 
v2
Comments
fjdiewornncalwe 4-Apr-13 15:20pm    
Plagiarised from source

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900