using System;
using System.Windows.Forms;
using Microsoft;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
public class PrintingExample : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox pictureBox;
private System.Windows.Forms.Button printButton;
private Font printFont;
public PrintingExample() : base()
{
// The Windows Forms Designer requires the following call.
InitializeComponent();
}
// The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null;
// Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
Image graph = pictureBox.Image;
ev.Graphics.DrawImage(graph, graph.Width, graph.Height);
// Print each line of the file.
//while(count < linesPerPage && ((line=streamToPrint.ReadLine()) != null))
//{
// yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
// ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
// count++;
//}
// If more lines exist, print another page.
if(line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
// The Windows Forms Designer requires the following procedure.
private void InitializeComponent() {
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(PrintingExample));
this.printButton = new System.Windows.Forms.Button();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.SuspendLayout();
//
// printButton
//
this.printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.printButton.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.printButton.Location = new System.Drawing.Point(360, 328);
this.printButton.Name = "printButton";
this.printButton.Size = new System.Drawing.Size(136, 40);
this.printButton.TabIndex = 0;
this.printButton.Text = "Print the file.";
this.printButton.Click += new System.EventHandler(this.PrintButtonClick);
//
// pictureBox
//
this.pictureBox.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox.Image")));
this.pictureBox.Location = new System.Drawing.Point(0, 8);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(496, 312);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
// PrintingExample
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(504, 381);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.printButton);
this.Name = "PrintingExample";
this.Text = "Print Example";
this.Load += new System.EventHandler(this.PrintingExampleLoad);
this.ResumeLayout(false);
}
// This is the main entry point for the application.
public static void Main(string[] args)
{
Application.Run(new PrintingExample());
}
void PrintingExampleLoad(object sender, System.EventArgs e)
{
}
void PrintButtonClick(object sender, System.EventArgs e)
{
try
{
try
{
printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler
(this.pd_PrintPage);
pd.Print();
}
catch (Exception ex1) {
MessageBox.Show(ex1.Message);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}