c# – 如何从SoftwareBitmap获取字节数组

前端之家收集整理的这篇文章主要介绍了c# – 如何从SoftwareBitmap获取字节数组前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
嗨我需要有关如何从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,100);
byte [] imageBytes = ms.ToArray();

任何帮助,或正确方向的指示,将不胜感激.

谢谢,安迪

解决方法

到目前为止我知道你不能这样做.但您可以使用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();
        }


    }
}
原文链接:https://www.f2er.com/csharp/97905.html

猜你在找的C#相关文章