views:

1043

answers:

1

I'm trying to pass some representation of an image back and forth between Silverlight and a WCF service. If possible I'd like to pass a System.Windows.Media.Imaging.BitmapImage, since that would mean the client doesn't have to do any conversion.

However, at some point I need to store this image in a database, meaning the image representation must be able to convert to and from byte[]. I can create a BitmapImage from a byte[] by reading the array into a MemoryStream and using BitmapImage.SetSource. But I can't seem to find a way to convert the other way - from BitmapImage to byte[]. Am I missing something obvious here?

If it helps at all, the conversion code could run on the server, i.e. it doesn't need to be Silverlight-safe.

A: 

Try using CopyPixels. You can copy the bitmap data to a byte array. However, I am honestly not sure what the format of the pixels would be...its probably dependent upon the kind of image that was originally loaded.

jrista
The page you linked to is for .NET in general, the Silverlight version of BitmapSource (in System.Windows.dll) doesn't support this method: http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapsource%28VS.95%29.aspx
theprise
Oh, apologies. I forgot that Silverlight and WPF are not quite equal yet.
jrista