Domanda

I am trying to convert a varbinary to an image in my silverlight project.

First I get the Binary from my database in my service.

    [OperationContract]
    public byte[] getAfbeelding(int id)
    {

        var query = (from p in dc.Afbeeldings
                     where p.id == id
                     select p.source).Single();

        byte[] source = query.ToArray();

Then I try to convert the varbinary to an image, using code found on StackOverflow:

    public static string convertToImage(byte[] source)
    {
        MemoryStream ms = new MemoryStream(source);
        Image img = Image.FromStream(ms);
        return img.Source.ToString();
    }

But as it turns out, the silverlight Image does not have a .FromStream, I tried all the examples found in this thread but NONE of them work in silverlight.

'System.Windows.Controls.Image' does not contain a definition for 'FromStream'

So yeah, I'm kinda lost and am not sure what to do. Any ideas on how to do this in silverlight?

È stato utile?

Soluzione

You're almost right. The following code should be all you need:

var bitmapImage = new BitmapImage();
bitmapImage.SetSource(new MemoryStream(imageData));
newImage.Source = bitmapImage;

where imageData is of type byte[] and newImage is the image you want to update.

Altri suggerimenti

you sould have a look at WriteableBitmap.

there is a pretty nice set of extensions freely available on codepelex or Nuget

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top