Skip to content

Instantly share code, notes, and snippets.

@ridercz
Created April 11, 2019 00:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ridercz/ec91d76247676b85a3e5ac3303537d28 to your computer and use it in GitHub Desktop.
Save ridercz/ec91d76247676b85a3e5ac3303537d28 to your computer and use it in GitHub Desktop.
Card printer
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