Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Hello everyone,

I am trying to develop a desktop app. I have a DB table that keeps information of students. Columns are name, surname and photo. I am adding name and surname to DB. However, I am in trouble while adding the image path to the related column.

1st problem is locating the selected image to a specific folder.
2nd problem is getting the name of image (test.jpg or trying.png) to store in db table.

C#
private void btnChoosePhoto_Click(object sender, EventArgs e)
       {
           if (btnChoosePhoto.Text =="Fotoğraf Seç")
           {
               openFileDialogPhoto.Title = "Lütfen Fotoğraf Seçiniz";
               openFileDialogPhoto.Filter = " (*.jpg)|*.jpg|(*.png)|*.png";
               openFileDialogPhoto.ShowDialog();
               if (openFileDialogPhoto.ShowDialog() == DialogResult.OK)
               {
                   pictureBox1.Image = Image.FromFile(openFileDialogPhoto.FileName);
                   label7.Text = openFileDialogPhoto.FileName.ToString();
                   btnChoosePhoto.Text = "Yukle";
               }
           }
           else
           {
               string path = @"C:\";
               pictureBox1.Image.Save(path + @"\" + Name.Text + ".jpg", ImageFormat.Jpeg); // problem here ?
           }


       }
Posted
Comments
Sergey Alexandrovich Kryukov 21-Nov-13 22:05pm    
What do you call "upload" here? Upload means sending a file from a client to a server to be stored on server side. And you are just saving a file on the same host. What exactly you want to do? What's the problem?
—SA
FoxRoot 22-Nov-13 2:35am    
Sergey, sorry for my bad explanations. I haven't sleept for 2 days and continued coding a mvc4 project. Coding is my love but I stil have a long way to walk. And I should add this; English is not my mother tonguee :) Thanx for the response. Er Daljeet Singh has already added the solution. You are the help angel. Take care :)
Sergey Alexandrovich Kryukov 22-Nov-13 2:47am    
:-)
Peter Leow 21-Nov-13 22:09pm    
There are basically two options to store images in a server - database or filesystem, you may want to read this article first before proceeding further:

http://www.phpriot.com/articles/storing-images-in-mysql/2
FoxRoot 22-Nov-13 2:36am    
I got the url. I will read and try to learn more. Thanks for the response.

1 solution

here is the solution of your problem

C#
private void button1_Click(object sender, EventArgs e)
        {
            if (btnChoosePhoto.Text == "Fotoğraf Seç")
            {
                openFileDialogPhoto.Title = "Lütfen Fotoğraf Seçiniz";
                openFileDialogPhoto.Filter = " (*.jpg)|*.jpg|(*.png)|*.png";
                DialogResult res = openFileDialogPhoto.ShowDialog();
                if (res == DialogResult.OK)
                {
                    pictureBox1.Image = Image.FromFile(openFileDialogPhoto.FileName);

                    string[] sss = openFileDialogPhoto.FileName.Split('\\');
                    string s = sss[sss.Length - 1]; //this is how you will get the name of the file
                    label1.Text = s;

                    //this line will save the image to the Image Folder which in Exist inside bin\debug folder
                    pictureBox1.Image.Save(Environment.CurrentDirectory + "\\Image\\" + s, ImageFormat.Jpeg);

                    //this ss is path you need to store in db.
                    string ss = Environment.CurrentDirectory + "\\Image\\" + s;
                    label7.Text = ss;

                    btnChoosePhoto.Text = "Yukle";
                }
            }

        }
 
Share this answer
 
Comments
FoxRoot 22-Nov-13 2:39am    
Thank you hero :) I was searching for this during 2 days. I copied and pasted (this is the first time) the code than I put breakdown and watched everyline to see what happens. Now I will delete the code and try to write by myself :) Thanks again.

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