Windows 8 如何在WinJS中显示Windows.Graphics.Imaging.BitmapDecoder?
我正在尝试制作一款应用过滤效果的应用程序。从FilePicker我可以得到一个IRandomaccesstream,我可以把它转换成位图解码器。但我不知道如何将位图数据显示为图像?我不想使用文件名或路径,只想将位图数据显示为图像。我应该在WinJS中使用什么控件以及如何使用它?使用Windows 8 如何在WinJS中显示Windows.Graphics.Imaging.BitmapDecoder?,windows-8,microsoft-metro,winjs,Windows 8,Microsoft Metro,Winjs,我正在尝试制作一款应用过滤效果的应用程序。从FilePicker我可以得到一个IRandomaccesstream,我可以把它转换成位图解码器。但我不知道如何将位图数据显示为图像?我不想使用文件名或路径,只想将位图数据显示为图像。我应该在WinJS中使用什么控件以及如何使用它?使用MSApp.createStreamFromInputStream()和URL.createObjectURL()将IIInputStream或IRandomAccessStream转换为图像: HTML应该如下所示:
MSApp.createStreamFromInputStream()
和URL.createObjectURL()
将IIInputStream或IRandomAccessStream转换为图像:
HTML应该如下所示:
<img id="theImage" src="#" />
这些MSDN博客更详细地解释了以下过程 http://goo.gl/OLgfn 我找到了一种从位图解码器创建位图编码器的方法。该编码器可用于创建可在字段中显示的内存流
internal async Task<InMemoryRandomAccessStream> applyFilterInternal()
{
inputStream = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(inputStream);
var memStream = new Windows.Storage.Streams.InMemoryRandomAccessStream();
var encoder = await Windows.Graphics.Imaging.BitmapEncoder.CreateForTranscodingAsync(memStream, decoder);
encoder.BitmapTransform.ScaledWidth = 640;
encoder.BitmapTransform.ScaledHeight = 480;
encoder.BitmapTransform.Rotation = Windows.Graphics.Imaging.BitmapRotation.Clockwise90Degrees;
// Fant is a relatively high quality interpolation algorithm.
encoder.BitmapTransform.InterpolationMode = Windows.Graphics.Imaging.BitmapInterpolationMode.Fant;
// Attempt to generate a new thumbnail from the updated pixel data.
// Note: Only JPEG, TIFF and JPEG-XR images support encoding thumbnails.
encoder.IsThumbnailGenerated = true;
await encoder.FlushAsync();
return memStream;
}
public IAsyncOperation<InMemoryRandomAccessStream> applyFilter()
{
Task<InMemoryRandomAccessStream> from = applyFilterInternal();
IAsyncOperation<InMemoryRandomAccessStream> to = from.AsAsyncOperation();
return to;
}
Kiewic,我感谢你的回答。但我面临的实际问题是,我需要从BitmapDecoder对象中提取IRandomAccessStream。我尝试了提供的几种方法,但我无法做到这一点。
internal async Task<InMemoryRandomAccessStream> applyFilterInternal()
{
inputStream = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(inputStream);
var memStream = new Windows.Storage.Streams.InMemoryRandomAccessStream();
var encoder = await Windows.Graphics.Imaging.BitmapEncoder.CreateForTranscodingAsync(memStream, decoder);
encoder.BitmapTransform.ScaledWidth = 640;
encoder.BitmapTransform.ScaledHeight = 480;
encoder.BitmapTransform.Rotation = Windows.Graphics.Imaging.BitmapRotation.Clockwise90Degrees;
// Fant is a relatively high quality interpolation algorithm.
encoder.BitmapTransform.InterpolationMode = Windows.Graphics.Imaging.BitmapInterpolationMode.Fant;
// Attempt to generate a new thumbnail from the updated pixel data.
// Note: Only JPEG, TIFF and JPEG-XR images support encoding thumbnails.
encoder.IsThumbnailGenerated = true;
await encoder.FlushAsync();
return memStream;
}
public IAsyncOperation<InMemoryRandomAccessStream> applyFilter()
{
Task<InMemoryRandomAccessStream> from = applyFilterInternal();
IAsyncOperation<InMemoryRandomAccessStream> to = from.AsAsyncOperation();
return to;
}
filter.applyFilter().then(function (memStream) {
var msStream = MSApp.createStreamFromInputStream("image/jpeg", memStream);
var imageURL = URL.createObjectURL(msStream);
id("imageInput").src = imageURL;
});