how to print on inkCanvas on mvvm?
Javier R
211
Reputation points
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(); }
Sign in to answer