I'm doing a project that has classes, for example, a user class, where you have: code, name, password and photo and if you have the method of user input:
public string Inserir()
{
return "insert into Usuario(Username,Senha,Foto) values ('" + _username + "','" + _senha + "','" + _vetorImagens + "')";
}
}
}
In the registration form, I have a function that takes the chosen image in an open dialog and fills in the picture box:
private void CarregaImagem()
{
try
{
this.Fd1.ShowDialog(this);
string strFn = this.Fd1.FileName;
if (string.IsNullOrEmpty(strFn))
return;
this.pbImagem.Image = Image.FromFile(strFn);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
and after that click on the save button if you have another function that should take the information, save in the attributes of my User class and finally save in the BD:
private void btCadastrar_Click(object sender, EventArgs e)
{
if (tbCsenha.Text != tbSenha.Text)
{
MessageBox.Show("As senhas não batem ");
tbSenha.Clear();
tbCsenha.Clear();
tbSenha.Focus();
}
else
{
cUsuario cUs = new cUsuario();
MemoryStream ms = new MemoryStream();
cUs.VetorImagens = null;
pbImagem.Image.Save(ms, ImageFormat.Jpeg);
cUs.VetorImagens = ms.ToArray();
cUs.Username = tbUser.Text;
cUs.Senha = tbSenha.Text ;
if (con.Cadastro(cUs.Inserir()) == true)
{
MessageBox.Show("Cadastrado com sucesso");
tbUser.Clear();
pbImagem.Image = null;
tbSenha.Clear();
tbCsenha.Clear();
tbUser.Focus();
}
}
}
This function saves the password and name correctly, however the photos always have the value 0x53797374656D2E427974655B5D. If anyone can help me, I've been breaking my head with this for some time, I've never manipulated images in a DB, even with classes.