本文介绍了将RenderTargetBitmap转换为BitmapImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个 RenderTargetBitmap ,我需要将其转换为 BitmapImage 。请检查下面的代码。

I have a RenderTargetBitmap, I need to convert it to BitmapImage. Please check the code below.

 RenderTargetBitmap bitMap = getRenderTargetBitmap();
 Image image = new Image();// This is a Image
 image.Source = bitMap;

在上面的代码中,我使用了Image。现在,我需要使用BitmapImage。我该怎么做?

In the above code I have used Image.Now I need to use a BitmapImage. How can I do this?

 RenderTargetBitmap bitMap = getRenderTargetBitmap();
 BitmapImage image = new BitmapImage();// This is a BitmapImage
 // how to set bitMap as source of BitmapImage ?


推荐答案

尽管似乎没有必要进行转换将 RenderTargetBitmap 转换为 BitmapImage ,您可以轻松编码 RenderTargetBitmap 放入 MemoryStream 并从该流中解码 BitmapImage

Although it doesn't seem to be necessary to convert a RenderTargetBitmap into a BitmapImage, you could easily encode the RenderTargetBitmap into a MemoryStream and decode the BitmapImage from that stream.

有几个在WPF中,下面的示例代码使用 PngBitmapEncoder

There are several BitmapEncoders in WPF, the sample code below uses a PngBitmapEncoder.

var renderTargetBitmap = getRenderTargetBitmap();
var bitmapImage = new BitmapImage();
var bitmapEncoder = new PngBitmapEncoder();
bitmapEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));

using (var stream = new MemoryStream())
{
    bitmapEncoder.Save(stream);
    stream.Seek(0, SeekOrigin.Begin);

    bitmapImage.BeginInit();
    bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
    bitmapImage.StreamSource = stream;
    bitmapImage.EndInit();
}

这篇关于将RenderTargetBitmap转换为BitmapImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 01:25