[解决]使用c#winform从sql server获取图像时出错 [英] [Solved]getting error while fetching image from sql server using c# winform

查看:83
本文介绍了[解决]使用c#winform从sql server获取图像时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我获取图片的原始代码是:



My original code to fetch image is :

private void loadImage()
        {
            cn = new SqlConnection(con);
            da = new SqlDataAdapter("Select empimage from newemployeerecord where employeeid='2015160000'", cn);
            ds.Tables.Clear();
            da.Fill(ds, "Image");
            if (ds.Tables["Image"].Rows.Count > 0)
            {
                pbUploadPhoto.Image = null;
                if (ds.Tables[0].Rows[0][0] != DBNull.Value)
                {
                    photo_aray = (byte[])ds.Tables[0].Rows[0][0];
                    MemoryStream ms = new MemoryStream(photo_aray);
                    pbUploadPhoto.Image = new Bitmap(ms); // Exception Part...ArgumentException Was Unhandled  (parameter is not valid)
                    pbUploadPhoto.Refresh();
                }
            }
        }





当我运行此代码时,我收到错误,即

ArgumentException未处理

参数无效。



为此我搜索了很多次但没有得到正确的解决方案因为我不想使用FileStream类。



请帮忙,因为我是这个错误的新手......



When i run this code i am getting error i.e.
ArgumentException was unhandled
parameter is not valid.

For this i googled so many times but not getting right solution as i am don't want to use FileStream Class.

pls help as i am new with this error...

推荐答案

好吧,要将数据保存在文件中,可以使用 FileStream类 [ ^ ]。

Well, to save the data in a file, you can use the FileStream class[^].
using (FileStream fs = new FileStream(@"C:\Test.jpg"))
{
    byte[] fileData = (byte[])ds.Tables[0].Rows[0][0];
    fs.Write(fileData, 0, fileData.Length);
    fs.Flush();
}



要将文件作为图像查看,您可以使用Windows上的任何预安装应用程序,例如Paintor默认图像查看器。 />


要打开文件并查看二进制数据,可以使用Visual Studio,UltraEdit或任何其他二进制编辑器。



请记住,这不是解决方案。只有帮助调试你的问题。



作为如何从数据库中读取数据的指针,你可以查看 DataReader [ ^ ]类而不是使用当你只想用一列读取一行时,DataAdapter有点矫枉过正。

参见使用DataReader检索数据 [ ^ ]


To view the file as an image you can use any of the pre-installed applications on Windows, such as Paintor the default image viewer.

To open the file and view the binary data, you can use Visual Studio, UltraEdit or any other binary editor.

Remember, this is not a solution. Only help to debug your problem.

As a pointer for how to read data from the database, you can look into the DataReader[^] class instead of using a DataAdapter which is kind of overkill when you want to read only one row with one column.
See also Retrieving Data Using a DataReader[^]


在DB

创建表格测试



Photoid varchar(10),

照片图片





选择图像并在图片框中预览

In DB
create table test
(
Photoid varchar(10),
Photo Image
)

To Select A Image and preview it in picturebox
private void btnImgBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image = new Bitmap(ofd.FileName);
                textBox1.Text = ofd.FileName;
            }
        }





保存在DB





To Save In DB

private void btnSave_Click(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection("Data Source=Server;Initial Catalog=Test;Integrated Security=True");
            try
            {
                byte[] image = null;
                FileStream fs = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fs);
                image = br.ReadBytes((int)fs.Length);
                cn.Open();
                string query = "insert into images (photoid,photo)values('" + textBox2.Text + "',@img)";
                SqlCommand objcmd = new SqlCommand(query, cn);
                objcmd.Parameters.Add(new SqlParameter("@img", image));
                int rowaffected = objcmd.ExecuteNonQuery();

                MessageBox.Show("Inserted Successfully");

                MessageBox.Show(rowaffected + "row'(s)" + "affected");
                
                cn.Close();

            }

            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);

            }
        }







显示图片返回图片框






To Show Image Back In PictureBox

private void btnShowImage_Click(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection("Data Source=Server;Initial Catalog=Test;Integrated Security=True");
            cn.Open();
            SqlCommand cmd = new SqlCommand("select photo from images where photoid="+textBox2.Text,cn);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                byte[] img = (byte[])dr[0];
                MemoryStream ms = new MemoryStream(img);
                pictureBox1.Image = Image.FromStream(ms);
            }
            cn.Close();
        }     


这篇关于[解决]使用c#winform从sql server获取图像时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆