以下是代码的外观。
void ApplyFilter()
{
this.filterOperationInProgress = true;
WriteableBitmap wBmp = new WriteableBitmap(0, 0);
wBmp = LocalWriteableBitmap.Clone();
NokiaFilters nf = new NokiaFilters(wBmp, FilterName.Mango);
wBmp = nf.render().Result;
this.filterOperationInProgress = false;
}
NokiaFilter Class:
class NokiaFilters
{
WriteableBitmap wb;
FilterName filtername = FilterName.NoFilter;
public NokiaFilters(WriteableBitmap wbm1, FilterName filtername1)
{
wb = wbm1;
filtername = filtername1;
}
public async Task<WriteableBitmap> render()
{
MemoryStream memstream = new MemoryStream(wb.ToByteArray());
if (memstream == null)
return wb;
//Mango
else if (filtername == FilterName.Mango)
{
memstream.Position = 0;
// Create a source to read the image from PhotoResult stream
using (var source = new StreamImageSource(memstream))
{
// Create effect collection with the source stream
using (var filters = new FilterEffect(source))
{
// Initialize the filter
ContrastFilter contrastfilter = new ContrastFilter(0.16);
HueSaturationFilter saturationfilter = new HueSaturationFilter(-0.01, -0.53);
LomoFilter lomofilter = new LomoFilter(0.5, 0.5, LomoVignetting.Low, LomoStyle.Yellow);
// Add the filter to the FilterEffect collection
filters.Filters = new List<IFilter> { contrastfilter, saturationfilter, lomofilter };
// Create a target where the filtered image will be rendered to
var target = new WriteableBitmap((int)wb.PixelWidth, (int)wb.PixelHeight);
// Create a new renderer which outputs WriteableBitmaps
using (var renderer = new WriteableBitmapRenderer( filters,target))
{
// Render the image with the filter(s)
try
{
target = await renderer.RenderAsync().AsTask().ConfigureAwait(false);
}
catch (System.InvalidOperationException inv)
{ }
catch { }
// Set the output image to Image control as a source
return target;
}
}
}
}
return wb;
}
}
我的用户界面完全卡住,甚至后退按钮都没有响应。当我停止调试器时,代码指向
wBmp = nf.render().Result;
在此之前开始执行的语句是
target = await renderer.RenderAsync().AsTask().ConfigureAwait(false);
我是并行编程的新手。我经常搜索,发现死锁在这种情况下很常见。我相信这也是一个僵局的情况。我怎么解决这个问题?
更新
更换后
wBmp = nf.render().Result;
带
wBmp = await nf.render().ConfigureAwait(false);
我得到了HRESULT:在
后的catch {}中出现0x8004C00F错误target = await renderer.RenderAsync().AsTask().ConfigureAwait(false);
这是stacktrace:
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Nokia.Graphics.Imaging.WriteableBitmapRenderer.<<RenderAsync>b__0>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at SomeApp.NokiaFilters.<render>d__f.MoveNext()
答案 0 :(得分:1)
解决了它。
替换
MemoryStream memstream = new MemoryStream(wb.ToByteArray());
与
MemoryStream memstream = new MemoryStream();
wb.SaveJpeg(memstream, wb.PixelWidth, wb.PixelHeight, 0, 100);
答案 1 :(得分:0)
您是否在没有ConfigureAwait(false);
的情况下尝试等待?是this.filterOperationInProgress
是否与UI绑定?如果是这样,可能会出现“更改UI”的情况,而不是在UI线程上。
编辑:wb
是否有效 - PixelWidth和PixelHeight是否大于或等于存储在MemoryStream
中的图片?