C#中如何把PictrueBox中的图片信息转换为字节数组以便于保存到数据库中?(注:PictrueBox中的图片不是从本地加载来的,而是通过截取得来的,就是说它没有路径)???

解决方案 »

  1.   


    using System.Runtime.Serialization.Formatters.Binary;
    //...            byte[] buffer;
                using(System.IO.MemoryStream ms = new System.IO.MemoryStream())
                {
                    new BinaryFormatter().Serialize(ms, pictureBox1.Image);
                    buffer = ms.GetBuffer();
                }//...
                System.IO.MemoryStream ms;
                //...
                Image img = new BinaryFormatter().Deserialize(ms) as Image;
      

  2.   


    photostream = New IO.MemoryStream
    picPhoto.Image.Save(photostream, System.Drawing.Imaging.ImageFormat.Jpeg)
    Dim bt() As Byte = New Byte() {}
    bt = photostream.ToArray
    //picPhoto就是picture的name//保存到数据库的时候,就
    Dim strSqlinsert As String
    strSqlinsert = "Insert userinfo (cardno,photo) values(@cardno,@photo)"
    Dim inscom As SqlClient.SqlCommand = New SqlClient.SqlCommand(strSqlinsert, m_con)
    command1.Parameters.Add("@cardno", SqlDbType.Int).Value = Trim(txtCardno.Text)
    command1.Parameters.Add("@photo", SqlDbType.Image).Value = btcommand1.ExecuteNonQuery