RenderTargetBitmap.RenderAsync参数异常

时间:2016-09-09 10:00:24

标签: c# windows-runtime winrt-xaml

我使用以下代码呈现UIElement

var bitmap = new RenderTargetBitmap();
await bitmap.RenderAsync(elementToRender); //this line throws the exception

var pixelBuffer = await bitmap.GetPixelsAsync();

var wb = new WriteableBitmap(1, 1);
wb = await wb.FromPixelBuffer(pixelBuffer, bitmap.PixelWidth, bitmap.PixelHeight);

var stream = await wb.GetStream();

这是我的xaml:

<Grid x:Name="elementToRender" Background="White" Width="400" Height="400">
    <Ellipse Fill="Black" />
</Grid>

我在加载页面后调用此方法,但如果我尝试使用此代码,我会得到一个异常,这就是stacktrace:

  System.ArgumentException 
  HResult=-2147024809
  Message=Value does not fall within the expected range.
  Source=mscorlib
  StackTrace:
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.GetResult()

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

问题是包含“elementToRender”的xaml元素将其自己的可见性设置为Collapsed