コード例 #1
0
        private static void PrintImage(string address, int port)
        {
            Connection connection = new TcpConnection(address, port);

            try
            {
                connection.Open();
                ZebraPrinter printer = ZebraPrinterFactory.GetInstance(PrinterLanguage.ZPL, connection);

                int x = 0;
                int y = 0;
                printer.PrintImage("data/mooncake.png", x, y);
            }
            catch (ConnectionException e)
            {
                Console.WriteLine(e.ToString());
            }
            catch (ZebraPrinterLanguageUnknownException e)
            {
                Console.WriteLine(e.ToString());
            }
            catch (IOException e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                connection.Close();
            }
        }
        private async void PrintButton_Clicked(object sender, EventArgs eventArgs)
        {
            SetInputEnabled(false);

            Connection connection = null;

            try {
                await Task.Factory.StartNew(() => {
                    connection = CreateConnection();
                    connection.Open();

                    ZebraPrinter printer = ZebraPrinterFactory.GetInstance(connection);

                    if (printer.PrinterControlLanguage == PrinterLanguage.LINE_PRINT)
                    {
                        throw new ConnectionException("Operation cannot be performed with a printer set to line print mode");
                    }

                    string signaturePath = Path.Combine(LocalApplicationDataFolderPath, "signature.jpeg");
                    double printWidth    = double.Parse(SGD.GET("ezpl.print_width", connection));

                    if (File.Exists(signaturePath))
                    {
                        File.Delete(signaturePath);
                    }

                    using (Stream stream = SignaturePad.GetImageStream(printWidth)) {
                        using (BinaryReader br = new BinaryReader(stream)) {
                            br.BaseStream.Position = 0;
                            byte[] signatureBytes  = br.ReadBytes((int)stream.Length);
                            File.WriteAllBytes(signaturePath, signatureBytes);
                        }
                    }

                    ZebraImageI image = ZebraImageFactory.Current.GetImage(signaturePath);
                    printer.PrintImage(image, 0, 0, image.Width, image.Height, false);
                });
            } catch (Exception e) {
                await DisplayAlert("Error", e.Message, "OK");
            } finally {
                try {
                    connection?.Close();
                } catch (ConnectionException) { }

                SetInputEnabled(true);
            }
        }