开发者

Silverlight 4 : Converting image into byte[]

开发者 https://www.devze.com 2023-02-18 10:54 出处:网络
I have found how to do this in .NET 4.0, but I think JpegBitmapEncoder doesn\'t exist in Silverlight:

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 = bitmapI开发者_高级运维mage};

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


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);


Have a look at this library: Imagetools

It contains some nice utilities and jpg and png encoders,

0

精彩评论

暂无评论...
验证码 换一张
取 消