I haven't tested it, but this should do the trick:
public Byte[] BufferFromImage(this BitmapImage imageSource)
{
Stream stream = imageSource.StreamSource;
Byte[] buffer = null;
if (stream != null && stream.Length > 0)
{
using (BinaryReader br = new BinaryReader(stream))
{
buffer = br.ReadBytes((Int32)stream.Length);
}
}
return buffer;
}
This is an extention method. You would put this in it's own class and use it like this:
BitmapImage myImage = new BitmapImage();
...
byte[] imageBytes = myImage.ToByteArray();