An example I found and worked was first you convert the bytes
for a BitmapImage
.
public BitmapImage BitmapImageFromBytes(byte[] bytes)
{
BitmapImage image = null;
MemoryStream stream = null;
try
{
stream = new MemoryStream(bytes);
stream.Seek(0, SeekOrigin.Begin);
System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
image = new BitmapImage();
image.BeginInit();
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
ms.Seek(0, SeekOrigin.Begin);
image.StreamSource = ms;
image.StreamSource.Seek(0, SeekOrigin.Begin);
image.EndInit();
}
catch (Exception)
{
throw;
}
finally
{
stream.Close();
stream.Dispose();
}
return image;
}
After this conversion is only assigned to the Source
of the component Image
MySqlConnection ligacaoBD = new MySqlConnection("server=127.0.0.1;user id=root;Pwd=123123;database=socios;persistsecurityinfo=True");
ligacaoBD.Open();
string cmd = string.Format("Select * from socios.socio WHERE idSocio={0}", 1);
MySqlCommand comandoMySQL = new MySqlCommand(cmd, ligacaoBD);
MySqlDataReader reader = comandoMySQL.ExecuteReader();
reader.Read();
byte[] foto = (byte[])reader["foto"];
image.Stretch = Stretch.Fill;
image.Source = BitmapImageFromBytes(foto);
MessageBox.Show(reader.GetString("idSocio"));