dir.by  
  Search  
Programming, development, testing
UWP (Universal Windows Platform) C# app for Windows
Write a program in UWP C# that prints an image of any size on a receipt EPSON POS printer (during execution, change the width of the picture = the width of the paper)
  Looked at 308 times    
 Write a program in UWP C# that prints a picture of any size on a receipt EPSON POS printer (stretch the picture to the full width of the paper) 
last updated: 12 October 2025
Download an example:
AppUWP1_PosPrinterImageResize.zip ...
size: 25 kb
Step 1. Let's open C# UWP an application that prints text and an image without stretching on a receipt printer
Step 2. In the MainPage.xaml.cs file, write a code to stretch the image on a receipt printer
  C#  
using System;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.Devices.PointOfService;
using Windows.Storage;

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 bitmapStream = await HelperPosPrinterImageResize.ConvertFileToStream(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;
                    await HelperPosPrinterImageResize.PrintImageWithResize(job, bitmapStream, 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);
               });
          }
     }
}
Step 3. Added a new file HelperPosPrinterImageResize.cs my useful functions for resizing the picture
  C#  
using System;
using System.IO;
using System.Threading.Tasks;
using Windows.Devices.PointOfService;
using Windows.Graphics.Imaging;
using Windows.Storage.Streams;
using Windows.Storage;

namespace AppUWP1
{
     public class HelperPosPrinterImageResize
     {
          internal static async Task<IRandomAccessStream> ConvertImageBytesToStream(byte[] imageBytes)
          {
               if (imageBytes == null)
                    return null;
               InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();

               Stream stream = randomAccessStream.AsStream();

               await stream.WriteAsync(imageBytes, 0, imageBytes.Length);
               await stream.FlushAsync();

               return randomAccessStream;
          }

          public static async Task<SoftwareBitmap> ResizeBitmap(IRandomAccessStream stream, BitmapFrame bitmapFrame, uint newWidth, uint newHeight)
          {
               var decoder = await BitmapDecoder.CreateAsync(stream);
               BitmapTransform bitmapTransform = new BitmapTransform();

               bitmapTransform.ScaledWidth = newWidth;
               bitmapTransform.ScaledHeight = newHeight;

               return await decoder.GetSoftwareBitmapAsync(
                                        bitmapFrame.BitmapPixelFormat,
                                        bitmapFrame.BitmapAlphaMode,
                                        bitmapTransform,
                                        ExifOrientationMode.IgnoreExifOrientation,
                                        ColorManagementMode.DoNotColorManage);
          }

          public static async Task<IRandomAccessStream> ConvertSoftwareBitmapToStream(SoftwareBitmap softwareBitmap)
          {
               try
               {
                    InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
                    BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, randomAccessStream);
                    encoder.SetSoftwareBitmap(softwareBitmap);
                    await encoder.FlushAsync();

                    randomAccessStream.Seek(0);
                    return randomAccessStream;
               }
               catch (Exception ex)
               {
               }
               return null;
          }

          public static async Task<IRandomAccessStream> ConvertFileToStream(StorageFolder storageFolder, string filename)
          {
               try
               {
                    StorageFile file = await storageFolder.GetFileAsync(filename);
                    return await file.OpenAsync(FileAccessMode.Read);
               }
               catch (Exception ex)
               {
               }
               return null;
          }

          public static async Task<BitmapFrame> ConvertStreamToBitmap(IRandomAccessStream stream)
          {
               try
               {
                    BitmapDecoder bitmapDecoder = await BitmapDecoder.CreateAsync(stream);
                    return await bitmapDecoder.GetFrameAsync(0);
               }
               catch (Exception ex)
               {
               }
               return null;
          }

          public static async Task PrintImageWithResize(ReceiptPrintJob job, IRandomAccessStream imageStream, PosPrinterAlignment align, uint maxWidth)
          {
               if (job == null || imageStream == null || maxWidth == 0)
                    return;

               var bitmapFrame = await ConvertStreamToBitmap(imageStream);

               maxWidth = maxWidth - 1;
               float rate = (float)maxWidth / (float)bitmapFrame.OrientedPixelWidth;
               uint newHeight = (uint)(bitmapFrame.OrientedPixelHeight * rate);

               var softwareBitmap = await ResizeBitmap(imageStream, bitmapFrame, maxWidth, newHeight);
               var streamResized = await ConvertSoftwareBitmapToStream(softwareBitmap);
               var bitmapFrameResized = await ConvertStreamToBitmap(streamResized);

               try
               {
                    job.PrintBitmap(bitmapFrameResized, align);
               }
               catch (Exception ex)
               {
               }
          }
     }
}
 
Note!

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\
 
Running the program
The picture is printed on the full width of the paper
 
← Previous topic
Write a program in UWP C# that prints text and images on a receipt EPSON printer
 
Next topic →
Write a program in UWP C# that prints a qr code on a receipt EPSON printer
 
Your feedback ... Comments ...
   
Your Name
Your comment (www links can only be added by a logged-in user)

  Объявления  
  Объявления  
 
Download and install Microsoft Visual Studio to write UWP applications on a computer with the Windows system
Open Visual Studio (to write the project UWP)
Creating a new C# UWP app on a Windows computer
Web API requests
Write a UWP C# program that sends a Web API GET request to a local server
Receipt printer (writing a C# program for printing)
Download and install the library EPSON OPOS ADK (this library is needed to print on an Epson printer using C# code) | I have an EPSON TM-T20X receipt printer
Adding a receipt EPSON printer to the EPSON OPOS ADK program
We write a program in UWP C# that prints text on a receipt EPSON printer (to find a receipt printer I use the C# code PointOfService.PosPrinter.GetDeviceSelector ...)
Write a program in UWP C# that prints text and images on a receipt EPSON printer
Write a program in UWP C# that prints an image of any size on a receipt EPSON POS printer (during execution, change the width of the picture = the width of the paper)
Write a program in UWP C# that prints a qr code on a receipt EPSON printer

  Ваши вопросы присылайте по почте: info@dir.by  
Яндекс.Метрика