Home > front end >  Converting stored images in DB to string and back to byte?
Converting stored images in DB to string and back to byte?

Time:11-23

So I have a couple tabs in a tabcontrol which do the task of registering and modifying info about a client. The first one among other info, saves a picture using OpenFileDialog and then store it into 'lblImagePath.Text' to use bitmap with it so I can save it into de DB, which can be seen in this code:

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

}

Everything goes smooth, register form works and I'm using a datagridview to visualize it. When I double click a row from the dgv, all the info loads into the second tab and let's me modify all the info except the profile picture, which can be previewed in a picturebox but doesnt load any other information about it, so when I hit the 'save changes' button, it won't let me proceed with it until I re-upload a profile picture, since path is non existant prior to that action. This is the code for user modifying:

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

        }

Some of the stuff I have in there might actually be useless to accomplish what I want. So I'll try to be as clear as possible, I want to be able to keep the current profile picture if another profile picture isn't uploaded to replace it.

As you might also see, instead of doing the query directly on the code, I'm using stored procedures, the idea is to keep those stored procedures and try to do the adjusting in the code.

CodePudding user response:

After reading @Llama 's comments, I realized solution was very simple, over the modifying code, I added this at the beginning of the try/catch:

Image tempy = new Bitmap(picPerfilMod.Image);
MemoryStream mems = new MemoryStream();
tempy.Save(mems, System.Drawing.Imaging.ImageFormat.Jpeg);
ImageByteArrayMod = mems.ToArray();

so I could turn image from picturebox back to array and without the need to modify it.

I will keep reading about varbinary column type use for this cases, since it clearly looks like a better/simpler idea.

  • Related