在UWP中将BitmapImage转换为软件位图

在UWP中将BitmapImage转换为软件位图,uwp,type-conversion,bitmapimage,Uwp,Type Conversion,Bitmapimage,我需要了解如何将BitmapImage对象转换为软件位图,以便在不丢失分辨率的情况下将其保存到文件中,就像使用RenderTargetBitmap一样 我有以下情况: private void TakePic() { BitmapImage currentImage = new BitmapImage(new Uri("http://.../image.jpg")); ImageControl.Source = currentImage; } private void Save

我需要了解如何将BitmapImage对象转换为软件位图,以便在不丢失分辨率的情况下将其保存到文件中,就像使用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,我会将输出缩小到该控件的大小


有什么解决方法可以实现这一点吗?

我认为,由于我们需要在本地保存图像,我们可以适当地分配一些系统资源来保存图像流

由于我们需要
软件位图
,因此不必使用
位图图像
作为图像源

以下是我的测试代码:

SoftwareBitmap CacheBitmap=null;
公共测试页()
{
this.InitializeComponent();
SetImageSource();
}
公共异步void SetImageSource()
{
//从web加载图像
WebRequest myrequest=WebRequest.Create(“http://.../image.jpg");
WebResponse myresponse=myrequest.GetResponse();
var imgstream=myresponse.GetResponseStream();
//尝试创建软件位图
MemoryStream ms=新的MemoryStream();
imgstream.CopyTo(ms);
var decoder=await BitmapDecoder.CreateAsync(ms.AsRandomAccessStream());
var softBitmap=wait decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8,BitmapAlphaMode.Premultiplied);
//使用SoftwareBitmapSource创建图像源
var source=新软件BitMapSource();
wait source.SetBitmapAsync(软位图);
TestImage.Source=源;
//保持参考
CacheBitmap=软位图;
}
当您需要保存图像时,可以使用此
软件位图

private void SavePic(){
StorageFile file=wait folder.CreateFileAsync(…);
使用(irandomaccesstream=await file.OpenAsync(FileAccessMode.ReadWrite))
{
BitmapEncoder编码器=等待BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId,流);
编码器。设置软件EBITMAP(缓存位图);
等待编码器。FlushAsync();
}
}  
注意

当我们最初使用Uri作为图像源时,下载是在图像内部完成的。我们无法将图像源作为源流,因此我们可以将下载过程提取到外部,以便控制原始数据流


致以最诚挚的问候。

我认为,由于我们需要在本地保存图像,我们可以适当地分配一些系统资源来保存图像流

由于我们需要
软件位图
,因此不必使用
位图图像
作为图像源

以下是我的测试代码:

SoftwareBitmap CacheBitmap=null;
公共测试页()
{
this.InitializeComponent();
SetImageSource();
}
公共异步void SetImageSource()
{
//从web加载图像
WebRequest myrequest=WebRequest.Create(“http://.../image.jpg");
WebResponse myresponse=myrequest.GetResponse();
var imgstream=myresponse.GetResponseStream();
//尝试创建软件位图
MemoryStream ms=新的MemoryStream();
imgstream.CopyTo(ms);
var decoder=await BitmapDecoder.CreateAsync(ms.AsRandomAccessStream());
var softBitmap=wait decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8,BitmapAlphaMode.Premultiplied);
//使用SoftwareBitmapSource创建图像源
var source=新软件BitMapSource();
wait source.SetBitmapAsync(软位图);
TestImage.Source=源;
//保持参考
CacheBitmap=软位图;
}
当您需要保存图像时,可以使用此
软件位图

private void SavePic(){
StorageFile file=wait folder.CreateFileAsync(…);
使用(irandomaccesstream=await file.OpenAsync(FileAccessMode.ReadWrite))
{
BitmapEncoder编码器=等待BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId,流);
编码器。设置软件EBITMAP(缓存位图);
等待编码器。FlushAsync();
}
}  
注意

当我们最初使用Uri作为图像源时,下载是在图像内部完成的。我们无法将图像源作为源流,因此我们可以将下载过程提取到外部,以便控制原始数据流


致以最良好的祝愿。

你好。我注意到图像转换的目的是在本地保存图像。然后,您可以尝试使用
HttpClient
下载图像,而无需进行图像转换。问题是,当用户决定保存在ImageControl中预览的图像时,服务器中可能不再存在,因为我有一个连续的“图像流”在服务器和API上,我可以在特定时间下载图像。但是,它只允许下载最后n秒的图像。如果用户使用第(n+1)秒的pic,则不再可用。我不知道我是否能自己解释我知道。所以我可以假设你只有一次机会从链接中获取图像。我理解您为什么要使用
软件位图
,但您似乎对如何将
位图
转换为
软件位图
有疑问。我能给出我的解决方案吗?当然,我会一直听那些可能有帮助的解决方案。我注意到图像转换的目的是在本地保存图像。然后,您可以尝试使用
HttpClient
下载图像,而无需进行图像转换。问题是,当用户决定保存在ImageControl中预览的图像时,服务器中可能不再存在,因为我有一个连续的“图像流”在服务器和API上,我可以在特定时间下载图像。但是,它只允许下载最后n秒的图像。如果用户使用第(n+1)秒的pic,则不再可用。我不知道我是否能自己解释我知道。