0

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?)

SamD
  • 41
  • 6
  • I have figured out the reason for error but have no insight on how to solve it. The link that I am using for reference uses inkcanvas (a part of Windows.UI.Xaml.Controls.InkCanvas) and the inkcanvas that I am using is a part of SDK provided by Wacom(Wacom.UX.InkCanvas). – SamD Jul 25 '19 at 07:55
  • In Visual Studio check menu "Project > Manage Nuget Packages" and "Project > Properties". – Benl Jul 25 '19 at 09:02

2 Answers2

0

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);
AlwaysLearning
  • 7,915
  • 5
  • 27
  • 35
  • Didn't work. RenderTargetBitmap does not contain a constructor that takes 5 arguments. – SamD Jul 25 '19 at 07:52
  • I used: `public RenderTargetBitmap (int pixelWidth, int pixelHeight, double dpiX, double dpiY, System.Windows.Media.PixelFormat pixelFormat);`, ref: https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.imaging.rendertargetbitmap.-ctor – AlwaysLearning Jul 25 '19 at 08:51
0

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".

    1. Select "Installed" and uninstall the current 2d graphics rendering package, if any.
    2. 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;
Benl
  • 2,777
  • 9
  • 13