C#,WFA,使用.NET 4.5平台,
我删除了textBox1(名字),textBox2(姓氏),pictureBox1(员工照片),button2(浏览)和button1(保存)以插入新员工。
button2 - >必须浏览图片并在pictureBox1中显示,
button1 - >必须保存图像,该图像由button2浏览并由pictureBox1显示,to this table in localhost.
运行程序后,I GET THIS ERROR (file could not be found.)
(虽然我单独浏览时没有任何错误)
我只想要包含修复此WFA的代码的答案。只是想确定我对此非常清楚。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
SqlConnection cnn = new SqlConnection("Initial Catalog=randomcompany;Data Source=localhost;Integrated Security=SSPI;");
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e) //Browse button
{
try
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" + "All files (*.*)|*.*";
dlg.Title = "Select Employee Picture";
if (dlg.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = System.Drawing.Image.FromFile(dlg.FileName);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e) //Save button
{
try
{
cnn.Open();
string path = pictureBox1.Image.ToString();
Byte[] imagedata = File.ReadAllBytes(path);
SqlCommand cmd = new SqlCommand("INSERT INTO Employees (EmployeeFirstname, EmployeeLastname, EmployeePhoto) VALUES (@item1,@item2,@img", cnn);
cmd.Parameters.AddWithValue("@item1", textBox1.Text);
cmd.Parameters.AddWithValue("@item2", textBox2.Text);
cmd.Parameters.AddWithValue("@img", imagedata);
cmd.ExecuteNonQuery();
cnn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
答案 0 :(得分:4)
当您尝试从图像中获取图像文件路径时:
string path = pictureBox1.Image.ToString();
你实际上得到的是Image type(System.Drawing.Bitmap)的名称
在对话框中选择后,需要保留图像文件的路径。你可以这样做:
pictureBox1.Image = System.Drawing.Image.FromFile(dlg.FileName);
pictureBox1.Tag = dlg.FileName;
然后你需要读这个名字:
string path = pictureBox1.Tag as string;
答案 1 :(得分:3)
问题在于:
string path = pictureBox1.Image.ToString();
不会返回路径。在类字段中获取路径时存储路径。我们将其命名为_path
:
private string _path;
然后在获取文件名时设置它:
_path = dlg.FileName;
然后在这里使用它:
Byte[] imagedata = File.ReadAllBytes(_path);
以下是修改后的完整代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private string _path;
SqlConnection cnn = new SqlConnection("Initial Catalog=randomcompany;Data Source=localhost;Integrated Security=SSPI;");
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e) //Browse button
{
try
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" + "All files (*.*)|*.*";
dlg.Title = "Select Employee Picture";
if (dlg.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = System.Drawing.Image.FromFile(dlg.FileName);
_path = dlg.FileName;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e) //Save button
{
try
{
cnn.Open();
Byte[] imagedata = File.ReadAllBytes(_path);
SqlCommand cmd = new SqlCommand("INSERT INTO Employees (EmployeeFirstname, EmployeeLastname, EmployeePhoto) VALUES (@item1,@item2,@img", cnn);
cmd.Parameters.AddWithValue("@item1", textBox1.Text);
cmd.Parameters.AddWithValue("@item2", textBox2.Text);
cmd.Parameters.AddWithValue("@img", imagedata);
cmd.ExecuteNonQuery();
cnn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
答案 2 :(得分:1)
您使用了错误的属性来获取图像路径。你应该 图片框的PictureBox.ImageLocation属性可以获取图像的确切位置。
修改此部分
private void button1_Click(object sender, EventArgs e) //Save button
{
try
{
cnn.Open();
string path = pictureBox1.ImageLocation; // this will work
string path = pictureBox1.Image.ToString(); // here error comes
Byte[] imagedata = File.ReadAllBytes(path);
SqlCommand cmd = new SqlCommand("INSERT INTO Employees (EmployeeFirstname, EmployeeLastname, EmployeePhoto) VALUES (@item1,@item2,@img", cnn);
cmd.Parameters.AddWithValue("@item1", textBox1.Text);
cmd.Parameters.AddWithValue("@item2", textBox2.Text);
cmd.Parameters.AddWithValue("@img", imagedata);
cmd.ExecuteNonQuery();
cnn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}