2015-11-08 194 views
0

我正在获取数据库中的字节,但无法将其转换为位图,在Parameter中出现异常。这是我迄今为止所做的。将字节转换为位图图像

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

我不能传递executeSalar的方法 – shampoo

+0

存在,我送你 –

回答

0

我完全从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

得到一个错误。 – shampoo

0

偷了这个答案尝试以下。我期望它应该工作。

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

    // use the Bitmap object `obj` here 
}