
Tranformar Array Bytes em BitmapImage
Março 26, 2007Deixo-vos aqui uma pequena função que nos permite transformar um Byte[] em BitmapImage, bastante útil quando guardamos imagens em base de dados ou transferindo por webServices!!
/// <summary>
/// Obter uma imagem atravez de um array de bytes
/// </summary>
/// <param name=”arraybytes”>array de bytes</param>
/// <returns>devolve um BitmapImage que podemos adicionar á source </returns>
/// <example>Obj.source=getImage(byte[])</example>
BitmapImage getImage(byte[] arraybytes)
{
// Criar a Imagem
// NOTE: Isto n é um Bitmap normal (GDI+)
BitmapImage bitmap = new BitmapImage();
if (arraybytes != null)
{
MemoryStream strm = new MemoryStream(arraybytes);
bitmap.BeginInit();
bitmap.StreamSource = strm;
bitmap.EndInit();
}
//COMMENT: Retorna a Imagem
return bitmap;
}
I’ve following exception message, using this sample:
NotSupportedException was unhandled
No imaging component suitable to complete this operation was found.