将数据库中存储的图像转换为字符串并返回字节?

Converting stored images in DB to string and back to byte?

所以我在选项卡控件中有几个选项卡,它们执行注册和修改有关客户端的信息的任务。其他信息中的第一个,使用 OpenFileDialog 保存图片,然后将其存储到 'lblImagePath.Text' 以使用位图,这样我就可以将它保存到 de DB 中,可以在这段代码中看到:

public partial class form : Form

{
        String strDBFilePath = "";
        String strFilePath = "";
        Image DefaultImage;
        byte[] ImageByteArray;
        byte[] ImageByteArrayMod;

private void btnAddUser_Click(object sender, EventArgs e)
        {
            if (txtEmailPersonal.Text != "")
            {
                try
                {
                    Image temp = new Bitmap(strFilePath);
                    MemoryStream ms = new MemoryStream();
                    temp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    ImageByteArray = ms.ToArray();
                    SqlConnection con = new SqlConnection();
                    con.ConnectionString = CONEXION.CONEXIONMAESTRA.conexion;
                    con.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd = new SqlCommand("insert_user", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@email", txtEmailPersonal.Text);
                    cmd.Parameters.AddWithValue("@password", txtPasswordPersonal.Text);
                    cmd.Parameters.AddWithValue("@profile_picture", ImageByteArray);
                    cmd.Parameters.AddWithValue("@imageFile_name", lblImagePath.Text);
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
             mostrar();
            }
        }

}

一切顺利,注册表格有效,我正在使用数据网格视图对其进行可视化。当我双击 dgv 中的一行时,所有信息都会加载到第二个选项卡中,让我修改除个人资料图片之外的所有信息,可以在图片框中预览但不会加载任何其他相关信息,所以当我点击'save changes' 按钮,它不会让我继续它,直到我重新上传个人资料图片,因为在该操作之前路径不存在。这是用户修改的代码:

private void btnGuardarCambiosPersonal_Click(object sender, EventArgs e)
        {
            if (txtEmailPersonalMod.Text != "")
            {
                try
                {
                    Image temp = new Bitmap(strDBFilePath);
                    MemoryStream ms = new MemoryStream();
                    temp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    ImageByteArrayMod = ms.ToArray();
                    SqlConnection con = new SqlConnection();
                    con.ConnectionString = CONEXION.CONEXIONMAESTRA.conexion;
                    con.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd = new SqlCommand("modify_user", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@correo", txtEmailPersonalMod.Text);
                    cmd.Parameters.AddWithValue("@password", txtPasswordPersonalMod.Text);
                    cmd.Parameters.AddWithValue("@profile_picture", ImageByteArrayMod);
                    cmd.Parameters.AddWithValue("@imageFile_name", lblFilePathMod.Text);
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                mostrar();
            }

        }

我在那里的一些东西实际上可能对完成我想要的东西毫无用处。所以我会尽量说清楚,如果没有上传另一张个人资料照片来替换它,我希望能够保留当前的个人资料照片。

正如您可能还看到的,我没有直接在代码上进行查询,而是使用存储过程,我的想法是保留这些存储过程并尝试在代码中进行调整。

看完@Llama 的评论后,我意识到解决方案很简单,通过修改代码,我在 try/catch:

的开头添加了这个
Image tempy = new Bitmap(picPerfilMod.Image);
MemoryStream mems = new MemoryStream();
tempy.Save(mems, System.Drawing.Imaging.ImageFormat.Jpeg);
ImageByteArrayMod = mems.ToArray();

所以我可以将图片从图片框转回数组,而无需修改它。

我会继续阅读有关 varbinary 列类型在这种情况下的使用,因为它显然看起来像 better/simpler 想法。