Silverlight 4: Convert Image to Byte []

I found how to do this in .NET 4.0, but I think that JpegBitmapEncoder does not exist in Silverlight:

MemoryStream memStream = new MemoryStream();              
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(imageC));
encoder.Save(memStream);
var bytes = memStream.GetBuffer();

How to convert image to bytes [] in silverlight?

UPDATE:

I have a Contact model that has a Photo property. Whenever I add a new contact, I would like to load the local default image and convert it and set the Photo property for it.

var bitmapImage = new BitmapImage
                            {
                                UriSource = new Uri("pack://application:,,,/xxx;component/Images/default.JPG")
                            };
            var image = new Image{Source = bitmapImage};

Is this the right way to load an image first?

+3
source share
2 answers

Take a look at this library: Imagetools

It contains some useful utilities and jpg and png encoders,

0
source

Using

myImage.Save(memStream, ImageFormat.Jpeg);
return memStream.ToArray();

UPDATE

, , BitmapImage.

, BitmapImage . , :

Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourcePath);
byte[] buffer = new byte[s.Length];
s.Read(buffer, 0, buffer.Length);
+5

All Articles