在metro应用程序中将BitmapImage转换为字节



和标题一样。如何在地铁风格的应用程序中转换BitmapeImage到字节。自由。我需要它在LuminanaceSource()中使用,它需要byte[]中的源。有一些例子,但我不知道如何使用它。

BitmapImage bmpImage = new BitmapImage(new Uri(file.Path));
   var encoderId = Windows.Graphics.Imaging.BitmapEncoder.JpegEncoderId;

谢谢

这两个方法应该将位图图像转换为字节[],反之亦然。

public BitmapImage ImageFromBuffer(Byte[] bytes)
{
    MemoryStream stream = new MemoryStream(bytes);
    BitmapImage image = new BitmapImage();
    image.BeginInit();
    image.StreamSource = stream;
    image.EndInit();
    return image;
}
public Byte[] BufferFromImage(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;
}

最新更新