将 InkStroke 的 BoundingRect 移动到 0;0
Move BoundingRect of InkStroke to 0;0
如何将 BoundingRect
移动到坐标:X=0;Y=0?
我需要将用户在 InkCanvas
上绘制的每个 InkStroke
作为图像单独保存在 Windows 通用应用程序中。
为了保存最终图像,我使用的是 Win2D,但这只是将 Canvas 从 <0,0>
裁剪为 <BoundingRectHeight,BoundingRectWidth>
...
private async Task SaveToImage(string id, InkStroke stroke, float width, float height)
{
CanvasDevice device = CanvasDevice.GetSharedDevice();
CanvasRenderTarget offscreen = new CanvasRenderTarget(device, width, height, 96);
using (CanvasDrawingSession ds = offscreen.CreateDrawingSession())
{
ds.Clear(Colors.Transparent);
ds.DrawInk(new InkStroke[] { stroke });
}
StorageFile f = await ApplicationData.Current.LocalFolder.CreateFileAsync(id + ".png", CreationCollisionOption.ReplaceExisting);
using (IRandomAccessStream output = await f.OpenAsync(FileAccessMode.ReadWrite))
{
await offscreen.SaveAsync(output, CanvasBitmapFileFormat.Png);
await output.FlushAsync();
}
}
我意识到这种做法可能是错误的。
所以,我决定这样做:
- 通过 Win2D
将给定笔划绘制到屏幕外 canvas
- Offscreen canvas 保存到 InMememoryRandomAccessStream
- 将给定的流裁剪成 WritableBitmap
- 打开 read/write
的存储文件
- 创建 BitmapEncoder 并从 WriteableBitmap 设置像素数据
您可以在我的 GitHub
中找到完整示例
如何将 BoundingRect
移动到坐标:X=0;Y=0?
我需要将用户在 InkCanvas
上绘制的每个 InkStroke
作为图像单独保存在 Windows 通用应用程序中。
为了保存最终图像,我使用的是 Win2D,但这只是将 Canvas 从 <0,0>
裁剪为 <BoundingRectHeight,BoundingRectWidth>
...
private async Task SaveToImage(string id, InkStroke stroke, float width, float height)
{
CanvasDevice device = CanvasDevice.GetSharedDevice();
CanvasRenderTarget offscreen = new CanvasRenderTarget(device, width, height, 96);
using (CanvasDrawingSession ds = offscreen.CreateDrawingSession())
{
ds.Clear(Colors.Transparent);
ds.DrawInk(new InkStroke[] { stroke });
}
StorageFile f = await ApplicationData.Current.LocalFolder.CreateFileAsync(id + ".png", CreationCollisionOption.ReplaceExisting);
using (IRandomAccessStream output = await f.OpenAsync(FileAccessMode.ReadWrite))
{
await offscreen.SaveAsync(output, CanvasBitmapFileFormat.Png);
await output.FlushAsync();
}
}
我意识到这种做法可能是错误的。
所以,我决定这样做:
- 通过 Win2D 将给定笔划绘制到屏幕外 canvas
- Offscreen canvas 保存到 InMememoryRandomAccessStream
- 将给定的流裁剪成 WritableBitmap
- 打开 read/write 的存储文件
- 创建 BitmapEncoder 并从 WriteableBitmap 设置像素数据
您可以在我的 GitHub
中找到完整示例