Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silverlight 4 : Converting image into byte[]

I have found how to do this in .NET 4.0, but I think JpegBitmapEncoder doesn't 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 can I convert an image to bytes[] in silverlight?

UPDATE:

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

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

Is this the correct way to load an image in first place?

like image 892
Houman Avatar asked May 07 '26 22:05

Houman


1 Answers

Use

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

UPDATE

OK it turns out that the image is a BitmapImage.

It seems that BitmapImage does not expose the functionality to save the image. The solution is to get the image from the embedded resource:

Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourcePath);
byte[] buffer = new byte[s.Length];
s.Read(buffer, 0, buffer.Length);
like image 148
Aliostad Avatar answered May 10 '26 11:05

Aliostad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!