如何将图像上传到特定文件夹并在DB中保留图像的名称? [英] How to upload the image to a specific folder and keep the name of the image in DB ?
本文介绍了如何将图像上传到特定文件夹并在DB中保留图像的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我正在尝试开发桌面应用程序。我有一个DB表,可以保存学生的信息。列是姓名和照片。我正在为DB添加姓名。但是,在将图像路径添加到相关列时,我遇到了麻烦。
第一个问题是将所选图像定位到特定文件夹。
第二个问题是得到图像的名称(test.jpg或尝试.png)存储在db表中。
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.
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 ?
}
}
推荐答案
这里是你问题的解决方案
here is the solution of your problem
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";
}
}
}
这篇关于如何将图像上传到特定文件夹并在DB中保留图像的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文