Created
April 11, 2019 00:56
-
-
Save ridercz/ec91d76247676b85a3e5ac3303537d28 to your computer and use it in GitHub Desktop.
Card printer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Drawing; | |
using System.Drawing.Printing; | |
using System.IO; | |
using System.Linq; | |
using System.Threading; | |
namespace CardPrinter { | |
class Program { | |
private const string FOLDER_PATH = @"D:\Downloads\badges\Avers"; | |
private const string FILE_MASK = "*.jpg"; | |
private const string PRINTER_NAME = "Zebra ZXP Series 3 USB Card Printer"; | |
private const string PAPER_SIZE = "86mmx54mm Card"; | |
private const bool REVERSE_ORDER = false; | |
private const int WAIT_SECONDS = 20; | |
static void Main(string[] args) { | |
// Get all files in folder | |
var dir = new DirectoryInfo(FOLDER_PATH); | |
var files = dir.GetFiles(FILE_MASK).Select(x => x.FullName).ToArray(); | |
#pragma warning disable CS0162 // Unreachable code detected | |
if (REVERSE_ORDER) Array.Reverse(files); | |
#pragma warning restore CS0162 // Unreachable code detected | |
Console.WriteLine($"Found {files.Length} files to print"); | |
Console.WriteLine("Press ENTER to start"); | |
Console.ReadLine(); | |
// Print the files | |
for (var i = 0; i < files.Length; i++) { | |
var fileName = Path.GetFileName(files[i]); | |
Console.Write($"Printing file {fileName} ({i+1}/{files.Length})..."); | |
var doc = new PrintDocument() { | |
DocumentName = fileName, | |
PrinterSettings = new PrinterSettings { | |
PrinterName = PRINTER_NAME | |
} | |
}; | |
doc.DefaultPageSettings.PaperSize = doc.PrinterSettings.PaperSizes.OfType<PaperSize>().Single(x => x.PaperName.Equals(PAPER_SIZE)); | |
doc.PrintPage += (s, e) => { | |
var image = Image.FromFile(files[i]); | |
var g = e.Graphics; | |
g.DrawImage(image, e.PageBounds); | |
}; | |
doc.Print(); | |
Console.WriteLine("OK"); | |
Thread.Sleep(TimeSpan.FromSeconds(WAIT_SECONDS)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment