Click here to Skip to main content
16,018,949 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here I am trying to copy and save the image from picture box.
I am having five picture box.

but I am Hardcoded the "image im = PictureBox1.Image;"
and image name,

this way i can copy and save the image from picturebox1.
How to copy the image from other picture box.
C#
private void MouseClick()
            {     
                PictureBox1.MouseClick += form_MouseClick;
              
                PictureBox2.MouseClick += form_MouseClick;
    
                PictureBox3.MouseClick += form_MouseClick;
    
                PictureBox4.MouseClick += form_MouseClick;

                PictureBox5.MouseClick += form_MouseClick;
            }
    		
    	private void frmRightClick_Disposed(object sender, EventArgs e)
            {
                folderBrowser.ShowDialog();
                string filepath = folderBrowser.SelectedPath;
                Image im = PictureBox1.Image;
                var obj = new Random();
                SaveImage(im, filepath + "\\" + obj.Next() + ".png");
            }
    		
    		private void SaveImage(Image im, string destPath)
            {
                im.Save(destPath, System.Drawing.Imaging.ImageFormat.Png);
            }


What I have tried:

I tried using the sender
C#
private void frmRightClick_Disposed(object sender, EventArgs e)
            {
                folderBrowser.ShowDialog();
                string filepath = folderBrowser.SelectedPath;
                Image im = PictureBox1.Image;
                var obj = new Random();
                SaveImage(im, filepath + "\\" + obj.Next() + ".png");
            }
Posted
Updated 17-Apr-17 5:59am
v2
Comments
CHill60 17-Apr-17 11:58am    
If you can copy an image from pictureBox1 why can't you do it from one of the other pictureBoxes?

1 solution

The sender parameter of the event handler is there for just that reason:
private void form_MouseClick(object sender, EventArgs e)
    {
    PictureBox pb = sender as PictureBox;
    if (pb != null)
        {
        folderBrowser.ShowDialog();
        string filepath = folderBrowser.SelectedPath;
        Image im = pb.Image;
        var obj = new Random();
        SaveImage(im, filepath + "\\" + obj.Next() + ".png");
        }
    }
 
Share this answer
 
Comments
Member 12277263 17-Apr-17 12:13pm    
Thanks, I tried that way, I am getting the pb null all the time
OriginalGriff 17-Apr-17 12:25pm    
Then you aren't clicking on the PictureBox!
Use the debugger to find out what type sender is ... that should help you work out what is going on.
Member 12277263 17-Apr-17 14:59pm    
thanks
Its working
Member 12277263 17-Apr-17 15:01pm    
Here I am saving the Image name with random number, is there any alternatives
OriginalGriff 17-Apr-17 15:33pm    
Loads! What are you trying to do?

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