问题
I am working on an application that captures real time pen strokes on a canvas using Wacom Bamboo Slate. The application is being developed for UWP platform using C#. After drawing on the canvas, save feature is to be implemented. I am using this for my reference. Below is the code and error message:
private async void BtnSave_Click(object sender, RoutedEventArgs e)
{
StorageFolder storageFolder = KnownFolders.SavedPictures;
var file = await storageFolder.CreateFileAsync("sample.jpg", CreationCollisionOption.ReplaceExisting);
CanvasDevice device = CanvasDevice.GetSharedDevice();
CanvasRenderTarget renderTarget = new CanvasRenderTarget(device, (int)inkCanvas.ActualWidth, (int)inkCanvas.ActualHeight, 96);
using (var ds = renderTarget.CreateDrawingSession())
{
ds.Clear(Colors.White);
ds.DrawInk(inkCanvas.InkPresenter.StrokeContainer.GetStrokes());
}
using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
await renderTarget.SaveAsync(fileStream, CanvasBitmapFileFormat.Jpeg, 1f);
}
}
CS1061 'InkCanvas' does not contain a definition for InkPresenter and no accessible extension method InkPresenter accepting a first argument of type InkCanvas could be found (are you missing a using directive or an assembly reference?)
回答1:
Have you considered:
RenderTargetBitmap rtb = new RenderTargetBitmap((int)inkCanvas.Width, (int)inkCanvas.Height, 96d, 96d, PixelFormats.Default);
rtb.Render(inkCanvas);
After which you can then:
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
encoder.Save(fileStream);
回答2:
The sample referred above can be found here.
The sample use CanvasDevice
from the Microsoft.Graphics.Canvas
namespace part of the package Win2D.UWP
(version 1.6.0) from Microsoft. The UWP project sample targets build 10240 (minimum 10240) of Windows 10.
The package Win2D.UWP
can be installed
- using the menu "Project > Manage Nuget Packages", or
by selecting the context menu "References" in the UWP project of "Solution Explorer".
- Select "Installed" and uninstall the current 2d graphics rendering package, if any.
- Select "Browse", look for
Win2D.UWP
and install the one from Microsoft.
Please note that the latest version of Win2D.UWP
updated 5/17/2018 version 1.23.0 requires target platform to be 17134.
For example, "Error List" might show the following error message after a build with version 1.23 of Win2D.UWP
and target version set to 10240 in the UWP project properties:
This version of Win2D requires Windows SDK >= 10.0.17134.0, but TargetPlatformVersion is 10.0.10240.0.
Target version can be changed in the UWP project properties
- select menu "Project > projectname Properties", or
- by selecting the context menu "Properties" from the UWP project in "Solution Explorer".
PS: Add the following after InitializeComponent();
in MainPage.xaml.cs
to enable drawing with a selection of input device types:
MyInkCanvas.InkPresenter.InputDeviceTypes = CoreInputDeviceTypes.Mouse | CoreInputDeviceTypes.Pen | CoreInputDeviceTypes.Touch;
来源:https://stackoverflow.com/questions/57196105/how-to-fix-this-missing-assembly-reference-error