question

Jramos avatar image
0 Votes"
Jramos asked ·

how to print on inkCanvas on mvvm?

Hello: as I can print in mvv m with inkcanvas ? I have this code and I get a failure Object reference not set to an instance of an object.'

this code the PrinterHelper.cs {

     public  static PrintHelper _printHelper;
     
     private readonly InkCanvas _inkCanvas;
     private readonly Canvas _printCanvas;

     public PrinterHelper (InkCanvas inkCanvas,Canvas printCanvas)
     {
         _inkCanvas = inkCanvas;

         _printCanvas = printCanvas ;
     }


     //Craete Bitmap from strokes

     public async Task PrintHelper()
     {
         var inkStream = new InMemoryRandomAccessStream();
         await _inkCanvas.InkPresenter.StrokeContainer.SaveAsync(inkStream.GetOutputStreamAt(0));
         var inkBitmap = new BitmapImage();
         await inkBitmap.SetSourceAsync(inkStream);

         //you need to adjust to layout the image properly in the print-page.

         var inkBounds = _inkCanvas.InkPresenter.StrokeContainer.BoundingRect;
         var inkMargin = new Thickness(inkBounds.Left, inkBounds.Top, _inkCanvas.ActualWidth - inkBounds.Right, _inkCanvas.ActualHeight - inkBounds.Bottom);

         //prepare Viebox + image to be printed.

         var inkViewBox = new Viewbox()
         {
             Child = new Image()
             {
                 Source = inkBitmap,
                 Margin = inkMargin
             },

             Width = _inkCanvas.ActualWidth,
             Height = _inkCanvas.ActualHeight
         };


         _printHelper = new PrintHelper(_printCanvas);
        _printHelper.AddFrameworkElementToPrint(inkViewBox);

         var printHelperOptions = new PrintHelperOptions();

         printHelperOptions.AddDisplayOption(StandardPrintTaskOptions.Copies);
         printHelperOptions.AddDisplayOption(StandardPrintTaskOptions.MediaSize);
         printHelperOptions.AddDisplayOption(StandardPrintTaskOptions.Collation);
         printHelperOptions.AddDisplayOption(StandardPrintTaskOptions.Orientation);

         printHelperOptions.PrintQuality = PrintQuality.Default;
         printHelperOptions.MediaSize = PrintMediaSize.Default;
         printHelperOptions.Collation = PrintCollation.Default;
         printHelperOptions.Collation = PrintCollation.Default;
         printHelperOptions.Orientation = PrintOrientation.Landscape;
     }



     private async void PrintHelper_OnPrintSucceded()
     {
         _printHelper.Dispose();
         var dialog = new MessageDialog("Printing Done");
         await dialog.ShowAsync();
     }

     private async void PrintHelper_OnPrintFailed()
     {
         _printHelper.Dispose();
         var dialog = new MessageDialog("Printing Failed");
         await dialog.ShowAsync();
     }

     public  static  async Task ShowPrintUIAsync()
     {
         var printHelperOptions = new PrintHelperOptions();

         await _printHelper.ShowPrintUIAsync("Printing INKPen", printHelperOptions);
     }

 }

}

and buttton private async void PrintButton_Click(object sender, RoutedEventArgs e) { await Components.PrinterHelper.ShowPrintUIAsync(); }

uwp-xaml
· 6
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, what is the statement that caused the error? Can you provide a minimum runnable demo so that we can analyze your problem?

0 Votes 0 · ·

5862-captura-de-pantalla-12.png

0 Votes 0 · ·

Can you view the stack information? When an error occurs, select View Details, open $exception, and select StackTrace. The main purpose of this step is to discover what variables are null. A null value generally means that no element was created or assigned a value. You can check where the problem is based on the corresponding information.

0 Votes 0 · ·
Jramos avatar image Jramos RichardZhang-MSFT ·

Hello:

The global variable can be used in the document PritHelper.cs As would the global variable PrintHelperOptions

0 Votes 0 · ·
Show more comments

0 Answers