2

C#,WFA,使用 .NET 4.5 平台,

我删除了 textBox1(名字)、textBox2(姓氏)、pictureBox1(员工照片)、button2(浏览)和 button1(保存)来插入新员工。

button2 -> 必须浏览图片并在pictureBox1中显示,

button1 -> 必须将 button2 浏览并由 pictureBox1 显示的图像保存到 localhost 中的此表中。

运行程序后,出现此错误(找不到文件。)

(虽然我单独浏览没有任何错误)

我只想要包含修复此 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);
            }

        }

    }
}
4

3 回答 3

4

当您尝试从此处的图像获取图像文件路径时:

string path = pictureBox1.Image.ToString();

你实际上得到了图像类型的名称(System.Drawing.Bitmap)

在对话框中选择图像文件后,您需要保留图像文件的路径。你可以这样做:

pictureBox1.Image = System.Drawing.Image.FromFile(dlg.FileName);
pictureBox1.Tag = dlg.FileName;

然后你需要像这样读取这个名字:

string path = pictureBox1.Tag as string;
于 2013-09-27T16:46:52.627 回答
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);
            }

        }

    }
}
于 2013-09-27T16:46:26.077 回答
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);
        }

    }
于 2013-09-27T16:50:28.903 回答