Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Windows Phone 8.1运行时将BitmapImage转换为字节[]数组_C#_Windows Runtime_Windows Phone 8.1_Bytearray_Bitmapimage - Fatal编程技术网

C# 在Windows Phone 8.1运行时将BitmapImage转换为字节[]数组

C# 在Windows Phone 8.1运行时将BitmapImage转换为字节[]数组,c#,windows-runtime,windows-phone-8.1,bytearray,bitmapimage,C#,Windows Runtime,Windows Phone 8.1,Bytearray,Bitmapimage,有一些示例可以这样做,但它们是针对Windows Phone 8.0或8.1 Silverlight的 但如何在WindowsPhone8.1运行时做到这一点 试过这个吗 private byte[] ConvertToBytes(BitmapImage bitmapImage) { byte[] data = null; using (MemoryStream stream = new MemoryStream()) { WriteableBitmap

有一些示例可以这样做,但它们是针对Windows Phone 8.0或8.1 Silverlight的

但如何在WindowsPhone8.1运行时做到这一点

试过这个吗

private byte[] ConvertToBytes(BitmapImage bitmapImage)
{
    byte[] data = null;
    using (MemoryStream stream = new MemoryStream())
    {
        WriteableBitmap wBitmap = new WriteableBitmap(bitmapImage);
        wBitmap.SaveJpeg(stream, wBitmap.PixelWidth, wBitmap.PixelHeight, 0, 100);
        stream.Seek(0, SeekOrigin.Begin);
        data = stream.GetBuffer();
    }

    return data;
}

无法从Windows.UI.Xaml.Media.Imaging中提取像素

最通用的解决方案是使用WriteableBitmap而不是BitmapImage。这两个类都是,几乎可以互换使用。通过其属性提供对其像素数据的访问:

byte[] pixelArray = myWriteableBitmap.PixelBuffer.ToArray(); // convert to Array
Stream pixelStream = wb.PixelBuffer.AsStream();  // convert to stream
否则,您将需要从BitmapImage从何处获取像素。根据BitmapImage的初始化方式,您可以从其属性中找到其来源。具有基于其URI源从位图图像创建可写位图的权限


一个丑陋的选项是将位图图像渲染成图像,基于图像创建一个位图,然后使用RenderTargetBitmap获取其像素。(

有一些注意事项。@marcinax您读过标题了吗?或者我发布了什么?我知道有100个样本和问题要做。。。您发布的链接是针对Windows Phone“7”的!!!!所以给你另一个链接:。这是给温特的。使用搜索。@marcinax不起作用。这对Windows Phone运行时应用程序不起作用,因为Windows.UI.Xaml.Media.Imaging.WriteableBitmap没有接受位图的构造函数。不,这也不是UWP代码。UWP也没有这样的构造函数;var stream=await-storageFile.OpenAsync(FileAccessMode.Read);var bitmapImage=new Windows.UI.Xaml.Media.Imaging.bitmapImage();等待bitmapImage.SetSourceAsync(流);var decoder=wait Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(流);我现在在不同的地方看到过几次相同的代码片段,想知道像素流是否需要处理?必须使用System.Runtime.InteropServices.WindowsRuntime添加
访问
AsStream()
扩展方法。