2 Replies Latest reply on Apr 20, 2018 4:27 AM by Rajesh Mohan

    How to print image in CPCL from Xamarin forms - iOS Platform.

    Rajesh Mohan

      Hi,

       

      This is regarding printing of images to Zebra printer from Xamarin Forms in iOS platform.

       

      1. model of Zebra printer - Zebra ZQ520.

       

      2. Language used - CPCL

       

      I am creating a CPCL file with the data to be printed. The data is extracted from SQLite on iPhone and saved in the CPCL file. Finally giving the command to print the cpcl file works fine and the data is printed.

       

      My issue is how to embed the image in this cpcl file. The image is saved as a blob in SQLite database. I need to retrieve this blob and convert it to an image file and then specify the path of the image in the CPCL file. For some reason, the image never prints. Have gone through all the threads regarding printing of image but none of them work. I have even specified the language to use as CPCL.

       

      Can you please assist how to achieve the printing of the image in CPCL. I have the following queries.

       

      1. What format should the image be saved as on the device.

      2. What is the syntax to embed the image in the CPCL file.

      3. Is there any other way to embed the image in the CPCL file and print it along with the data.

       

      Thanks,

       

      Rajesh.

        • Re: How to print image in CPCL from Xamarin forms - iOS Platform.
          Robin West

          Hi Rajesh,

          The image should be a CGImageRef generated from a PNG, JPG, or BMP type of file.  You can then use the PrintImage() method to convert the image to the correct printer image type and print.  You can embed the image by setting InsideFormat in the parameters to True.  You will have to make sure the PrinterLanguage when you create the ZebraPrinter object is set to CPCL or the image conversion won't work.

          Does this make sense?

          • Re: How to print image in CPCL from Xamarin forms - iOS Platform.
            Rajesh Mohan

            Madam,

             

            I followed all the steps as explained by you along with some sample code in the Zebra Forums related to creating a CGImage.

             

            My code is as below.

             

            Code in the PCL project.

             

            Bluetooth Connectivity

             

            public void btn_signprint_clicked(Object sender, EventArgs e)
                    {
                        new Task(new Action(() => {
                            //This will make the call to the function private IFieldDescription[] PrintFormatOnPrinter(string fileNameOnPrinter, string[] mydata)
                            StartBluetoothDiscovery();
                        })).Start();
                    }

                    private void StartBluetoothDiscovery()
                    {

                        IDiscoveryEventHandler bthandler = DiscoveryHandlerFactory.Current.GetInstance();
                        bthandler.OnFoundPrinter += DiscoveryHandler_OnFoundPrinter;
                        connetionType = ConnectionType.Bluetooth;
                        System.Diagnostics.Debug.WriteLine("Starting Bluetooth Discovery");

                        DependencyService.Get<IPrinterDiscovery>().FindBluetoothPrinters(bthandler);
                    }

                    private void DiscoveryHandler_OnFoundPrinter(object sender, IDiscoveredPrinter discoveredPrinter)
                    {

                        //Debug.WriteLine("Found Printer:" + discoveredPrinter.ToString());
                        myPrinter = discoveredPrinter;

                        new Task(new Action(() => {

                            PrintSignature();
                            //StartCitationPrint();

                        })).Start();

                    }

                    public void PrintSignature()
                    {
                        IConnection connection = null;
                        try
                        {
                            connection = myPrinter.Connection;
                            connection.Open();

                            connection.MaxTimeoutForRead = 5000;

                            connection.TimeToWaitForMoreData = 1000;

                          
                            IZebraPrinter printer = ZebraPrinterFactory.Current.GetInstance(PrinterLanguage.CPCL, connection);
                            if ((!CheckPrinterLanguage(connection)) || (!PreCheckPrinterStatus(printer)))
                            {
                                resetPage();
                                return;
                            }

                            //GetSignaturePrint(connection);

                            DependencyService.Get<ISignature>().GetPrint(connection);

                            connection.Close();

                        }
                        catch (Exception ex)
                        {

                        }

                     }

             

            Code in the iOS project

             

            public CoreGraphics.CGImage GetImage(string filename)
                    {
                         CoreGraphics.CGImage image = null;


                             //creating the CGImage  (I found this code from this link Image Conversion Error on iOS printing a PNG file using IZebraPrinter.PrintImage )
                            image = new CoreGraphics.CGImage(500, 130, 1, 1, 400, CoreGraphics.CGColorSpace.CreateDeviceGray(), CoreGraphics.CGBitmapFlags.First, CoreGraphics.CGDataProvider.FromFile(filename), null, false, CoreGraphics.CGColorRenderingIntent.Default);

                            return image;
                    }


                    public void GetPrint(IConnection connection)
                    {
                        var folder = DependencyService.Get<ISignature>().GetFilePath();

                        var imgPath = folder + "/Signature.png";


                        try{
                            //first define the image in CGImage format
                            CoreGraphics.CGImage getImage = GetImage(imgPath);

             

                            //specify the language to CPCL   
                            IZebraPrinter printer = ZebraPrinterFactory.Current.GetInstance(PrinterLanguage.CPCL, connection);

             

                           //print the image
                            printer.PrintImage(getImage,120, 0, 500, 130, true);

                        }

                        catch(Exception ex)
                        {
                           
                        }
             

            }

             

            When executing this code it throws an error <Error>: CGImageCreate: invalid image bits/pixel: 1.

             

            Kindly let me know how to fix this error. Is the code correct in order to creating a CGImage from a PNG image so that it can be printed from iPhone to Zebra ZQ520.

             

            Thanks,

             

            Rajesh.