private void PrintButton_Click(object sender, RoutedEventArgs evt) { SetPrintButtonState(false); Connection printerConnection = null; Task.Run(() => { try { printerConnection = connectionSelector.GetConnection(); printerConnection.Open(); ZebraImageI image = ZebraImageFactory.GetImage(fileSelector.FileName); if (viewModel.ShouldStoreImage) { ZebraPrinterFactory.GetInstance(printerConnection).StoreImage(viewModel.StoredFileName, image, 540, 412); } ZebraPrinterFactory.GetInstance(printerConnection).PrintImage(image, 0, 0, 550, 412, false); } catch (ConnectionException e) { MessageBoxCreator.ShowError(e.Message, "Connection Error"); } catch (ZebraPrinterLanguageUnknownException e) { MessageBoxCreator.ShowError(e.Message, "Connection Error"); } catch (IOException e) { MessageBoxCreator.ShowError(e.Message, "Image Error"); } catch (ZebraIllegalArgumentException e) { MessageBoxCreator.ShowError(e.Message, "Illegal Arguments"); } catch (ArgumentException e) { MessageBoxCreator.ShowError(e.Message, "Invalid File Path"); } finally { try { if (printerConnection != null) { printerConnection.Close(); } } catch (ConnectionException) { } finally { SetPrintButtonState(true); } } }); }
/// <summary> /// Print content /// </summary> /// <returns>The print.</returns> /// <param name="content">Content.</param> public static PrintResult Print(string content, string address, string signature) { var result = new PrintResult { Status = PrintStatus.PrintFailed }; IConnection connection = null; try { connection = new BluetoothConnectionInsecure(address); connection.Open(); var printer = ZebraPrinterFactory.GetInstance(PrinterLanguage.Cpcl, connection); // if there is a signature store it if (signature != null) { using (var image = ZebraImageFactory.GetImage(signature)) { printer.StoreImage(SignatureFilename, image, image.Width / 4, image.Height / 4); } Thread.Sleep(SignaturePauseMilliseconds); } // write the label var printLabel = ConvertExtendedAscii(content); connection.SendAndWaitForResponse(printLabel, InitialiseResponseTimeoutMilliseconds, ResponseCompletionTimeoutMilliseconds, null); var status = printer.CurrentStatus; while (status.NumberOfFormatsInReceiveBuffer > 0 && status.IsReadyToPrint) { Thread.Sleep(500); status = printer.CurrentStatus; } if (!status.IsReadyToPrint) { LogPrinterFailure(result, status); } result.Status = PrintStatus.Success; printer.SendCommand("! U1 setvar \"formats.cancel_all\" \"\"" + "\r\n"); return(result); } catch (Exception exception) { var context = Application.Context; var message = context.Resources.GetString(Resource.String.message_print_failed_continue); Microsoft.AppCenter.Crashes.Crashes.TrackError(exception); result.Message = message; result.Status = PrintStatus.PrintException; return(result); } finally { connection.Close(); } }