0

I am getting the byte in database but I cannot convert it to bitmap, I got an exception in Parameter. This is what I've done so far.

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();
Arghya C
  • 9,805
  • 2
  • 47
  • 66
shampoo
  • 49
  • 3
  • 10

2 Answers2

0

I totally stole this answer from 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;
    }
}
Community
  • 1
  • 1
online Thomas
  • 8,864
  • 6
  • 44
  • 85
0

try the below. I expect it should work.

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

    // use the Bitmap object `obj` here
}
Deepak Sharma
  • 4,124
  • 1
  • 14
  • 31