else e.HasMorePages = false;
}
To let the user preview the output before the image is sent to the printer for printing, use the PrintPreviewDialog()
class:
private void btnPreview_Click(object sender, EventArgs e) {
//---show preview---
PrintPreviewDialog dlg = new PrintPreviewDialog() {
Document = printDoc
};
dlg.ShowDialog();
}
This code previews the output in a separate window (see Figure 16-15). The user can click the printer icon to send the output to the printer. The user can also choose to enlarge the page or view multiple pages on one single screen.

Figure 16-15
To print the image to a printer, use the PrintDialog
class to let the user choose the desired printer (see Figure 16-16) instead of sending the output directly to the default printer:
private void btnPrint_Click(object sender, EventArgs e) {
//---let user select a printer to print---
PrintDialog pd = new PrintDialog() {
Document = printDoc, AllowSomePages = true
};
DialogResult result = pd.ShowDialog();
if (result == DialogResult.OK) printDoc.Print();
}

Figure 16-16
Figure 16-17 shows the output if the user indicated that he wanted to print from page 1 to 3 (in Form1
). Note the page number displayed below the image.

Figure 16-17
Deploying the Application
Now the application is ready to be deployed to your customers. One of the most challenging tasks faced by Windows application developers today is the deployment of their applications on the client machines. Once an application is deployed, any change to or maintenance of the application requires redeployment. Worse, with so many different client configurations, updating a Windows application is always fraught with unknowns.
Beginning with Visual Studio 2005, Microsoft rolled out a new deployment technology known as ClickOnce, which makes such deployments and even updates extremely easy and painless. ClickOnce was designed specifically to ease the deployment of Windows applications, in particular
Using ClickOnce, a Windows application can be deployed through the convenience of a web server, file servers, or even CDs. Once an application is installed using ClickOnce, it can automatically check for new updates to the application from the publisher, saving a lot of effort in maintenance and application upgrades. On the security front, ClickOnce applications run within a secure sandbox and are configured using the Code Access Security model.
Publishing the Application Using ClickOnce
Deploying your application using ClickOnce is very straightforward. In Visual Studio 2008, select Build→Publish PhotoViewer (see Figure 16-18).

Figure 16-18
The Publish Wizard (see Figure 16-19) opens. By default, your application will be published to the local web server (IIS) using the path shown in the textbox. However, you can also publish your application using a disk path, file share, FTP, or an external web server. For this example, use the default and click Next.

Figure 16-19
In the next page, indicate if the application is available both online and offline or available online only. Accept the default selection, and click Next to proceed to the next step.
In the next page, click Finish to complete the wizard and start the publishing process. When publishing is completed, a web page (publish.htm
) appears; it contains a link to install the application (see Figure 16-20).

Figure 16-20
The Publish.htm
page lists the following:
□ Name, Version, and Publisher information
□ Prerequisites required for your application (automatically generated based on the application you are deploying)
The URL http://<server_name>/PhotoViewer/publish.htm
is the deployment location of your application. Users who want to install this application through ClickOnce simply need to go to this URL, using their web browser. You provide the URL to your users through email, brochures, and so on.
To install the application, click the Install button. You are presented with:
□ File Download dialog — Security Warning prompt. Click Run to download the application.
□ Internet Explorer dialog — Security Warning. Click Run to proceed with the installation.
□ Application Install dialog — Security Warning. Click Install to install the application (see Figure 16-21).

Figure 16-21
Once installed, the application is launched automatically. You can also launch the application from