private void SaveTemplateImages(string imageDirectory) { List <string> existingTemplateImageFiles = zebraCardTemplate.GetTemplateImageNames(); List <FileInfo> imageFileInfoList = new DirectoryInfo(imageDirectory).EnumerateFiles().Where(f => SupportedImageExtensions.Contains(f.Extension.ToLower())).ToList(); foreach (FileInfo imageFileInfo in imageFileInfoList) { try { if (existingTemplateImageFiles.Contains(imageFileInfo.Name)) { zebraCardTemplate.DeleteTemplateImage(imageFileInfo.Name); } byte[] templateImageData = File.ReadAllBytes(imageFileInfo.FullName); zebraCardTemplate.SaveTemplateImage(imageFileInfo.Name, templateImageData); Application.Current.Dispatcher.Invoke(() => { JobStatusControl.UpdateLog($"Saving image file {imageFileInfo.Name}..."); }); } catch (Exception e) { string errorMessage = $"Error saving template image file {imageFileInfo.Name}: {e.Message}"; JobStatusControl.UpdateLog(errorMessage); MessageBoxHelper.ShowError(errorMessage); } } }
private async void StartJobButton_Click(object sender, RoutedEventArgs e) { int? jobId = null; CardSource?cardSource = null; JobStatusControl.ClearLog(); await printerManager.PerformAction("Starting smart card operation...", (zebraCardPrinter, connection) => { if (printerManager.IsPrinterReady(zebraCardPrinter, JobStatusControl)) { cardSource = (CardSource)Enum.Parse(typeof(CardSource), viewModel.SelectedSource); zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.CARD_SOURCE, cardSource.ToString()); zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.CARD_DESTINATION, viewModel.SelectedDestination.ToString()); bool isEncoderTypeContact = viewModel.SelectedEncoderType.Equals("contact", StringComparison.OrdinalIgnoreCase) || viewModel.SelectedEncoderType.Equals("contact_station", StringComparison.OrdinalIgnoreCase); string settingName = isEncoderTypeContact ? ZebraCardJobSettingNames.SMART_CARD_CONTACT : ZebraCardJobSettingNames.SMART_CARD_CONTACTLESS; string settingValue = isEncoderTypeContact ? "yes" : viewModel.SelectedEncoderType; zebraCardPrinter.SetJobSetting(settingName, settingValue); jobId = zebraCardPrinter.SmartCardEncode(1); } }, (exception) => { string errorMessage = $"Error sending smart card job: {exception.Message}"; MessageBoxHelper.ShowError(errorMessage); JobStatusControl.UpdateLog(errorMessage); }); if (jobId.HasValue && cardSource.HasValue) { await JobStatusControl.StartPolling(printerManager.Printer, new JobInfo(jobId.Value, cardSource.Value)); } }
private async void PrintButton_Click(object sender, RoutedEventArgs e) { int? jobId = null; CardSource?cardSource = null; JobStatusControl.ClearLog(); await printerManager.PerformAction("Sending print job to printer...", (zebraCardPrinter, connection) => { if (printerManager.IsPrinterReady(zebraCardPrinter, JobStatusControl)) { TemplateJob templateJob = CreateTemplateJob(); if (templateJob.JobInfo.CardDestination.HasValue) { if (templateJob.JobInfo.CardDestination.Value == CardDestination.Eject && zebraCardPrinter.HasLaminator()) { templateJob.JobInfo.CardDestination = CardDestination.LaminatorAny; } } cardSource = templateJob.JobInfo.CardSource; jobId = zebraCardPrinter.PrintTemplate(1, templateJob); } }, (exception) => { string errorMessage = $"Error printing card: {exception.Message}"; MessageBoxHelper.ShowError(errorMessage); JobStatusControl.UpdateLog(errorMessage); }); if (jobId.HasValue && cardSource.HasValue) { await JobStatusControl.StartPolling(printerManager.Printer, new JobInfo(jobId.Value, cardSource.Value)); } }
private async Task WriteMagEncodeData() { int? jobId = null; CardSource?cardSource = null; JobStatusControl.ClearLog(); await printerManager.PerformAction("Writing mag encode data...", (zebraCardPrinter, connection) => { if (printerManager.IsPrinterReady(zebraCardPrinter, JobStatusControl)) { cardSource = (CardSource)Enum.Parse(typeof(CardSource), viewModel.SelectedSource); Dictionary <string, string> jobSettings = new Dictionary <string, string>(); jobSettings.Add(ZebraCardJobSettingNames.CARD_SOURCE, cardSource.ToString()); jobSettings.Add(ZebraCardJobSettingNames.CARD_DESTINATION, viewModel.SelectedDestination.ToString()); jobSettings.Add(ZebraCardJobSettingNames.MAG_COERCIVITY, viewModel.SelectedCoercivityType.ToString()); jobSettings.Add(ZebraCardJobSettingNames.MAG_VERIFY, viewModel.VerifyEncoding ? "yes" : "no"); zebraCardPrinter.SetJobSettings(jobSettings); jobId = zebraCardPrinter.MagEncode(1, viewModel.Track1Data, viewModel.Track2Data, viewModel.Track3Data); } }, (exception) => { string errorMessage = $"Error writing mag encode data: {exception.Message}"; MessageBoxHelper.ShowError(errorMessage); JobStatusControl.UpdateLog(errorMessage); }); if (jobId.HasValue && cardSource.HasValue) { await JobStatusControl.StartPolling(printerManager.Printer, new JobInfo(jobId.Value, cardSource.Value)); } }
private async void CancelJobButton_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrWhiteSpace(viewModel.CancellableJobId)) { MessageBoxHelper.ShowError("No job ID specified"); return; } bool success = true; await printerManager.PerformAction($"Cancelling job ID {viewModel.CancellableJobId}...", (zebraCardPrinter, connection) => { zebraCardPrinter.Cancel(int.Parse(viewModel.CancellableJobId)); }, (exception) => { success = false; string errorMessage = $"Error cancelling job ID {viewModel.CancellableJobId}: {exception.Message}"; MessageBoxHelper.ShowError(errorMessage); JobStatusControl.UpdateLog(errorMessage); }, () => { if (success) { JobStatusControl.UpdateLog($"Cancel job ID {viewModel.CancellableJobId} command sent successfully."); } }); }
private byte[] CropImage(ZebraGraphics graphics, byte[] imageData, int croppedWidth, int croppedHeight) { int xOffset = viewModel.XOffset < 0 ? 0 : viewModel.XOffset; int yOffset = viewModel.YOffset < 0 ? 0 : viewModel.YOffset; JobStatusControl.UpdateLog($"Cropping image from xOffset:{xOffset} yOffset:{yOffset} with width:{croppedWidth} and height:{croppedHeight}..."); byte[] croppedImage = graphics.CropImage(imageData, xOffset, yOffset, croppedWidth, croppedHeight); JobStatusControl.UpdateLog("Finished cropping image"); return(croppedImage); }
private int ConstrainHeight(int height, int?maxHeight) { if (height < 1) { JobStatusControl.UpdateLog("Height must be positive. Setting height to 1..."); height = 1; } else if (maxHeight.HasValue && height > maxHeight.Value) { JobStatusControl.UpdateLog($"Specified height ({height}) is greater than the maximum height ({maxHeight.Value}). Setting height to maximum height..."); height = maxHeight.Value; } return(height); }
private int ConstrainWidth(int width, int?maxWidth) { if (width < 1) { JobStatusControl.UpdateLog("Width must be positive. Setting width to 1..."); width = 1; } else if (maxWidth.HasValue && width > maxWidth.Value) { JobStatusControl.UpdateLog($"Specified width ({width}) is greater than the maximum width ({maxWidth.Value}). Setting width to maximum width..."); width = maxWidth.Value; } return(width); }
private async void CancelAllButton_Click(object sender, RoutedEventArgs e) { bool success = true; await printerManager.PerformAction("Cancelling all jobs...", (zebraCardPrinter, connection) => { zebraCardPrinter.Cancel(0); }, (exception) => { success = false; string errorMessage = $"Error cancelling all jobs: {exception.Message}"; MessageBoxHelper.ShowError(errorMessage); JobStatusControl.UpdateLog(errorMessage); }, () => { if (success) { JobStatusControl.UpdateLog("Cancel all jobs command sent successfully."); } }); }
private void ApplyMonochromeConversion(ZebraGraphics graphics, PrintType printType, MonochromeConversion monochromeConversionType) { JobStatusControl.UpdateLog("Converting graphic..."); if (printType != PrintType.MonoK && printType != PrintType.GrayDye) { switch (monochromeConversionType) { case MonochromeConversion.Diffusion: JobStatusControl.UpdateLog("Ignoring diffusion option for non-mono/gray format type..."); break; case MonochromeConversion.HalfTone_6x6: case MonochromeConversion.HalfTone_8x8: JobStatusControl.UpdateLog("Ignoring halftone option for non-mono/gray format type..."); break; } } else { switch (monochromeConversionType) { case MonochromeConversion.Diffusion: graphics.MonochromeConverionType = MonochromeConversion.Diffusion; JobStatusControl.UpdateLog("Applying diffusion algorithm..."); break; case MonochromeConversion.HalfTone_6x6: graphics.MonochromeConverionType = MonochromeConversion.HalfTone_6x6; JobStatusControl.UpdateLog("Applying 6x6 halftone algorithm..."); break; case MonochromeConversion.HalfTone_8x8: graphics.MonochromeConverionType = MonochromeConversion.HalfTone_8x8; JobStatusControl.UpdateLog("Applying 8x8 halftone algorithm..."); break; } } }
private async void ConvertButton_Click(object sender, RoutedEventArgs e) { JobStatusControl.ClearLog(); await Task.Run(() => { try { using (ZebraGraphics graphics = new ZebraCardGraphics(null)) { graphics.PrinterModel = viewModel.SelectedPrinterModelInfo.PrinterModel; if (!Path.IsPathRooted(viewModel.OriginalGraphicFilename)) { throw new ArgumentException("Original graphic filename must be an absolute path"); } System.Drawing.Image image = ImageHelper.CreateImageFromFile(viewModel.OriginalGraphicFilename); byte[] imageData = ImageHelper.ConvertImage(image); int width; // Width of final output image int height; // Height of final output image switch (viewModel.SelectedDimensionOption) { case DimensionOption.Crop: int croppedWidth = ConstrainWidth(viewModel.Width, viewModel.SelectedPrinterModelInfo.MaxWidth); int croppedHeight = ConstrainHeight(viewModel.Height, viewModel.SelectedPrinterModelInfo.MaxHeight); imageData = CropImage(graphics, imageData, croppedWidth, croppedHeight); width = croppedWidth; height = croppedHeight; break; case DimensionOption.Resize: width = ConstrainWidth(viewModel.Width, viewModel.SelectedPrinterModelInfo.MaxWidth); height = ConstrainHeight(viewModel.Height, viewModel.SelectedPrinterModelInfo.MaxHeight); JobStatusControl.UpdateLog($"Resizing image to {width}x{height}..."); break; case DimensionOption.Original: default: width = ConstrainWidth(image.Width, viewModel.SelectedPrinterModelInfo.MaxWidth); height = ConstrainHeight(image.Height, viewModel.SelectedPrinterModelInfo.MaxHeight); JobStatusControl.UpdateLog("Keeping current image dimensions unless they exceed the maximum model-specific width and height..."); break; } GraphicsFormat graphicsFormat = viewModel.SelectedGraphicsFormat; MonochromeConversion monochromeConversionType = viewModel.SelectedGraphicsFormat.GetMonochromeConversion(); PrintType printType = viewModel.SelectedGraphicsFormat.GetPrintType(); OrientationType orientationType = OrientationType.Landscape; JobStatusControl.UpdateLog($"Setting orientation to {orientationType}..."); graphics.Initialize(width, height, orientationType, printType, System.Drawing.Color.White); graphics.DrawImage(imageData, 0, 0, width, height, RotationType.RotateNoneFlipNone); ApplyMonochromeConversion(graphics, printType, monochromeConversionType); JobStatusControl.UpdateLog($"Writing graphic file to path {viewModel.ConvertedGraphicFilename}..."); WriteToFile(viewModel.ConvertedGraphicFilename, graphics.CreateImage().ImageData); JobStatusControl.UpdateLog("Finished converting graphic"); } } catch (Exception exception) { string errorMessage = $"Error converting graphic: {exception.Message}"; JobStatusControl.UpdateLog(errorMessage); MessageBoxHelper.ShowError(errorMessage); } }); }
private async void PrintButton_Click(object sender, RoutedEventArgs e) { ZebraCardGraphics graphics = null; int? jobId = null; CardSource?cardSource = null; JobStatusControl.ClearLog(); await printerManager.PerformAction("Sending print job to printer...", (zebraCardPrinter, connection) => { if (printerManager.IsPrinterReady(zebraCardPrinter, JobStatusControl)) { graphics = new ZebraCardGraphics(zebraCardPrinter); List <GraphicsInfo> graphicsData = new List <GraphicsInfo>(); if (viewModel.PrintFrontSide) { byte[] frontSideGraphicData = ImageHelper.ConvertImage(ImageHelper.CreateImageFromFile(viewModel.FrontSideGraphicFilename)); graphics.Initialize(0, 0, OrientationType.Landscape, viewModel.FrontSidePrintType, System.Drawing.Color.White); graphics.DrawImage(frontSideGraphicData, 0, 0, 0, 0, RotationType.RotateNoneFlipNone); graphicsData.Add(BuildGraphicsInfo(graphics.CreateImage(), CardSide.Front, viewModel.FrontSidePrintType)); graphics.Clear(); } if (viewModel.PrintFrontSideOverlay) { if (viewModel.FrontSideOverlayGraphicFilename != null) { byte[] frontSideOverlayGraphicData = ImageHelper.ConvertImage(ImageHelper.CreateImageFromFile(viewModel.FrontSideOverlayGraphicFilename)); graphics.Initialize(0, 0, OrientationType.Landscape, PrintType.Overlay, System.Drawing.Color.White); graphics.DrawImage(frontSideOverlayGraphicData, 0, 0, 0, 0, RotationType.RotateNoneFlipNone); graphicsData.Add(BuildGraphicsInfo(graphics.CreateImage(), CardSide.Front, PrintType.Overlay)); graphics.Clear(); } else { graphicsData.Add(BuildGraphicsInfo(null, CardSide.Front, PrintType.Overlay)); graphics.Clear(); } } if (viewModel.PrintBackSide) { byte[] backSideGraphicData = ImageHelper.ConvertImage(ImageHelper.CreateImageFromFile(viewModel.BackSideGraphicFilename)); graphics.Initialize(0, 0, OrientationType.Landscape, PrintType.MonoK, System.Drawing.Color.White); graphics.DrawImage(backSideGraphicData, 0, 0, 0, 0, RotationType.RotateNoneFlipNone); graphicsData.Add(BuildGraphicsInfo(graphics.CreateImage(), CardSide.Back, PrintType.MonoK)); graphics.Clear(); } cardSource = (CardSource)Enum.Parse(typeof(CardSource), zebraCardPrinter.GetJobSettingValue(ZebraCardJobSettingNames.CARD_SOURCE)); jobId = zebraCardPrinter.Print(viewModel.Quantity, graphicsData); } }, (exception) => { string errorMessage = $"Error printing card: {exception.Message}"; MessageBoxHelper.ShowError(errorMessage); JobStatusControl.UpdateLog(errorMessage); }, () => { if (graphics != null) { graphics.Close(); } }); if (jobId.HasValue && cardSource.HasValue) { await JobStatusControl.StartPolling(printerManager.Printer, new JobInfo(jobId.Value, cardSource.Value)); } }
private async void RetrieveSettingsRanges() { viewModel.Sources.Clear(); viewModel.Destinations.Clear(); viewModel.EncoderTypes.Clear(); await printerManager.PerformAction("Retrieving settings ranges...", (zebraCardPrinter, connection) => { bool hasLaminator = zebraCardPrinter.HasLaminator(); if (zebraCardPrinter.HasSmartCardEncoder()) { string cardSourceRange = zebraCardPrinter.GetJobSettingRange(ZebraCardJobSettingNames.CARD_SOURCE); if (cardSourceRange != null) { foreach (CardSource source in Enum.GetValues(typeof(CardSource))) { if (cardSourceRange.Contains(source.ToString())) { Application.Current.Dispatcher.Invoke(() => { viewModel.Sources.Add(source.ToString()); }); } } } string cardDestinationRange = zebraCardPrinter.GetJobSettingRange(ZebraCardJobSettingNames.CARD_DESTINATION); if (cardDestinationRange != null) { foreach (CardDestination destination in Enum.GetValues(typeof(CardDestination))) { if (cardDestinationRange.Contains(destination.ToString())) { if (!destination.ToString().Contains("Laminator") || hasLaminator) { Application.Current.Dispatcher.Invoke(() => { viewModel.Destinations.Add(destination.ToString()); }); } } } } Dictionary <string, string> smartCardConfigurations = zebraCardPrinter.GetSmartCardConfigurations(); foreach (string encoderType in smartCardConfigurations.Keys) { Application.Current.Dispatcher.Invoke(() => { viewModel.EncoderTypes.Add(encoderType); }); } } else { MessageBoxHelper.ShowError("Unable to proceed with demo because no smart card encoder was found."); } }, (exception) => { string errorMessage = $"Error retrieving settings ranges: {exception.Message}"; MessageBoxHelper.ShowError(errorMessage); JobStatusControl.UpdateLog(errorMessage); Application.Current.Dispatcher.Invoke(() => { viewModel.Sources.Clear(); viewModel.Destinations.Clear(); viewModel.EncoderTypes.Clear(); }); }, null); }
private async void SendJobsButton_Click(object sender, RoutedEventArgs e) { JobStatusControl.ClearLog(); viewModel.IsSendJobsButtonEnabled = false; bool success = true; List <MultiJobControlViewModel> jobViewModels = new List <MultiJobControlViewModel> { SetSelectedFullOverlays(Job1Control.ViewModel), SetSelectedFullOverlays(Job2Control.ViewModel), SetSelectedFullOverlays(Job3Control.ViewModel), SetSelectedFullOverlays(Job4Control.ViewModel) }; jobViewModels = jobViewModels.Where((viewModel) => { if (IsJobValid(viewModel)) { return(true); } else { UpdateJobStatus(viewModel.JobNumber, "Not configured"); return(false); } }).ToList(); await printerManager.PerformAction("Sending jobs...", (zebraCardPrinter, connection) => { if (AreAnyJobsValid(jobViewModels)) { if (printerManager.IsPrinterReady(zebraCardPrinter, JobStatusControl)) { JobStatusControl.UpdateLog("Setting up jobs..."); foreach (MultiJobControlViewModel jobViewModel in jobViewModels) { SetUpAndSendJob(zebraCardPrinter, jobViewModel); } } else { success = false; } } else { throw new ZebraCardException("No jobs configured"); } }, (exception) => { success = false; string errorMessage = $"Error sending jobs: {exception.Message}"; MessageBoxHelper.ShowError(errorMessage); JobStatusControl.UpdateLog(errorMessage); }); if (success) { List <JobInfo> jobInfoList = new List <JobInfo>(); foreach (MultiJobControlViewModel jobViewModel in jobViewModels) { if (jobViewModel.JobId.HasValue) { CardSource cardSource = (CardSource)Enum.Parse(typeof(CardSource), jobViewModel.SelectedSource); jobInfoList.Add(new JobInfo(jobViewModel.JobNumber, jobViewModel.JobId.Value, cardSource)); } } await JobStatusControl.StartPolling(printerManager.Printer, jobInfoList); } viewModel.IsSendJobsButtonEnabled = true; }
private async void RetrieveSettingsRanges() { viewModel.Sources.Clear(); viewModel.Destinations.Clear(); viewModel.PrintOptimizations.Clear(); viewModel.PrintTypes.Clear(); viewModel.CoercivityTypes.Clear(); await printerManager.PerformAction("Retrieving settings ranges...", (zebraCardPrinter, connection) => { string installedRibbon = zebraCardPrinter.GetSettingValue(ZebraCardSettingNames.RIBBON_DESCRIPTION); if (!string.IsNullOrEmpty(installedRibbon)) { installedRibbon = installedRibbon.ToLower(); if (installedRibbon.Contains(ColorOption)) { Application.Current.Dispatcher.Invoke(() => { viewModel.PrintTypes.Add(PrintType.Color); }); } if (IsPrintTypeSupported(installedRibbon, MonoRibbonOptions)) { Application.Current.Dispatcher.Invoke(() => { viewModel.PrintTypes.Add(PrintType.MonoK); }); } if (IsPrintTypeSupported(installedRibbon, OverlayRibbonOptions)) { Application.Current.Dispatcher.Invoke(() => { viewModel.PrintTypes.Add(PrintType.Overlay); }); } string cardSourceRange = zebraCardPrinter.GetJobSettingRange(ZebraCardJobSettingNames.CARD_SOURCE); if (cardSourceRange != null) { foreach (CardSource source in Enum.GetValues(typeof(CardSource))) { if (cardSourceRange.Contains(source.ToString())) { Application.Current.Dispatcher.Invoke(() => { viewModel.Sources.Add(source.ToString()); }); } } } string cardDestinationRange = zebraCardPrinter.GetJobSettingRange(ZebraCardJobSettingNames.CARD_DESTINATION); if (cardDestinationRange != null) { foreach (CardDestination destination in Enum.GetValues(typeof(CardDestination))) { if (cardDestinationRange.Contains(destination.ToString())) { if (!destination.ToString().Contains("Laminator") || zebraCardPrinter.HasLaminator()) { Application.Current.Dispatcher.Invoke(() => { viewModel.Destinations.Add(destination.ToString()); }); } } } } bool hasMagneticEncoder = zebraCardPrinter.HasMagneticEncoder(); if (hasMagneticEncoder) { string coercivityTypeRange = zebraCardPrinter.GetJobSettingRange(ZebraCardJobSettingNames.MAG_COERCIVITY); if (coercivityTypeRange != null) { foreach (CoercivityType coercivityType in Enum.GetValues(typeof(CoercivityType))) { if (coercivityTypeRange.Contains(coercivityType.ToString())) { Application.Current.Dispatcher.Invoke(() => { viewModel.CoercivityTypes.Add(coercivityType.ToString()); }); } } } } Application.Current.Dispatcher.Invoke(() => { viewModel.IsPrintOptimizationAvailable = zebraCardPrinter.GetJobSettings().Contains(ZebraCardJobSettingNames.PRINT_OPTIMIZATION); viewModel.HasDualSidedPrintCapability = zebraCardPrinter.GetPrintCapability() == TransferType.DualSided; viewModel.HasMagneticEncoder = hasMagneticEncoder; }); } else { throw new ZebraCardException("No ribbon installed. Please install a ribbon and try again."); } }, (exception) => { string errorMessage = $"Error retrieving settings ranges: {exception.Message}"; MessageBoxHelper.ShowError(errorMessage); JobStatusControl.UpdateLog(errorMessage); Application.Current.Dispatcher.Invoke(() => { viewModel.Sources.Clear(); viewModel.Destinations.Clear(); viewModel.PrintOptimizations.Clear(); viewModel.PrintTypes.Clear(); viewModel.CoercivityTypes.Clear(); }); }, null); }