Drag an image from the Picture Box to the Windows folder

AMER SAID 396 Reputation points
2022-02-18T02:50:46.237+00:00

I have an code that pulls an image from the Pictures box into the specified Windows folder. The problem is that my program path appears as part of the image name. I ask that no part of the program path appears as part of the image names.
I remove programDirectory + but nothing happen .

//MouseDown event
 if (e.Button == MouseButtons.Left && Pic_Box.Image != null)
            {



                var programFullPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
                var programDirectory = System.IO.Path.GetDirectoryName(programFullPath);
                var pic = (PictureBox)sender;


                string a = DateTime.Now.ToString("yyyy-MM-dd") + ".png";
                Pic_Box.Image.Save(programDirectory  + a.ToString());
                var files = new string[] { programDirectory + a.ToString() };
                var res = pic.DoDragDrop(new DataObject(DataFormats.FileDrop, files), DragDropEffects.Copy | DragDropEffects.Move);
                MessageBox.Show(res.ToString());

            }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,819 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 24,501 Reputation points Microsoft Vendor
    2022-02-18T05:50:47.593+00:00

    @AMER SAID , based on my test, I reproduced your problem. We need to combine the path by using Path.Combine Method to combine directory and files.

    You could modify into the following code.

            string a = DateTime.Now.ToString("yyyy-MM-dd") + ".png";  
            string newpath=Path.Combine(programDirectory, a);  
            pictureBox1.Image.Save(newpath);  
     
            var files = new string[] { newpath };  
    

    Result:
    175694-4.gif

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.