Search
Duplicate

Creating a UWP Desktop App in C# 5/5: Adding Printing Functionality to Complete the App

Published On
2024/09/13
Lang
EN
Tags
Programming
Window Development
UWP

Series

Hello! In this post, we'll explain how to add a printing function to the photo booth app and package the app into an executable file. This will complete the app and prepare it for distribution. Since the UI is already prepared, let's create print-related functions to allow users to immediately output their captured photos to the printer.

1. Implementing the Printing Function

In UWP, we can implement printing functionality using the Windows.Graphics.Printing API. We'll use PrintManager and PrintDocument to provide a print preview and support output to the printer.

1.1 Initializing PrintManager and Creating Print Preview

We'll initialize PrintManager and PrintDocument to handle print jobs and prepare to print the captured images.
private PrintManager printManager; private PrintDocument printDocument; private IPrintDocumentSource printDocumentSource; private UIElement printPreview; // Variable to store the print preview public MainPage() { this.InitializeComponent(); EnterFullScreenMode(); InitializeCameraAsync(); // Initialize PrintManager printManager = PrintManager.GetForCurrentView(); printManager.PrintTaskRequested += PrintManager_PrintTaskRequested; }
C#
복사
PrintManager is the central class for handling print requests. We initialize it and set it up to process print jobs when the user requests printing.

1.2 Handling the Print Button Click Event

This is the event called when the user clicks the print button. In this function, we use PrintManager.ShowPrintUIAsync() to display the print dialog and initiate the print job.
private async void PrintButton_Click(object sender, RoutedEventArgs e) { try { // Update print preview printPreview = CreatePrintPage(); // Reinitialize PrintDocument printDocument = new PrintDocument(); printDocumentSource = printDocument.DocumentSource; printDocument.Paginate += PrintDocument_Paginate; printDocument.GetPreviewPage += PrintDocument_GetPreviewPage; printDocument.AddPages += PrintDocument_AddPages; // Start printing task await PrintManager.ShowPrintUIAsync(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"Error occurred in PrintButton_Click: {ex.Message}"); } }
C#
복사
The PrintButton_Click function generates a print preview and displays the print UI through PrintManager when the user clicks the print button.

1.3 Processing the Print Job

We use PrintManager_PrintTaskRequested and PrintDocument to process the print job. By creating the page to be printed and providing its preview, we display the printing process to the user.
private void PrintManager_PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args) { var printTask = args.Request.CreatePrintTask("PhotoBooth Print", sourceRequestedArgs => { sourceRequestedArgs.SetSource(printDocumentSource); }); printTask.Completed += PrintTask_Completed; } private void PrintDocument_Paginate(object sender, PaginateEventArgs e) { printDocument.SetPreviewPageCount(1, PreviewPageCountType.Final); } private void PrintDocument_GetPreviewPage(object sender, GetPreviewPageEventArgs e) { // Use the pre-generated print preview printDocument.SetPreviewPage(1, printPreview); } private void PrintDocument_AddPages(object sender, AddPagesEventArgs e) { // Use the pre-generated print preview printDocument.AddPage(printPreview); printDocument.AddPagesComplete(); }
C#
복사
PrintManager_PrintTaskRequested sets the content to be printed when a print job is requested and provides a preview of the pages to be printed through PrintDocument.

1.4 Generating the Image to Print

This is a function that composes the page containing the image to be printed. Here, we retrieve the captured photo from CapturedImage.Source and add it to the page to be printed.
private UIElement CreatePrintPage() { Image printImage = new Image { Source = CapturedImage.Source, // Composite image Stretch = Windows.UI.Xaml.Media.Stretch.UniformToFill, // Image size adjusted to fit the screen HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, }; StackPanel printPanel = new StackPanel(); printPanel.Children.Add(printImage); return printPanel; }
C#
복사
This function generates an Image object containing the composite image to prepare it for printing.

2. Packaging into an Executable File

Now that we've added the printing functionality, the next step is to package the app into an executable file for distribution. To package a UWP app into an executable file, follow these steps in Visual Studio:

2.1 Building the Project

1.
Ensure that the project builds successfully and can be run in Visual Studio.
2.
Click on Build > Build Solution from the menu to build the project.

2.2 Packaging the App

1.
Select Project > Publish > Create App Packages from the Visual Studio menu.
2.
Choose "Yes" to create app packages.
3.
Select sideloading as the distribution method and generate the app package.

2.3 Distributing the Installation Files

Once the package generation is complete, a package containing the .appx file along with the necessary certificate file (.cer) will be created. By providing these files to other users, they will be able to install the app.

3. Conclusion

In this post, we explained the process of adding a printing function to the photo booth app and packaging the app into an executable file for distribution. This allows users to output their captured photos to a printer and brings the app to a state where it can be distributed.

Final Summary

Through this five-part series, we developed a photo booth app using C# and UWP. The app includes camera capture, image compositing, and printing functions, and we've completed it to the point of distribution. Moving forward, we can continue to implement and expand various features using UWP and C#.

Read in other languages:

Support the Author:

If you enjoy my article, consider supporting me with a coffee!
Search
September 2024
Today
S
M
T
W
T
F
S