|
using System;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Drawing;
namespace AutoDiagramer
{
#region BitmapPrintPageEventHandler CLASS
/// <summary>
/// This class allows the user to print a control.
/// </summary>
public class BitmapPrintPageEventHandler
{
#region Private Fields
// private Control control;
private int currentPage;
private int requiredPagesForWidth;
private int requiredPagesForHeight;
private int lastPage;
private Bitmap bitmap;
#endregion
#region Constructors
/// <summary>
/// Constructor.
/// </summary>
/// <param name="control">The control that will be printed.</param>
public BitmapPrintPageEventHandler(Bitmap bmp)
{
this.Bmp = bmp;
}
#endregion
#region Properties
/// <summary>
/// The Control that will be printed.
/// </summary>
public Bitmap Bmp
{
get
{
return this.bitmap;
}
set
{
if (value == null)
{
throw new ArgumentNullException();
}
this.bitmap = value;
this.currentPage = 0;
}
}
/// <summary>
/// Returns true if the EventHandler is about to print the first page
/// </summary>
public bool IsFirstPage
{
get
{
return this.currentPage == 0;
}
}
#endregion
#region Public Methods
/// <summary>
/// Represents the method that will handle the PrintPage event of a PrintDocument.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">A PrintPageEventArgs that contains the event data.</param>
public virtual void PrintPage(object sender, PrintPageEventArgs e)
{
if (this.currentPage == 0)
{
this.requiredPagesForWidth = ((int)this.bitmap.Width / e.MarginBounds.Width) + 1;
this.requiredPagesForHeight = ((int)this.bitmap.Height / e.MarginBounds.Height) + 1;
this.lastPage = (this.requiredPagesForWidth * this.requiredPagesForHeight) - 1;
}
e.HasMorePages = (this.currentPage < this.lastPage);
int posX = ((int)this.currentPage % this.requiredPagesForWidth) * e.MarginBounds.Width;
int posY = ((int)this.currentPage / this.requiredPagesForWidth) * e.MarginBounds.Height;
e.Graphics.DrawImage(this.bitmap, new Rectangle(e.MarginBounds.X, e.MarginBounds.Y, e.MarginBounds.Width, e.MarginBounds.Height), new Rectangle(posX, posY, e.MarginBounds.Width, e.MarginBounds.Height), GraphicsUnit.Pixel);
++this.currentPage;
if (this.currentPage > this.lastPage)
{
this.currentPage = 0;
}
}
#endregion
}
#endregion
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)
- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence
Both of these at Sussex University UK.
Award(s)
I am lucky enough to have won a few awards for Zany Crazy code articles over the years
- Microsoft C# MVP 2016
- Codeproject MVP 2016
- Microsoft C# MVP 2015
- Codeproject MVP 2015
- Microsoft C# MVP 2014
- Codeproject MVP 2014
- Microsoft C# MVP 2013
- Codeproject MVP 2013
- Microsoft C# MVP 2012
- Codeproject MVP 2012
- Microsoft C# MVP 2011
- Codeproject MVP 2011
- Microsoft C# MVP 2010
- Codeproject MVP 2010
- Microsoft C# MVP 2009
- Codeproject MVP 2009
- Microsoft C# MVP 2008
- Codeproject MVP 2008
- And numerous codeproject awards which you can see over at my blog