Set Silverlight Image Source from byte array

I’ve been playing around with Silverlight and WCF services and I came across the need to set the Source of an Image control in Silverlight from a byte array. I have a service method which returns an image from the server as a byte array which I want to show in the Silverlight application.

My service method is simple and looks like this:

using System.IO;

public byte[] GetImage(string serverPath)
    return File.ReadAllBytes(serverPath);

In the Silverlight application I create a MemoryStream from the byte array, then a BitmapImage from the MemoryStream and set the image source to the BitmapImage.

using System.IO;
using System.Windows.Media.Imaging;

void proxy_GetImageCompleted(object sender, GetImageCompletedEventArgs e)
    MemoryStream stream = new MemoryStream(e.Result);
    BitmapImage b = new BitmapImage();
    imgImage.Source = b;

proxy_GetImageCompleted is the event handler for when my asynchronous call to GetImage completes, where e.Result is the byte array.

Posted on by Joe in Silverlight

12 Responses to Set Silverlight Image Source from byte array

  1. Marc

    How to convert BitmapImage to byte array ?
    Thank you

  2. joestevens

    Hi Marc

    In Silverlight 2 there is no way to the get a byte array from the BitmapImage.

    In what situation do you need to do this?


  3. Marc

    Maybe it’s not necessary. I was just asking to find a solution for resizing the BitmapImage because I need to use it immediatly after the user has selected it from his Hard Drive since the image can be huge and I don’t want to validate a small image. I want people to send any image size since a lot of people do not know how to do it I want my program to do it for them. But I saw no way else then resizing the image on my WCF service AFTER I sent it asynchronously. But the user have his image on hand very huge and the application becomes very slow if the user upload many images like this. So I really need to resize the image at the source between the user selecting it in his hard drive and showing it to him in the canvas.

  4. Joe


    You can resize the image at the client using FJCore.

    See the following post for details:


  5. Rich

    Great post! It was most useful to me. Thanks.

  6. Joe


    Not done VB.NET for a while but can’t spot anything in the code. It looks like you may be using WPF here? At what point when debugging do you get the error?


  7. George

    Can you post the entire code ?
    I ‘m new in Silverlight and I need some help…
    Thank you

  8. Nicolas

    Thank you, it hepls.

  9. Chinjoo

    great post. The only issue I find with this is when the image is of TIFF format and the application is silverlight. It errors ot with the following exception: Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED)) at b.SetSource(stream);

    Any suggestion of solving this issue?

  10. Star Liu

    thank you, it helps! ^_^
    i’m a coder from china.

  11. ricardo

    can you give an example using a DataBase like oracle? This is my webservices.

    public byte[] InformeTecnico(Int64 otClave)
    byte[] PictureStream;

    string CadenaConexion = ConfigurationManager.AppSettings[“CadenaConexion2”];
    string queryOracle = “select a.gar_deta_informe_tec from prd_garantia_det a where a.gar_detaclave=1”;
    OracleConnection conexionOracle = new OracleConnection(CadenaConexion);
    OracleCommand comandoOracle = new OracleCommand(queryOracle, conexionOracle);


    OracleDataReader registrosOracle = comandoOracle.ExecuteReader();

    PictureStream = File.ReadAllBytes(registrosOracle[0].ToString());

    // 4. Cerramos la conexion y el OracleDataReader

    // Aqui se devuelve el resultado con la totalidad de filas
    return PictureStream;

    Sorry but my Inglish is not good;
    It’s ok my webservice?

  12. Joe


    Afraid I’ve not used Oracle in a very long time, and have used Silverlight since writing these posts.


Add a Comment