0
This is Good Afternoon Guys!
I have a little difficulty in passing my registered photo to my PictureBox
to update the registry.
My scenario is this::
- I have a
Form
data entry registration and - A
Form
that makes the search of the register that is presented in aDataGridView
and when clicked on the record it returns me the data for registration form so that I can do the update;
Well I insert the data and the photo converted to binary, using db sqlserver
more difficult to pass the photo to my picture, the other fields I can bring, but not the photo.
Note: it’s my first time working with photo in WPF.
follow my code, guys.
<-- This is from my DataGridView
where I select the cell by throwing the dice to the form
criminal record-->
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);
}
}
This is my code for conducting the Internet 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 column Tb_imageuser" is where I store my image!
Thanks for your help!
It’s winforms or wpf?
– Leandro Angelo
Hello Leandro! Application is in WINDOWS FORMS
– Lucas_Silva
and you’re sure that
foto_array
is getting abyte[]
of that image?– Leandro Angelo
Have you tried changing this line:
frm.picFoto.Image = Image.FromStream(ms);
for thisfrm.picFoto.Image = new Bitmap(ms);
.– João Martins
Hello @Joãomartins did the test with the change, but it didn’t work either.
– Lucas_Silva
Has the image been correctly recorded in the database? The byte array has the correct image?
– João Martins
was recorded yes @Joãomartins the insertion of the data with the photo ta inserted in the database, the problem being to bring back the image. the character data I made the test all comes, only the photo that does not!
– Lucas_Silva
How are you saving the image to the database? Edit your question and enter the pf code.
– João Martins
Ready @Joãomartins made the change so I can better understand my scenario.
– Lucas_Silva
What kind is your column that holds the image?
– João Martins
Initially I had left this column as 'image', but I searched more and saw that 'VARBINARY' is better for image storage! so I left it as VARBINARY.
– Lucas_Silva