首页 > 解决方案 > 将数据库中存储的图像转换为字符串并返回字节?

问题描述

所以我在 tabcontrol 中有几个选项卡,它们执行注册和修改有关客户端的信息的任务。其他信息中的第一个,使用 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();
            }
        }

}

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

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();
            }

        }

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

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

标签: c#winforms

解决方案


在阅读了@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在这种情况下使用列类型的信息,因为它显然看起来是一个更好/更简单的想法。


推荐阅读