Upload photo from Datagridview and switch to picturebox - Windows Forms C #

0

Speak Good Afternoon! I'm having a hard time passing my recorded photo to my PictureBox to update the registry.

My scenario is as follows:

  • I have Form of register that does the insertion of data and
  • A Form that does the search of the record that is displayed in a DataGridView and when clicked on the record it returns the data to the registration form for you to do the update;

Well, I do the insertion of the data and the converted photo to binary, using db sqlserver plus with difficulty in passing the photo to my picture, the other fields I can bring, but the photo does not. p>

Note: this is my first time working with a photo in WPF.

follow my galley code.

< - This is from my DataGridView where I select the cell throwing the data to the form of register - >

   int linhaNumero = 0;
    byte[] foto_array;

private void dgw_Onibus_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        try
        {
            if (lblOperacao.Text == "Aluno Master")
            {

                DataGridViewRow dr = dgw_Onibus.SelectedRows[0];
                this.Hide();
                FRM_CadOnibusCircular frm = new FRM_CadOnibusCircular();
                frm.Show();

                frm.TB_id.Text = dr.Cells[0].Value.ToString();
                frm.TB_IdFicha.Text = dr.Cells[1].Value.ToString();
                frm.TB_NomeUser.Text = dr.Cells[2].Value.ToString();
                frm.MK_TelUser.Text = dr.Cells[3].Value.ToString();
                frm.TB_RgUser.Text = dr.Cells[4].Value.ToString();
                frm.MTK_CpfUser.Text = dr.Cells[5].Value.ToString();

                frm.picFoto.Image = null;

                if (ds.Tables[0].Rows[linhaNumero][6] != System.DBNull.Value)
                {
                    foto_array = (byte[])ds.Tables[0].Rows[linhaNumero][6];
                    MemoryStream ms = new MemoryStream(foto_array);
                    frm.picFoto.Image = Image.FromStream(ms);
                }

                frm.TB_NomeProprietario.Text = dr.Cells[7].Value.ToString();
                frm.MK_TelProprietario.Text = dr.Cells[8].Value.ToString();
                frm.TB_EnderecoProprietario.Text = dr.Cells[9].Value.ToString();
                frm.TB_QdProprietario.Text = dr.Cells[10].Value.ToString();
                frm.TB_EmailProprietario.Text = dr.Cells[11].Value.ToString();
                frm.TB_RgProprietario.Text = dr.Cells[12].Value.ToString();
                frm.MKT_Proprietario.Text = dr.Cells[13].Value.ToString();
                frm.TB_NumeroProprietario.Text = dr.Cells[14].Value.ToString();
                frm.TB_LoteProprietario.Text = dr.Cells[15].Value.ToString();

                bool proprietarioChecked = dgw_Onibus.CurrentRow.Cells["opcao"].Value.ToString().Contains("Proprietario");
                bool depedenteChecked = dgw_Onibus.CurrentRow.Cells["Opcao"].Value.ToString().Contains("Depedente");
                bool colaboradorChecked = dgw_Onibus.CurrentRow.Cells["opcao"].Value.ToString().Contains("Colaborador"); // Ou contém "Yoga", não sei como você guarda isto na sua 'DataGridView'

                // Obter checkBoxlist da seleção

                int indexCbProprietario = GetItemIndex(frm.CLB_Opcao, "Proprietario");
                int indexCbDepedente = GetItemIndex(frm.CLB_Opcao, "Depedente");
                int indexCbColaborador = GetItemIndex(frm.CLB_Opcao, "Colaborador"); // fmr.chekedListBox é o nome do seu CheckedListBox dentro do seu form. 

                //Pegar o valor da dataGridView que diz se a determinada atividade está marcada





                // Checar o valor da Opcao no seu 'checkedListBox'
                frm.CLB_Opcao.SetItemChecked(indexCbProprietario, proprietarioChecked);
                frm.CLB_Opcao.SetItemChecked(indexCbDepedente, depedenteChecked);
                frm.CLB_Opcao.SetItemChecked(indexCbColaborador, colaboradorChecked);



                frm.BT_UpdateOnibus.Enabled = true;
                frm.BT_DeleteOnibus.Enabled = true;
                frm.BT_SaveOnibus.Enabled = false;
                frm.Lbl_Usuario.Text = lblUsuario.Text;
                lblOperacao.Text = "";
            }
        }
        catch (Exception erro)
        {
            MessageBox.Show(erro.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

Esee is my code for performing the insert in the database

  void converterFoto()
    {
        //convertendo a foto para dados binários
        if (picFoto.Image != null)
        {
            ms = new MemoryStream();
            picFoto.Image.Save(ms, ImageFormat.Jpeg);
            byte[] foto_array = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(foto_array, 0, foto_array.Length);
            cg.cmd.Parameters.AddWithValue("@d8", foto_array);
        }
    }
private void BT_SaveOnibus_Click(object sender, EventArgs e)
    {
        try
        {
            if (TB_NomeUser.Text == "")
            {
                MessageBox.Show("Informe o nome do Usuário", "Aviso do sistema", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                TB_NomeUser.Focus();
                return;

            }
            if (MK_TelUser.Text == "")
            {
                MessageBox.Show("Informe o Telefone", "Aviso do sistema", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                MK_TelUser.Focus();
                return;

            }
            if (TB_RgUser.Text == "")
            {
                MessageBox.Show("Informe o Rg do Usuário", "Aviso do sistema", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                TB_RgUser.Focus();
                return;
            }
            if (MTK_CpfUser.Text == "")
            {
                MessageBox.Show("Informe o Cpf do Usuário", "Aviso do sistema", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                MTK_CpfUser.Focus();
                return;                    
            }


            if (CLB_Opcao.CheckedItems.Count > 0)
            {
                for (int i = 0; i < CLB_Opcao.CheckedItems.Count; i++)
                {
                    if (str == "")
                    {
                        str = CLB_Opcao.CheckedItems[i].ToString();
                    }
                    else
                    {
                        str += "," + CLB_Opcao.CheckedItems[i].ToString();
                    }
                }
            }


            cg.con = new SqlConnection(cn.DBconn);
            cg.con.Open();
            string cb = "insert into CadOnibusUser(F_Cod,Id_UserOnibus,TB_NomeUser,TB_TelefoneUser,TB_RgUser,TB_CpfUser,OpcaoUser,TB_ImageUser,TB_NomePropri,TB_TelefonePropri,TB_EnderecoPropri,TB_QuadraPropri,TB_EmailPropri,TB_RgPropri,TB_CpfPropri,TB_NumeroPropri,TB_LotePropri) VALUES (@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11,@d12,@d13,@d14,@d15,@d16,@d17)";
            cg.cmd = new SqlCommand(cb);
            cg.cmd.Connection = cg.con;

            cg.cmd.Parameters.AddWithValue("@d1", TB_id.Text);
            cg.cmd.Parameters.AddWithValue("@d2", TB_IdFicha.Text);
            cg.cmd.Parameters.AddWithValue("@d3", TB_NomeUser.Text);
            cg.cmd.Parameters.AddWithValue("@d4", MK_TelUser.Text);
            cg.cmd.Parameters.AddWithValue("@d5", TB_RgUser.Text);
            cg.cmd.Parameters.AddWithValue("@d6", MTK_CpfUser.Text);
            cg.cmd.Parameters.AddWithValue("@d7", str);
            cg.cmd.Parameters.AddWithValue("@d9", TB_NomeProprietario.Text);
            cg.cmd.Parameters.AddWithValue("@d10", TB_NumeroProprietario.Text);
            cg.cmd.Parameters.AddWithValue("@d11", TB_EnderecoProprietario.Text);
            cg.cmd.Parameters.AddWithValue("@d12", TB_QdProprietario.Text);
            cg.cmd.Parameters.AddWithValue("@d13", TB_EmailProprietario.Text);
            cg.cmd.Parameters.AddWithValue("@d14", TB_RgProprietario.Text);
            cg.cmd.Parameters.AddWithValue("@d15", MKT_Proprietario.Text);
            cg.cmd.Parameters.AddWithValue("@d16", TB_NumeroProprietario.Text);
            cg.cmd.Parameters.AddWithValue("@d17", TB_LoteProprietario.Text);
            converterFoto();




            cg.cmd.ExecuteReader();
            cg.con.Close();

            st1 = Lbl_Usuario.Text;
            st2 = "Novo Usuário incluído '" + TB_NomeUser.Text + "' com seguinte id '" + TB_id.Text + "'";
            cf.LogFunc(st1, System.DateTime.Now, st2);

            BT_SaveOnibus.Enabled = false;

            MessageBox.Show("Salvo com sucesso", "Registro", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        catch (Exception erro)
        {
            MessageBox.Show(erro.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

This is my table where I entered the data "My TB_imageUser" column is where I store my image!

Thank you for your help!

    
asked by anonymous 08.08.2018 / 20:48

2 answers

0

I was able to solve the problem I was quoting again! I'll leave here if anyone needs it!

foto_array = (byte[])dr.Cells[6].Value;

This was the change for each cell in the datagridview to take the photo and play on the main form of each selected user.

    
15.08.2018 / 19:02
0

I managed to solve this problem I was forgetting to call the method in my global class from my dataset.

What I did was this

In my global class where I call all my methods I've created

public DataSet ds;

and then I called him on my search form to play the data in the main form.

cg.ds = new DataSet();

After that I just changed my line

if (ds.Tables[0].Rows[linhaNumero][6] != System.DBNull.Value)

for

if (cg.ds.Tables[0].Rows[linhaNumero][6] != System.DBNull.Value)
{    
    foto_array = (byte[])cg.ds.Tables[0].Rows[linhaNumero][6];    
}
    
15.08.2018 / 12:52