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);
zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.MAG_ENCODING_TYPE, "ISO");
zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.MAG_COERCIVITY, "High");
zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.MAG_VERIFY, "yes");
int jobId = zebraCardPrinter.PrintAndMagEncode(1, graphicsData, "Zebra Technologies", "2222222222", "3333333333");
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);
}
}
+Graphics- #region Graphics
private static List<GraphicsInfo> DrawGraphics(ZebraCardPrinter zebraCardPrinter) {
ZebraCardImageI zebraCardImage = null;
List<GraphicsInfo> graphicsData = new List<GraphicsInfo>();
using (ZebraGraphics graphics = new ZebraCardGraphics(zebraCardPrinter)) {
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();
graphicsData.Add(AddImage(CardSide.Front, PrintType.Overlay, 0, 0, 1, null));
}
return graphicsData;
}
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
+JobStatus- #region JobStatus
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
+CleanUp- #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
}