Sponsored Ad

Wednesday, October 28, 2009

Graphics, Multimedia, and Printing Recipes

. NET uses an asynchronous event model based on publication. To print a document, it becomes an instance System.Drawing.Printing.PrintDocument together their places, and then call its Print method, which schedules the print job. The common language runtime (CLR), then fire the BeginPrint, PrintPage, and events of the PrintDocument class EndPrint a new thread. To cover these events and the use of System.Drawing.Graphics oppose the exit provided data to the page. Graphics and text on a page are written the same way as you attract a window using GDI +. However, you may need to track their position on a page, because every Graphics class method requires explicit coordinates that indicate where draw.You to configure printer settings via the PrintDocument.PrinterSettings PrintDocument. DefaultPageSettings and properties. The property returns an object PrinterSettings PrinterSettings full (as described in recipe 8-11), which identifies the printer to use. The property offers a complete PageSettings DefaultPageSettings object that specifies the printer resolution, margins, orientation, and so on. You can set these properties in code, or use System.Windows.Forms.PrintDialog class for the user to make changes through the print dialog box standard Windows (as shown in Figure 8-9). In the Print dialog box, the user can select a printer and select the number of copies. The user can also click the Properties button to configure advanced options such as page layout and printer resolution. Finally, the user can accept or cancel the print operation, click OK or Cancel.

The example of the supplies from a form with a single button. When the user clicks the button, the application makes a new PrintDocument grants the user to configure the settings, and then initiates an asynchronous print operation (provided the user clicks OK). An event handler responds to the event PrintPage and writes several lines of text and picture.

using System;

using System.Drawing;

using System.Windows.Forms;

using System.Drawing.Printing;

using System.IO;

namespace Apress.VisualCSharpRecipes.Chapter08

{

public partial class Recipe08_14 : Form

{

public Recipe08_14()

{

InitializeComponent();

}

private void cmdPrint_Click(object sender, EventArgs e)

{

// Create the document and attach an event handler.

PrintDocument doc = new PrintDocument();

doc.PrintPage += this.Doc_PrintPage;

// Allow the user to choose a printer and specify other settings.

PrintDialog dlgSettings = new PrintDialog();

dlgSettings.Document = doc;

// If the user clicked OK, print the document.

if (dlgSettings.ShowDialog() == DialogResult.OK)

{

// This method returns immediately, before the print job starts.

// The PrintPage event will fire asynchronously.

doc.Print();

}

}

private void Doc_PrintPage(object sender, PrintPageEventArgs e)

{

// Define the font.

using (Font font = new Font("Arial", 30))

{

// Determine the position on the page.

// In this case, we read the margin settings

// (although there is nothing that prevents your code

// from going outside the margin bounds.)

float x = e.MarginBounds.Left;

float y = e.MarginBounds.Top;

// Determine the height of a line (based on the font used).

float lineHeight = font.GetHeight(e.Graphics);

// Print five lines of text.

for (int i = 0; i < 5; i++)

{

// Draw the text with a black brush,

// using the font and coordinates we have determined.

e.Graphics.DrawString("This is line " + i.ToString(), font, Brushes.Black, x, y);

// Move down the equivalent spacing of one line.

y += lineHeight;

}

y += lineHeight;

// Draw an image.

e.Graphics.DrawImage(

Image.FromFile( Path.Combine(Application.StartupPath, "test.jpg") ),

x, y);

}

}

}

}

0 comments:

Post a Comment

Sponsored Ad

Website Update

Followers