2015-11-08 1 views
0

Je reçois l'octet dans la base de données mais je ne peux pas le convertir en bitmap, j'ai une exception dans le paramètre. C'est ce que j'ai fait jusqu'ici.Conversion d'octets en image bitmap

con.ConnectionString = MyConnectionString; 
con.Open(); 
OdbcCommand cmds = new OdbcCommand("Select ID from try where kalabaw = 5", con); 
OdbcDataAdapter da = new OdbcDataAdapter(cmds); 
byte[] image = (byte[])cmds.ExecuteScalar(); 
TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap)); 
Bitmap bitmap = (Bitmap)tc.ConvertFrom(image); 
pictureBox2.Image = bitmap; 
con.Close(); 
+1

http://stackoverflow.com/questions/14337071/convert-array-of-bytes-to-bitmapimage –

+0

je ne peux pas passer la executeSalar à la méthode – shampoo

+0

il n'y a pas ExecuteScalar dans le lien que je vous ai envoyé –

Répondre

0

Je suis totalement volé cette réponse de convert array of bytes to bitmapimage

public BitmapImage ToImage(byte[] array) 
{ 
    using (var ms = new System.IO.MemoryStream(array)) 
    { 
     var image = new BitmapImage(); 
     image.BeginInit(); 
     image.CacheOption = BitmapCacheOption.OnLoad; // here 
     image.StreamSource = ms; 
     image.EndInit(); 
     return image; 
    } 
} 
+0

obtenir une erreur avec cela. – shampoo

0

essayer le ci-dessous. Je m'attends à ce que cela fonctionne.

byte[] bytes = GetBytesArrayFromDatabase(); 
using (MemoryStream ms = new MemoryStream(bytes)) 
{ 
    ms.Position = 0; 
    Bitmap obj = new Bitmap(ms); 

    // use the Bitmap object `obj` here 
}