在 UWP 中将 BitmapImage 转换为 SoftwareBitmap [英] Convert BitmapImage to SoftwareBitmap in UWP

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

问题描述

我需要了解如何将 BitmapImage 对象转换为 SoftwareBitmap,以便将其保存到文件中而不会像使用 RenderTargetBitmap 那样丢失分辨率.

I need to find out how to convert a BitmapImage object into a SoftwareBitmap in order to save it to a file without losing resolution as I would do with RenderTargetBitmap.

我有以下情况:

private void TakePic() {
    BitmapImage currentImage = new BitmapImage(new Uri("http://.../image.jpg"));
    ImageControl.Source = currentImage;
}

private void SavePic() {
    StorageFile file = await folder.CreateFileAsync(...);
    using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
    {
        BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
        encoder.SetSoftwareBitmap();
        await encoder.FlushAsync();
    }
}   

我不能在 ImageControl 上使用 RenderTargetBitmap,因为我来自 url 的图像比 ImageControl 大小大得多.使用 RenderTargetBitmap,我会将输出缩小到此控件的大小.

I can't use RenderTargetBitmap on ImageControl because my image from url is way bigger than the ImageControl size. With RenderTargetBitmap I would shrink my output to the size of this control.

是否有任何解决方法可以实现这一目标?

Is there any workaround to achieve this?

推荐答案

我觉得既然我们有本地保存图片的需求,我们可以适当分配一些系统资源来保存图片流.

I think that since we have the need to save the image locally, we can properly allocate some system resources to save the image stream.

因为我们需要SoftwareBitmap,所以我们不必使用BitmapImage 作为Image 的来源.

Since we need SoftwareBitmap, we don't have to use BitmapImage as the source of the Image.

这是我的测试代码:

SoftwareBitmap CacheBitmap = null;

public TestPage()
{
    this.InitializeComponent();
    SetImageSource();
}

public async void SetImageSource()
{
    // Load image from web
    WebRequest myrequest = WebRequest.Create("http://.../image.jpg");
    WebResponse myresponse = myrequest.GetResponse();
    var imgstream = myresponse.GetResponseStream();

    // Try to create SoftwareBitmap
    MemoryStream ms = new MemoryStream();
    imgstream.CopyTo(ms);
    var decoder = await BitmapDecoder.CreateAsync(ms.AsRandomAccessStream());
    var softBitmap = await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8,BitmapAlphaMode.Premultiplied);

    // Use SoftwareBitmapSource to ImageSource
    var source = new SoftwareBitmapSource();
    await source.SetBitmapAsync(softBitmap);
    TestImage.Source = source;

    // Keep reference
    CacheBitmap = softBitmap;
}

当需要保存图片时,可以使用这个SoftwareBitmap:

When you need to save image, you can use this SoftwareBitmap:

private void SavePic() {
    StorageFile file = await folder.CreateFileAsync(...);
    using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
    {
        BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
        encoder.SetSoftwareBitmap(CacheBitmap);
        await encoder.FlushAsync();
    }
}  

注意.

当我们最初使用 Uri 作为图像源时,下载是在 Image 内部完成的.我们无法获取图片源作为源流,所以我们可以将下载过程提取到外部,这样我们就可以控制原始数据流.

When we initially use Uri as the image source, the download is done inside the Image. We can't get the image source as source stream, so we can extract the download process to the outside, so that we can control the original data stream.

最好的问候.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆