【发布时间】:2015-06-03 13:21:56
【问题描述】:
我尝试将 InkCanvas 的笔画渲染到 Windows 10 通用应用程序中的 RenderTargetBitmap。这是我的 xaml 代码:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="10" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid x:Name="container">
<Rectangle Fill="LightBlue" />
<InkCanvas x:Name="InkCanvas" />
</Grid>
<Image Grid.Row="2" x:Name="TheImage" />
<Button Grid.Row="3" Content="CopyToRendertargt" Click="Button_Click" />
</Grid>
这是我设置 Image.Source 属性的代码:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
InkCanvas.InkPresenter.InputDeviceTypes = Windows.UI.Core.CoreInputDeviceTypes.Mouse;
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
RenderTargetBitmap renderTarget = new RenderTargetBitmap();
await renderTarget.RenderAsync(this.container);
this.TheImage.Source = renderTarget;
}
}
当我单击按钮时,我在 InkCanvas 上所做的笔划消失并且 InkCanvas 被冻结,直到我调整应用程序窗口的大小。笔画未呈现到 RenderTargetBitmap。该图像仅显示 LightBlue 矩形。
有人对此有解决方案吗?
** 更新 **
对于那些正在寻找在 uwp 上将笔划保存到位图的正确方法的人。我找到了将 InkCanvas 笔划保存到位图的 UWP 方式:InkStrokeContainer 对象有一个名为 SaveAsync(...) 的方法,它将笔划保存到流中。如果您将此流用作位图的源,您将获得作为位图的笔划。
InkStrokeContainer container = TheInkCanvas.InkPresenter.StrokeContainer;
WriteableBitmap bmp;
using (InMemoryRandomAccessStream ims =
new InMemoryRandomAccessStream())
{
await container.SaveAsync(ims);
bmp = await new WriteableBitmap(1, 1)
.FromStream(ims, BitmapPixelFormat.Bgra8);
}
*注意:本示例代码中使用了 WriteableBitmapEx (https://www.nuget.org/packages/WriteableBitmapEx/)
【问题讨论】: