如何从SoftwareBitmap获取字节数组

8

你好,我需要帮助,想知道如何从C# UWP的SoftwareBitmap中获取字节数组,以便通过TCP套接字发送。

我还可以访问“VideoFrame previewFrame”对象,这是我从中获取SoftwareBitmap的地方。

我在网上看到了如下操作,但是UWP不支持wb.SaveJpeg(...)。除非我漏掉了什么?

MemoryStream ms = new MemoryStream();
WriteableBitmap wb = new WriteableBitmap(myimage);
wb.SaveJpeg(ms, myimage.PixelWidth, myimage.PixelHeight, 0, 100);
byte [] imageBytes = ms.ToArray();

任何帮助或指引都将不胜感激。谢谢,安迪。

1
这个问题与TCP无关,专注于你真正的问题“如何从UWP中的SoftwareBitmapSource获取字节数组”,暂时不要涉及TCP相关内容。 - Scott Chamberlain
好的,那你能告诉我如何在UWP中从SoftwareBitmapSource获取字节数组吗? - Andy
我是说编辑你的问题,并用一个更专注的问题来更新它(另外我不做uwp,所以我不知道答案)。 - Scott Chamberlain
4个回答

9
当然,你可以从一个SoftwareBitmap访问一个编码的byte[]数组。
看这个例子,提取一个jpeg编码的byte[]
// includes BitmapEncoder, which defines some static encoder IDs
using Windows.Graphics.Imaging;


private async void PlayWithData(SoftwareBitmap softwareBitmap)
{
    // get encoded jpeg bytes
    var data = await EncodedBytes(softwareBitmap, BitmapEncoder.JpegEncoderId);

    // todo: save the bytes to a DB, etc
}

private async Task<byte[]> EncodedBytes(SoftwareBitmap soft, Guid encoderId)
{
    byte[] array = null;

    // First: Use an encoder to copy from SoftwareBitmap to an in-mem stream (FlushAsync)
    // Next:  Use ReadAsync on the in-mem stream to get byte[] array

    using (var ms = new InMemoryRandomAccessStream())
    {
        BitmapEncoder encoder = await BitmapEncoder.CreateAsync(encoderId, ms);
        encoder.SetSoftwareBitmap(soft);

        try
        {
            await encoder.FlushAsync();
        }
        catch ( Exception ex ){ return new byte[0]; }

        array = new byte[ms.Size];
        await ms.ReadAsync(array.AsBuffer(), (uint)ms.Size, InputStreamOptions.None);
    }
    return array;
}

3
为什么那个应该正确的答案(这个)被埋在了最后,而一个完全不相关的代码却获得了最高票数! - Vijay Chavda

5

要从SoftwareBitmap获取字节数组,可以使用 "SoftwareBitmap.CopyToBuffer"。

但是,首先需要:

using System.Runtime.InteropServices.WindowsRuntime;

因为需要使用AsBuffer()方法将其转换为byte[]数组。

...

StorageFile file = await StorageFile.GetFileFromPathAsync(ImageFilePath);
using (IRandomAccessStream fileStream = await File.OpenAsync(FileAccessMode.Read),
                                           memStream = new InMemoryRandomAccessStream())
  {
  // Open a Stream and decode a JPG image
  BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);
  var softwareBitmap = await decoder.GetSoftwareBitmapAsync();

  byte [] imageBytes = new byte[4*decoder.PixelWidth*decoder.PixelHeight];
  softwareBitmap.CopyToBuffer(imageBytes.AsBuffer());
  //...  now you can use the imageBytes[]
}

欢迎来到SO。请考虑为您的代码添加上下文。同时,请仔细检查您的代码格式。 - Richard Erickson

4
据我所知,你无法这样做。但是你可以使用SoftwareBitmap进行操作。请参见以下示例:https://msdn.microsoft.com/en-us/library/windows/apps/mt244351.aspx(SoftwareBitmap是SoftwareBitmapSource的私有字段... 通过反射读取它... 可能这个建议完全是错误的)
private async void SaveSoftwareBitmapToFile(SoftwareBitmap softwareBitmap, StorageFile outputFile)
{
    using (IRandomAccessStream stream = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
    {
        // Create an encoder with the desired format
        BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);

        // Set the software bitmap
        encoder.SetSoftwareBitmap(softwareBitmap);

        // Set additional encoding parameters, if needed
        encoder.BitmapTransform.ScaledWidth = 320;
        encoder.BitmapTransform.ScaledHeight = 240;
        encoder.BitmapTransform.Rotation = Windows.Graphics.Imaging.BitmapRotation.Clockwise90Degrees;
        encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Fant;
        encoder.IsThumbnailGenerated = true;

        try
        {
            await encoder.FlushAsync();
        }
        catch (Exception err)
        {
            switch (err.HResult)
            {
                case unchecked((int)0x88982F81): //WINCODEC_ERR_UNSUPPORTEDOPERATION
                                                 // If the encoder does not support writing a thumbnail, then try again
                                                 // but disable thumbnail generation.
                    encoder.IsThumbnailGenerated = false;
                    break;
                default:
                    throw;
            }
        }

        if (encoder.IsThumbnailGenerated == false)
        {
            await encoder.FlushAsync();
        }


    }
}

1
然而我并不是从文件中打开图像,而是从VideoFrame中获取SoftwareBitmap。那么我该如何从VideoFrame中打开流呢? - Andy
这段代码将 SoftwareBitmap 保存到文件。使用编码器将 softwarebitmap 转换为流:https://msdn.microsoft.com/en-us/library/windows/apps/br226212.aspx。您可以使用任何 IRandomAccessStream 流。在此处查看如何转换为 io 流:https://dev59.com/f2sz5IYBdhLWcg3wxq0V。 - kain64b
我不理解你的评论。我的理解是BitmapEncoder需要使用IRandomAccessStream进行初始化。但是,我该如何从SoftwareBitmap获取流呢? - Andy
Andy,有两种类型的流,输入和输出 :) 我的例子是关于输出流的,正好符合你的需求。编码器将数据写入流中。正如Scott Chamberlain所说:使用InMemoryRandomAccessStream。 - kain64b
1
不应该使用 throw err; 重新抛出错误,因为它会覆盖堆栈跟踪。应该使用 throw; - Liero
显示剩余3条评论

3
获取预览帧 UWP相机示例获取一个SoftwareBitmap作为相机帧,并通过数组对其像素进行操作(原地操作),之后可以将其保存为JPEG格式。如果我正确理解了您的问题,那么您所需的所有代码应该都在其中。
基本上,这应该是大部分内容(包括一些相机代码):
private async Task GetPreviewFrameAsSoftwareBitmapAsync()
{
    // Get information about the preview
    var previewProperties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;

    // Create the video frame to request a SoftwareBitmap preview frame
    var videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, (int)previewProperties.Width, (int)previewProperties.Height);

    // Capture the preview frame
    using (var currentFrame = await _mediaCapture.GetPreviewFrameAsync(videoFrame))
    {
        // Collect the resulting frame
        SoftwareBitmap previewFrame = currentFrame.SoftwareBitmap;

        // Add a simple green filter effect to the SoftwareBitmap
        EditPixels(previewFrame);
    }
}

private unsafe void EditPixels(SoftwareBitmap bitmap)
{
    // Effect is hard-coded to operate on BGRA8 format only
    if (bitmap.BitmapPixelFormat == BitmapPixelFormat.Bgra8)
    {
        // In BGRA8 format, each pixel is defined by 4 bytes
        const int BYTES_PER_PIXEL = 4;

        using (var buffer = bitmap.LockBuffer(BitmapBufferAccessMode.ReadWrite))
        using (var reference = buffer.CreateReference())
        {
            // Get a pointer to the pixel buffer
            byte* data;
            uint capacity;
            ((IMemoryBufferByteAccess)reference).GetBuffer(out data, out capacity);

            // Get information about the BitmapBuffer
            var desc = buffer.GetPlaneDescription(0);

            // Iterate over all pixels
            for (uint row = 0; row < desc.Height; row++)
            {
                for (uint col = 0; col < desc.Width; col++)
                {
                    // Index of the current pixel in the buffer (defined by the next 4 bytes, BGRA8)
                    var currPixel = desc.StartIndex + desc.Stride * row + BYTES_PER_PIXEL * col;

                    // Read the current pixel information into b,g,r channels (leave out alpha channel)
                    var b = data[currPixel + 0]; // Blue
                    var g = data[currPixel + 1]; // Green
                    var r = data[currPixel + 2]; // Red

                    // Boost the green channel, leave the other two untouched
                    data[currPixel + 0] = b;
                    data[currPixel + 1] = (byte)Math.Min(g + 80, 255);
                    data[currPixel + 2] = r;
                }
            }
        }
    }
}

在类外声明以下内容以启用GetBuffer方法:

[ComImport]
[Guid("5b0d3235-4dba-4d44-865e-8f1d0e4fd04d")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
unsafe interface IMemoryBufferByteAccess
{
    void GetBuffer(out byte* buffer, out uint capacity);
}

当然,为了让所有这些工作正常运行,您的项目必须允许不安全代码。
最后,以下是将SoftwareBitmap保存为JPEG文件的方法:
private static async Task SaveSoftwareBitmapAsync(SoftwareBitmap bitmap, StorageFile file)
{
    using (var outputStream = await file.OpenAsync(FileAccessMode.ReadWrite))
    {
        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, outputStream);

        // Grab the data from the SoftwareBitmap
        encoder.SetSoftwareBitmap(bitmap);
        await encoder.FlushAsync();
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接