问题描述
我的工作适用过滤器,以图像的Windows 8地铁应用程序。我有应用程序的Web版本,并希望将它移植。但是,正如我们都知道的WinRT不具备一切美好的事物.NET另有规定:/
I am working on a Windows 8 Metro app that applies filters to images. I have a web version of the app and wanted to port it. But as we all know WinRT doesn't have all the good things .NET provides otherwise :/
目前,我一个字节数组上应用过滤器,我想保留这样的说法,因为它是超级快!所以这几天我一直在寻找方法来转换StorageFile为byte [],然后到BitmapImage的字节[]。
Currently I am applying the filters on a byte array and I want to keep it that way, because it's super fast! So for the past few days I have been searching for ways to convert a StorageFile to byte[] and then byte[] to BitmapImage.
到目前为止,我已经能够做到第一个(StorageFile为byte [])。下面是我如何做到这一点:
So far I have managed to do the first one (StorageFile to byte[]). Here is how I do it:
public async Task<Byte[]> ImageFileToByteArray(StorageFile file)
{
IRandomAccessStream stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
PixelDataProvider pixelData = await decoder.GetPixelDataAsync();
return pixelData.DetachPixelData();
}
这一段代码返回字节[]
包含的像素数据为BGRA。
This piece of code returns a byte[]
that contains the pixel data as BGRA.
和来这里的棘手的部分。我不能字节数组成功地转换成的BitmapImage。我已经找遍了的地方和许多人建议使用WriteableBitmap的,但这并不做很多对我好。我还发现了应工作的代码一些作品......但他们不这样做。
And here comes the tricky part. I cannot successfully convert the byte array into a BitmapImage. I have searched all over the places and many people suggest using WriteableBitmap but that doesn't do much good to me. I have also found some pieces of code that should be working... but they don't.
一个我已经使用InMemoryRandomAccessStream这样的尝试的解决方案:
One of the solutions I have tried is using InMemoryRandomAccessStream like this:
public async Task<BitmapImage> ByteArrayToBitmapImage(Byte[] pixels)
{
var stream = new InMemoryRandomAccessStream();
await stream.WriteAsync(pixels.AsBuffer());
stream.Seek(0);
var image = new BitmapImage();
await image.SetSourceAsync(stream);
return image;
}
这之一抛出以下异常:
类型的异常'System.Exception的'发生在mscorlib.dll,但在用户代码中没有处理
其他信息:组件无法找到。 (从HRESULT异常:0x88982F50)
Additional information: The component cannot be found. (Exception from HRESULT: 0x88982F50)
我试着用这条线来代替:
I tried using this line instead:
PixelDataProvider pixelData = await decoder.GetPixelDataAsync(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Ignore,
new BitmapTransform(),
ExifOrientationMode.IgnoreExifOrientation,
ColorManagementMode.DoNotColorManage);
但它确实我没有好,因为我不断收到这个异常。
But it did me no good since I keep getting that exception.
我也试过这样的:
var bitmapImage = new BitmapImage();
var pixels = await ImageFileToByteArray(file);
ImageSource imgSource;
using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
{
using (DataWriter writer = new DataWriter(ms.GetOutputStreamAt(0)))
{
writer.WriteBytes(pixels);
await writer.StoreAsync();
}
await bitmapImage.SetSourceAsync(ms);
imgSource = bitmapImage;
}
和获得相同的异常的第一段代码。
And get the same exception as the first piece of code.
我也试过,包括使用正常流,然后转换成IRandomAccessStream几个其他的方式,但他们也不能工作。
I have also tried several other ways that include using a normal Stream then converting into a IRandomAccessStream but they didn't work either.
所有上面的代码似乎没什么问题。所以,我此刻的猜测是,这个问题是在字节[]
。我猜的PixelData取出的格式里面是无效的,所以我试图将其更改为RGBA但这并没有帮助。另外,PixelHeight和BitmapImage的的PixelWidth为0。
All of the above code seems fine to me. So my guess at the moment is that the problem is in the byte[]
. I'm guessing that the format of the pixelData inside is not valid, so I tried changing it to RGBA but that didn't help either. Also the PixelHeight and PixelWidth of the BitmapImage are 0.
推荐答案
这是为我工作,
private async Task<BitmapImage> ByteArrayToBitmapImage(byte[] byteArray)
{
var bitmapImage = new BitmapImage();
var stream = new InMemoryRandomAccessStream();
await stream.WriteAsync(byteArray.AsBuffer());
stream.Seek(0);
bitmapImage.SetSource(stream);
return bitmapImage;
}
这篇关于C#的Windows 8商店(地铁,WinRT的)字节数组的BitmapImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!