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 () =>
{
// find printer
Windows.Devices.PointOfService.PosPrinter posPrinter = await HelperPosPrinter.FindPosPrinterAsync();
ClaimedPosPrinter claimedPrinter = await posPrinter.ClaimPrinterAsync();
ReceiptPrintJob job = await HelperPosPrinter.PreparePrinter(claimedPrinter);
// print text
StringBuilder bodyToPrint = new StringBuilder();
bodyToPrint.AppendLine("Hello");
bodyToPrint.AppendLine("Thank you");
HelperPosPrinter.PrintText(job, bodyToPrint.ToString());
// print qr code
string myText = "https://dir.by/developer";
job.PrintBarcode(myText, BarcodeSymbologies.Qr, 250/*width*/, 250/*height*/, PosPrinterBarcodeTextPosition.None, PosPrinterAlignment.Center);
// finish
bool result = await HelperPosPrinter.FinishPrint(job, claimedPrinter);
});
}
}
}