简体   繁体   English

怎么把UWP SoftwareBitmap转换成WPF Bitmap?

[英]How to convert UWP SoftwareBitmap to WPF Bitmap?

I'm trying to get a SoftwareBitmap to be usable by stuff in WPF as a Bitmap . 我正在尝试使SoftwareBitmap可以作为WPF中的东西用作Bitmap Approaches like this question shows look promising, but they seem to be using objects from different namespaces that don't quite work in my case. 这个问题所示的方法看起来很有前途,但是它们似乎正在使用来自不同名称空间的对象,这些对象在我的情况下不太起作用。 For context, I'm using MediaCapture from UWP land for webcam streaming in my app which is WPF. 就上下文而言,我将UWP land中的MediaCapture用于WPF应用程序中的网络摄像头流。

public static WriteableBitmap WriteableBitmapFromSoftwareBitmap(SoftwareBitmap soft)
{
    WriteableBitmap writeable = new 
    WriteableBitmap(soft.PixelWidth, soft.PixelHeight);
    soft.CopyToBuffer(writeable.PixelBuffer);

    return writeable;
}

public static Bitmap BitmapFromWriteableBitmap(WriteableBitmap writeBmp)
{
    Bitmap bmp;
    using (MemoryStream outStream = new MemoryStream())
    {
        System.Windows.Media.Imaging.BitmapEncoder enc = new 
            BmpBitmapEncoder();

        enc.Frames.Add(BitmapFrame.Create(writeBmp)); // Error here
        enc.Save(outStream);
        bmp = new Bitmap(outStream);
    }

    return bmp;
}

Unfortunately, in BitmapFromWriteableBitmap , the call to BitmapFrame.Create is complaining that it can't create a URI from that kind of WriteableBitmap . 不幸的是,在BitmapFromWriteableBitmap ,调用BitmapFrame.Create抱怨它不能从那种创建一个URI WriteableBitmap

Note that the WriteableBitmap is a Windows.UI.Xaml.Media.Imaging.WriteableBitmap , the SoftwareBitmap is a Windows.Graphics.Imaging.SoftwareBitmap , and the desired Bitmap is a System.Drawing.Bitmap . 请注意, WriteableBitmapWindows.UI.Xaml.Media.Imaging.WriteableBitmapSoftwareBitmapWindows.Graphics.Imaging.SoftwareBitmap ,而所需的BitmapSystem.Drawing.Bitmap

There is no direct conversion. 没有直接转换。 You'll need to extract the image data from the SoftwareBitmap and then create the new Bitmap from that data. 您需要从SoftwareBitmap中提取图像数据,然后根据该数据创建新的Bitmap。

This is essentially what the linked question does: it encodes the data from the WriteableBitmap into a .BMP stream and then loads the System.Drawing.Bitmap from that stream. 本质上,这是链接问题的工作方式:它将WriteableBitmap中的数据编码为.BMP流,然后从该流中加载System.Drawing.Bitmap。

You can do the same from the SoftwareBitmap by using Windows.Graphics.Imaging.BitmapEncoder . 您可以使用Windows.Graphics.Imaging.BitmapEncoder在SoftwareBitmap中执行相同的操作。 SetSoftwareBitmap to convert its contents to an image stream and then create a new System.Drawing.Bitmap from that stream. SetSoftwareBitmap将其内容转换为图像流,然后从该流创建一个新的System.Drawing.Bitmap。

See Save a SoftwareBitmap to a file with BitmapEncoder for sample code. 请参阅使用BitmapEncoder将SoftwareBitmap保存到文件中以获取示例代码。 You can render to an InMemoryRandomAccessStream instead of a StorageFile's stream to avoid saving to disk, and can use AsStream to convert it to a .Net System.IO.Stream to read into the System.Drawing.Bitmap something like the following (untested) snippet: 您可以呈现给InMemoryRandomAccessStream而不是StorageFile的流,以避免保存到磁盘,并且可以使用AsStream将其转换为.Net System.IO.Stream,以将其读取到System.Drawing.Bitmap中,类似于以下(未经测试的)代码片段。 :

using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
{
    BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
    encoder.SetSoftwareBitmap(softwareBitmap);
    await encoder.FlushAsync();
    bmp = new System.Drawing.Bitmap(stream.AsStream);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM