Click or drag to resize

JobUtilPrintAndMagEncode Method

Writes the magnetic encoding data and prints a card.

Namespace:  Zebra.Sdk.Card.Job
Assembly:  SdkApi_Card_Core (in SdkApi_Card_Core.dll) Version: 2.14.1989
Syntax
int PrintAndMagEncode(
	int copies,
	List<GraphicsInfo> graphicsData,
	string track1Data,
	string track2Data,
	string track3Data
)

Parameters

copies
Type: SystemInt32
The number of copies to be printed.
graphicsData
Type: System.Collections.GenericListGraphicsInfo
A List containing details of the images to be printed.
track1Data
Type: SystemString
Data to be encoded on track 1.
track2Data
Type: SystemString
Data to be encoded on track 2.
track3Data
Type: SystemString
Data to be encoded on track 3.

Return Value

Type: Int32
The assigned job ID number.
Exceptions
ExceptionCondition
ConnectionExceptionIf the device is busy or there is an error communicating with the printer.
SettingsExceptionIf the job settings are not valid.
ZebraCardExceptionIf a printer error occurs or if the specified arguments are invalid.
ZebraIllegalArgumentExceptionIf any of the supplied arguments are invalid or an error occurs while building the job.
Examples
Demonstrates how to encode and print to a magnetic card.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Threading;
using Zebra.Sdk.Card.Containers;
using Zebra.Sdk.Card.Enumerations;
using Zebra.Sdk.Card.Graphics;
using Zebra.Sdk.Card.Graphics.Enumerations;
using Zebra.Sdk.Card.Job;
using Zebra.Sdk.Card.Printer;
using Zebra.Sdk.Comm;

public class MagneticEncodeAndPrintExample {

    private const int CARD_FEED_TIMEOUT = 30000;

    public static void Main(string[] args) {
        Connection connection = null;
        ZebraCardPrinter zebraCardPrinter = null;

        try {
            connection = new TcpConnection("1.2.3.4", 9100);
            connection.Open();

            zebraCardPrinter = ZebraCardPrinterFactory.GetInstance(connection);

            if (zebraCardPrinter.HasMagneticEncoder()) {
                List<GraphicsInfo> graphicsData = DrawGraphics(zebraCardPrinter);

                // Configure magnetic encoding settings
                zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.MAG_ENCODING_TYPE, "ISO");  // ISO=default
                zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.MAG_COERCIVITY, "High");    // High=default
                zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.MAG_VERIFY, "yes");         // yes=default

                // Send job
                int jobId = zebraCardPrinter.PrintAndMagEncode(1, graphicsData, "Zebra Technologies", "2222222222", "3333333333");

                // Poll job status
                JobStatusInfo jobStatus = PollJobStatus(jobId, zebraCardPrinter);
                Console.WriteLine($"Job {jobId} completed with status '{jobStatus.PrintStatus}'.");
            } else {
                Console.WriteLine("No magnetic encoder installed.");
            }
        } catch (Exception e) {
            Console.WriteLine($"Error printing and encoding card: {e.Message}");
        } finally {
            CloseQuietly(connection, zebraCardPrinter);
        }
    }

-    #region Graphics
     /// <exception cref="ConnectionException"></exception>
     /// <exception cref="IOException"></exception>
     /// <exception cref="NotSupportedException"></exception>
     /// <exception cref="System.Security.SecurityException"></exception>
     /// <exception cref="UnauthorizedAccessException"></exception>
     /// <exception cref="Zebra.Sdk.Card.Exceptions.ZebraCardException"></exception>
     /// <exception cref="Zebra.Sdk.Device.ZebraIllegalArgumentException"></exception>
     private static List<GraphicsInfo> DrawGraphics(ZebraCardPrinter zebraCardPrinter) {
         // Generate image data
         ZebraCardImageI zebraCardImage = null;
         List<GraphicsInfo> graphicsData = new List<GraphicsInfo>();
 
         using (ZebraGraphics graphics = new ZebraCardGraphics(zebraCardPrinter)) {
             // Front side color
             string colorImagePath = @"path\to\myColorImage.bmp";
             byte[] imageData = File.ReadAllBytes(colorImagePath);
 
             zebraCardImage = DrawImage(graphics, PrintType.Color, imageData, 0, 0, 0, 0);
             graphicsData.Add(AddImage(CardSide.Front, PrintType.Color, 0, 0, -1, zebraCardImage));
             graphics.Clear();
 
             // Front side full overlay
             graphicsData.Add(AddImage(CardSide.Front, PrintType.Overlay, 0, 0, 1, null));
         }
         return graphicsData;
     }
 
     /// <exception cref="Zebra.Sdk.Device.ZebraIllegalArgumentException"></exception>
     /// <exception cref="Zebra.Sdk.Card.Exceptions.ZebraCardException"></exception>
     private static ZebraCardImageI DrawImage(ZebraGraphics graphics, PrintType printType, byte[] imageData, int xOffset, int yOffset, int width, int height) {
         graphics.Initialize(0, 0, OrientationType.Landscape, printType, Color.White);
         graphics.DrawImage(imageData, xOffset, yOffset, width, height, RotationType.RotateNoneFlipNone);
         return graphics.CreateImage();
     }
 
     private static GraphicsInfo AddImage(CardSide side, PrintType printType, int xOffset, int yOffset, int fillColor, ZebraCardImageI zebraCardImage) {
         return new GraphicsInfo {
             Side = side,
             PrintType = printType,
             GraphicType = zebraCardImage != null ? GraphicType.BMP : GraphicType.NA,
             XOffset = xOffset,
             YOffset = yOffset,
             FillColor = fillColor,
             Opacity = 0,
             Overprint = false,
             GraphicData = zebraCardImage ?? null
         };
     }
     #endregion Graphics

-    #region JobStatus
     /// <exception cref="ArgumentException"></exception>
     /// <exception cref="ConnectionException"></exception>
     /// <exception cref="IOException"></exception>
     /// <exception cref="OverflowException"></exception>
     /// <exception cref="Zebra.Sdk.Settings.SettingsException"></exception>
     /// <exception cref="Zebra.Sdk.Card.Exceptions.ZebraCardException"></exception>
     private static JobStatusInfo PollJobStatus(int jobId, ZebraCardPrinter zebraCardPrinter) {
         JobStatusInfo jobStatusInfo = new JobStatusInfo();
         bool isFeeding = false;
 
         long start = Math.Abs(Environment.TickCount);
         while (true) {
             jobStatusInfo = zebraCardPrinter.GetJobStatus(jobId);
 
             if (!isFeeding) {
                 start = Math.Abs(Environment.TickCount);
             }
 
             isFeeding = jobStatusInfo.CardPosition.Contains("feeding");
 
             string alarmDesc = jobStatusInfo.AlarmInfo.Value > 0 ? $" ({jobStatusInfo.AlarmInfo.Description})" : "";
             string errorDesc = jobStatusInfo.ErrorInfo.Value > 0 ? $" ({jobStatusInfo.ErrorInfo.Description})" : "";
 
             Console.WriteLine($"Job {jobId}: status:{jobStatusInfo.PrintStatus}, position:{jobStatusInfo.CardPosition}, mag:{jobStatusInfo.MagneticEncodingStatus}, alarm:{jobStatusInfo.AlarmInfo.Value}{alarmDesc}, error:{jobStatusInfo.ErrorInfo.Value}{errorDesc}");
 
             if (jobStatusInfo.PrintStatus.Contains("done_ok")) {
                 break;
             } else if (jobStatusInfo.PrintStatus.Contains("error") || jobStatusInfo.PrintStatus.Contains("cancelled")) {
                 Console.WriteLine($"The job encountered an error [{jobStatusInfo.ErrorInfo.Description}] and was cancelled.");
                 break;
             } else if (jobStatusInfo.ErrorInfo.Value > 0) {
                 Console.WriteLine($"The job encountered an error [{jobStatusInfo.ErrorInfo.Description}] and was cancelled.");
                 zebraCardPrinter.Cancel(jobId);
             } else if (jobStatusInfo.PrintStatus.Contains("in_progress") && isFeeding) {
                 if (Math.Abs(Environment.TickCount) > start + CARD_FEED_TIMEOUT) {
                     Console.WriteLine("The job timed out waiting for a card and was cancelled.");
                     zebraCardPrinter.Cancel(jobId);
                 }
             }
 
             Thread.Sleep(1000);
         }
         return jobStatusInfo;
     }
     #endregion JobStatus

-    #region CleanUp
     private static void CloseQuietly(Connection connection, ZebraCardPrinter zebraCardPrinter) {
         try {
             if (zebraCardPrinter != null) {
                 zebraCardPrinter.Destroy();
             }
         } catch { }
 
         try {
             if (connection != null) {
                 connection.Close();
             }
         } catch { }
     }
     #endregion CleanUp
}
See Also