private int PrintAndMagEncode(ZebraCardPrinter zebraCardPrinter, MultiJobControlViewModel jobViewModel) { int jobId; ZebraCardGraphics graphics = null; try { graphics = new ZebraCardGraphics(zebraCardPrinter); List <GraphicsInfo> graphicsInfoList = new List <GraphicsInfo>(); if (jobViewModel.HasValidFrontSide) { foreach (GraphicsInfo graphicsInfo in CreateGraphicsInfo(graphics, jobViewModel.FrontSideGraphicFilenames, CardSide.Front)) { graphicsInfoList.Add(graphicsInfo); } } if (jobViewModel.HasValidBackSide) { foreach (GraphicsInfo graphicsInfo in CreateGraphicsInfo(graphics, jobViewModel.BackSideGraphicFilenames, CardSide.Back)) { graphicsInfoList.Add(graphicsInfo); } } if (jobViewModel.HasValidMagEncodeJob) { jobId = zebraCardPrinter.PrintAndMagEncode(jobViewModel.SelectedQuantity, graphicsInfoList, jobViewModel.Track1Data, jobViewModel.Track2Data, jobViewModel.Track3Data); } else { jobId = zebraCardPrinter.Print(jobViewModel.SelectedQuantity, graphicsInfoList); } } finally { if (graphics != null) { graphics.Close(); } } return(jobId); }
private async void ConvertButton_Click(object sender, RoutedEventArgs e) { JobStatusControl.ClearLog(); await Task.Run(() => { ZebraGraphics graphics = null; try { graphics = new ZebraCardGraphics(null) { 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); } finally { if (graphics != null) { graphics.Close(); } } }); }