面临错误:无法将位从位图转换为字节[] [英] Facing Error : Failed to Convert Value from bitmap to a byte[]

查看:78
本文介绍了面临错误:无法将位从位图转换为字节[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void Upload_Click_1(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            //img filter
            open.Filter = "ImageFiles(*.jpg; *.jpeg; *.gif; *.bmp;)|*.jpg; *.jpeg; *.gif; *.bmp";
            if (open.ShowDialog() == DialogResult.OK)
            {
                String photoFilePath = open.FileName.ToString();
                pic.ImageLocation = photoFilePath;
                textbox.Text = photoFilePath;
            }
        }
        private void Save_Click(object sender, EventArgs e)
        {

         try
         {
             
             byte[] imageBt = null;
             FileStream fs = new FileStream(this.textbox.Text, FileMode.Open, FileAccess.Read);
             BinaryReader br = new BinaryReader(fs);
             imageBt = br.ReadBytes((int)fs.Length);
             
                String connString = ConfigurationManager.ConnectionStrings["logindb"].ConnectionString;
                SqlConnection conn = new SqlConnection(connString);
                SqlCommand cmd = new SqlCommand("storeimg", conn);
                //parameters
                cmd.Parameters.Add("@Photo", SqlDbType.Image).Value = pic.Image;
                cmd.Parameters.Add("@ID", SqlDbType.Int).Value = IDtex.Text;
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                MessageBox.Show("Added");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex.Message);
            }

        }

推荐答案

请看这里:为什么我得到参数无效。我从数据库中读取图像时出现异常? [ ^ ] - 这是完全相同的问题......
See here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - it's exactly the same problem...


为什么要使用pic.image?

cmd.Parameters.Add(@ Photo,SqlDbType.Image).Value = pic.Image;



使用字节数组imageBt相反。





此外图片数据类型将被弃用,为什么不使用varbynary?
Why using pic.image?
cmd.Parameters.Add("@Photo", SqlDbType.Image).Value = pic.Image;

Use the byte array imageBt instead.


Moreover Image data type will be deprecated, why not using varbynary?


这篇关于面临错误:无法将位从位图转换为字节[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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