C#
using System;
using System.Linq;
using System.Threading.Tasks;
using Windows.Devices.Enumeration;
using Windows.Devices.PointOfService;
using Windows.Graphics.Imaging;
using Windows.Storage.Streams;
using Windows.Storage;
namespace AppUWP1
{
public class HelperPosPrinter
{
public static async Task<BitmapFrame> ConvertFileToBitmap(StorageFolder storageFolder, string filename)
{
try
{
StorageFile file = await storageFolder.GetFileAsync(filename);
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read))
{
BitmapDecoder bitmapDecoder = await BitmapDecoder.CreateAsync(stream);
return await bitmapDecoder.GetFrameAsync(0);
}
}
catch (Exception ex)
{
}
return null;
}
public static async Task<Windows.Devices.PointOfService.PosPrinter> FindPosPrinterAsync()
{
try
{
string posAsql = Windows.Devices.PointOfService.PosPrinter.GetDeviceSelector();
DeviceInformationCollection deviceCollection = await DeviceInformation.FindAllAsync(posAsql);
int count = deviceCollection.Count();
foreach (var item in deviceCollection)
{
var printer = await Windows.Devices.PointOfService.PosPrinter.FromIdAsync(item.Id);
if (printer != null)
return printer;
}
}
catch (Exception)
{
}
return null;
}
public static async Task<ReceiptPrintJob> PreparePrinter(ClaimedPosPrinter claimedPrinter)
{
await claimedPrinter.EnableAsync();
claimedPrinter.Receipt.CharactersPerLine = 56;
claimedPrinter.Receipt.LineSpacing = 20;
claimedPrinter.Receipt.IsLetterQuality = true;
return claimedPrinter.Receipt.CreateJob();
}
public static void PrintImage(ReceiptPrintJob job, BitmapFrame bitmapFrame, PosPrinterAlignment align, uint maxWidth)
{
try
{
job.PrintBitmap(bitmapFrame, align);
}
catch (Exception ex)
{
}
}
public static void PrintText(ReceiptPrintJob job, string textToPrint)
{
job.Print(textToPrint);
}
public static async Task<bool> FinishPrint(ReceiptPrintJob job, ClaimedPosPrinter claimedPrinter)
{
try
{
bool result = await job.ExecuteAsync();
claimedPrinter.Dispose();
return result;
}
catch (Exception e)
{
}
return false;
}
}
}
C#
using System;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.Devices.PointOfService;
namespace AppUWP1
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.Loaded += MainPage_Loaded;
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
Task.Run(async () =>
{
// load bitmap
var bitmap = await HelperPosPrinter.ConvertFileToBitmap(Windows.Storage.ApplicationData.Current.LocalFolder, "my_for_pos_printer.jpg");
// find printer
Windows.Devices.PointOfService.PosPrinter posPrinter = await HelperPosPrinter.FindPosPrinterAsync();
ClaimedPosPrinter claimedPrinter = await posPrinter.ClaimPrinterAsync();
ReceiptPrintJob job = await HelperPosPrinter.PreparePrinter(claimedPrinter);
// print image
uint maxWidth = claimedPrinter.Receipt.LineWidth;
HelperPosPrinter.PrintImage(job, bitmap, PosPrinterAlignment.Center, maxWidth);
// print text
StringBuilder bodyToPrint = new StringBuilder();
bodyToPrint.AppendLine("Hello");
bodyToPrint.AppendLine("Thank you");
HelperPosPrinter.PrintText(job, bodyToPrint.ToString());
// finish
bool result = await HelperPosPrinter.FinishPrint(job, claimedPrinter);
});
}
}
}
Note 1
The picture
my_for_pos_printer.jpg ... should be put in the folder
Windows.Storage.ApplicationData.Current.LocalFolder
I have this folder:
C:\Users\evgen\AppData\Local\Packages\77843154-c68b-4049-8c8f-ef5b18a64f73_92754ewph3nsr\LocalState\
Note 2
In order for the picture to be printed on the printer, the width of the picture must be less than the width of the printer's paper.
Width of my picture = 370 pixels
On my
EPSON printer, paper width = 570 pixels (
claimedPrinter.Receipt.LineWidth parameter)