加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 18:14:28 所属栏目:百科 来源:网络整理
导读:嗨我需要有关如何从C#UWP中的SoftwareBitmap获取字节数组的帮助,以便我可以通过TCP套接字发送它. 我还可以访问“VideoFrame previewFrame”对象,这是我从中获取SoftwareBitmap的地方. 我在网上看过类似下面的内容,但是UWP不支持wb.SaveJpeg(…).除非我错过了
嗨我需要有关如何从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();
        }


    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读