Here is how the Code looks.
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;
}
}
My UI completely stucks and even the back button not responding. When i stop the debugger, the code is pointed at
wBmp = nf.render().Result;
The statement that started executing just before this happens is
target = await renderer.RenderAsync().AsTask().ConfigureAwait(false);
I am new to parallel programming. I have searched a lot and found that deadlock is very common in such scenario. I believe this is also a Deadlock situation. How can i resolve this?
Update:
After replacing
wBmp = nf.render().Result;
with
wBmp = await nf.render().ConfigureAwait(false);
i am getting HRESULT: 0x8004C00F error in catch {} after
target = await renderer.RenderAsync().AsTask().ConfigureAwait(false);
Here is the 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()
Solved it.
replaced
MemoryStream memstream = new MemoryStream(wb.ToByteArray());
with
MemoryStream memstream = new MemoryStream();
wb.SaveJpeg(memstream, wb.PixelWidth, wb.PixelHeight, 0, 100);