问题描述
这是一个跟进的答案,我已经previously发布了一个问题here.
我把这个建议的回答到一个异步函数:
公共静态异步任务< INT [] GT; bitmapToIntArray(图像的BitmapImage)
{
字符串文件名=/Images/flowers.jpg; StorageFile文件=等待ApplicationData.Current.LocalFolder.GetFileAsync(文件名);
IRandomAccessStream流=等待file.OpenAsync(FileAccessMode.Read);
BitmapDe codeR德codeR =等待BitmapDe coder.CreateAsync(流); VAR PixelData取出=伺机脱coder.GetPixelDataAsync();
变种像素= pixelData.DetachPixelData(); VAR WIDTH =去coder.OrientedPixelWidth;
VAR HEIGHT =去coder.OrientedPixelHeight; INT [] =色彩新INT [宽*高]。
对于(VAR I = 0; I<高度;我++)
{
对于(VAR J = 0; J<宽度; J ++)
{
字节R =像素[(我*高+ J)* 4 + 0]; //红
字节G =像素[(我*高+ J)* 4 + 1]; //绿色
字节B =像素[(我*高+ J)* 4 + 2]; //蓝(RGBA) 颜色[I *高+ J] = R;
}
} 返回颜色;
}
这是从下面的main函数调用
公共无效ApplyImageFilter(图片userImage,诠释imageWidth,诠释imageHeight)
{
...
INT [] SRC = PixelUtils.bitmapToIntArray(userImage)。结果;
...
;
}
然而,当我踏进上面的行,会发生什么情况是,只有bitmapToIntArray功能的第二行:
StorageFile文件=等待ApplicationData.Current.LocalFolder.GetFileAsync(文件名);
等待直到完成,然后将其通过该函数的其余跳回ApplyImageFilter和步骤(以及在端给出一个错误)。它不会去任何一行在bitmapToIntArray功能第一的await后。我比较了previous项目我成功地做了异步/等待着,似乎像我遵循同样的程序两次。还阅读了关于异步/等待功能,并与code位打左右,但没有运气。我在上还有什么我可以尝试的损失,所以任何建议将大大AP preciated。
您可以通过调用阻塞UI线程结果
。
一个你去异步,你必须去异步的所有道路。
公共异步任务ApplyImageFilter(图片userImage,诠释imageWidth,诠释imageHeight)
{
...
INT [] SRC =等待PixelUtils.bitmapToIntArray(userImage);
...
}
有关更多信息异步等待
,读的。
This is a follow up to an answer to a question I had previously posted here.
I put the suggested answer into an async function:
public static async Task<int[]> bitmapToIntArray(Image bitmapImage)
{
string fileName = "/Images/flowers.jpg";
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
var pixelData = await decoder.GetPixelDataAsync();
var pixels = pixelData.DetachPixelData();
var width = decoder.OrientedPixelWidth;
var height = decoder.OrientedPixelHeight;
int[] colors = new int[width * height];
for (var i = 0; i < height; i++)
{
for (var j = 0; j < width; j++)
{
byte r = pixels[(i * height + j) * 4 + 0]; //red
byte g = pixels[(i * height + j) * 4 + 1]; //green
byte b = pixels[(i * height + j) * 4 + 2]; //blue (rgba)
colors[i * height + j] = r;
}
}
return colors;
}
It is being called from the main function below:
public void ApplyImageFilter(Image userImage, int imageWidth, int imageHeight)
{
...
int[] src = PixelUtils.bitmapToIntArray(userImage).Result;
...
;
}
However, when I step into the line above, what happens is that only the second line of the bitmapToIntArray function:
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
is waited until done, and then it jumps back to the ApplyImageFilter and steps through the rest of that function (and gives an error at the end). It doesn't go to any line after the first await in the bitmapToIntArray function. I've compared async/await with a previous project I did successfully and it seems like I followed the same procedure both times. Also read up on async/await functionality and played around with the code a bit but had no luck. I'm at a loss on what else I can try, so any suggestions will be greatly appreciated.
You are blocking the UI thread by calling Result
.
One you go async, you must go async all the way.
public async Task ApplyImageFilter(Image userImage, int imageWidth, int imageHeight)
{
...
int[] src = await PixelUtils.bitmapToIntArray(userImage);
...
}
For more information on async-await
, read the articles on my curation.
这篇关于的Windows Phone 8.1 BitmapDe codeR异步/伺机而不是通过全功能加强的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!