Printing a Multi-page TIFF File

Snowbound Software’s RasterMasterTM for the .NET Platform includes the ability to print multi-page file formats such as TIFF files. You can use the C# code sample below to print a multi-page file.

/**********************************************************************

;PROGRAM: PrintForm.cs

;

;PURPOSE: Printing and preview sample.

;

;Copyright (c) 2005, SnowBound SoftWare.

;

;This is example code for all SnowBound customers to freely copy and use

;however they wish.

;

;LIBRARY FUNCTIONS USED:

; Snowbnd() - Init Snowbound component.

; CIMG_decompress_bitmap() - Load an image from disk.

; CIMG_display_bitmap - Display image.

; CIMG_print_bitmap - Print the image.

**********************************************************************/

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Drawing.Printing;

using System.Threading;

using SbdNetLib;

namespace Print_CSHARP

{

///

/// Summary description for Form1.

///

public class Form1 : System.Windows.Forms.Form

{

// public delegate void PrintPageDoneEventHandler(object sender, EventArgs e);

public delegate void PrintPageDoneEventHandler(EventArgs e);

public event PrintPageDoneEventHandler myPrintPageDone = null;

private System.Windows.Forms.Button btnLoad;

private System.Windows.Forms.Button btnPrint;

private System.Windows.Forms.Panel panImage;

private Snowbnd simage;

private string fileName;

private int iLoaded;

private int status;

private System.Windows.Forms.OpenFileDialog openImageDialog;

private System.Drawing.Printing.PrintDocument printImage;

private System.Windows.Forms.Button btnPreImage;

private System.Windows.Forms.PrintDialog printImageDialog;

private System.Windows.Forms.PrintPreviewDialog printPreImageDialog;

private CDIB_HEADER sdibHeader;

private System.Windows.Forms.PageSetupDialog pageSetupDialog;

private System.Windows.Forms.Button btnPageSetup;

private int iPageNumber;

private int iCurPage = 0;

private int iDecompFlag = 0;

///

/// Required designer variable.

///

private System.ComponentModel.Container components = null;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//

}

///

/// Clean up any resources being used.

///

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

///

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

///

private void InitializeComponent()

{

System.Resources.ResourceManager resources = new

System.Resources.ResourceManager(typeof(Form1));

this.btnLoad = new System.Windows.Forms.Button();

this.btnPrint = new System.Windows.Forms.Button();

this.panImage = new System.Windows.Forms.Panel();

this.openImageDialog = new System.Windows.Forms.OpenFileDialog();

this.printImage = new System.Drawing.Printing.PrintDocument();

this.btnPreImage = new System.Windows.Forms.Button();

this.printImageDialog = new System.Windows.Forms.PrintDialog();

this.printPreImageDialog = new System.Windows.Forms.PrintPreviewDialog();

this.pageSetupDialog = new System.Windows.Forms.PageSetupDialog();

this.btnPageSetup = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// btnLoad

//

this.btnLoad.Location = new System.Drawing.Point(16, 16);

this.btnLoad.Name = "btnLoad";

this.btnLoad.Size = new System.Drawing.Size(120, 32);

this.btnLoad.TabIndex = 0;

this.btnLoad.Text = "Load Image";

this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click);

//

// btnPrint

//

this.btnPrint.Location = new System.Drawing.Point(330, 16);

this.btnPrint.Name = "btnPrint";

this.btnPrint.Size = new System.Drawing.Size(120, 32);

this.btnPrint.TabIndex = 1;

this.btnPrint.Text = "Print";

this.btnPrint.Click += new System.EventHandler(this.btnPrint_Click);

//

// panImage

//

this.panImage.Location = new System.Drawing.Point(16, 64);

this.panImage.Name = "panImage";

this.panImage.Size = new System.Drawing.Size(592, 344);

this.panImage.TabIndex = 2;

this.panImage.Paint += new System.Windows.Forms.PaintEventHandler(this.panImage_Paint);

//

// printImage

//

this.printImage.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printImage_PrintPage);

//

// btnPreImage

//

this.btnPreImage.Location = new System.Drawing.Point(173, 16);

this.btnPreImage.Name = "btnPreImage";

this.btnPreImage.Size = new System.Drawing.Size(120, 32);

this.btnPreImage.TabIndex = 3;

this.btnPreImage.Text = "Print Preview";

this.btnPreImage.Click += new System.EventHandler(this.btnPreImage_Click);

//

// printPreImageDialog

//

this.printPreImageDialog.AutoScrollMargin = new System.Drawing.Size(0, 0);

this.printPreImageDialog.AutoScrollMinSize = new System.Drawing.Size(0, 0);

this.printPreImageDialog.ClientSize = new System.Drawing.Size(400, 300);

this.printPreImageDialog.Enabled = true;

this.printPreImageDialog.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreImageDialog.Icon")));

this.printPreImageDialog.Location = new System.Drawing.Point(26, 966);

this.printPreImageDialog.MinimumSize = new System.Drawing.Size(375, 250);

this.printPreImageDialog.Name = "printPreImageDialog";

this.printPreImageDialog.TransparencyKey = System.Drawing.Color.Empty;

this.printPreImageDialog.Visible = false;

//

// btnPageSetup

//

this.btnPageSetup.Location = new System.Drawing.Point(487, 16);

this.btnPageSetup.Name = "btnPageSetup";

this.btnPageSetup.Size = new System.Drawing.Size(120, 32);

this.btnPageSetup.TabIndex = 4;

this.btnPageSetup.Text = "Page Setup";

this.btnPageSetup.Click += new System.EventHandler(this.btnPageSetup_Click);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(680, 438);

this.Controls.Add(this.btnPageSetup);

this.Controls.Add(this.btnPreImage);

this.Controls.Add(this.panImage);

this.Controls.Add(this.btnPrint);

this.Controls.Add(this.btnLoad);

this.Name = "Form1";

this.Text = "Form1";

this.Load += new System.EventHandler(this.Form1_Load);

this.ResumeLayout(false);

}

#endregion

///

/// The main entry point for the application.

///

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void Form1_Load(object sender, System.EventArgs e)

{

simage = new Snowbnd();

}

private void btnLoad_Click(object sender, System.EventArgs e)

{

openImageDialog.Filter = "Image files (*.bmp)|*.tif|All files (*.*)|*.*";

openImageDialog.FilterIndex = 2;

openImageDialog.RestoreDirectory = true;

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

{

fileName = openImageDialog.FileName;

status = simage.CIMGLOW_set_alias(4);

//for multiple pages

iPageNumber = simage.CIMGLOW_get_pages(fileName);

if(iPageNumber < 1)

iPageNumber = 1;

status = simage.CIMG_decompress_bitmap(fileName);

iLoaded = 1;

if (status >= 0)

{

sdibHeader = new CDIB_HEADER();

status = simage.CIMG_bitmap_info(sdibHeader);

// if (sdibHeader.biHeight > 0)

// panImage.Width = panImage.Height * sdibHeader.biWidth / sdibHeader.biHeight;

if (sdibHeader.biWidth > 0)

panImage.Height = panImage.Width * sdibHeader.biHeight / sdibHeader.biWidth;

}

Refresh();

}

}

private void panImage_Paint(object sender,

System.Windows.Forms.PaintEventArgs e)

{

simage.CIMG_display_bitmap(e.Graphics,0,0,panImage.Width,panImage.Height);

}

private void btnPrint_Click(object sender, System.EventArgs e)

{

// printImage.PrintPage += new PrintPageEventHandler(this.printImage_PrintPage);

if(myPrintPageDone == null)

myPrintPageDone += new

PrintPageDoneEventHandler(this.OnPrintPageDone);

iCurPage = 0;

status = simage.CIMG_decompress_bitmap_page(fileName, iCurPage);

if(status >= 0)

{

sdibHeader = new CDIB_HEADER();

status = simage.CIMG_bitmap_info(sdibHeader);

iDecompFlag = 1;

}

printImageDialog.Document = printImage;

// printImageDialog.PrinterSettings =

printImageDialog.PrinterSettings.FromPage = 0;

printImageDialog.PrinterSettings.ToPage = iPageNumber -1;

// printImageDialog.PrintToFile = true;

DialogResult result = printImageDialog.ShowDialog();

if (result == DialogResult.OK)

{

printImage.Print();

}

}

private void printImage_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)

{

GraphicsUnit gUnit = e.Graphics.PageUnit;

int iWidth = (int)(e.Graphics.DpiX*e.MarginBounds.Width/100);

int iHeight = (int)(e.Graphics.DpiY*e.MarginBounds.Height/100);

//for multiple page

// status = simage.CIMG_decompress_bitmap_page(fileName, iCurPage);

// if(status >= 0)

// {

// sdibHeader = new CDIB_HEADER();

// status = simage.CIMG_bitmap_info(sdibHeader);

while(iDecompFlag == 0)

{

Thread.Sleep(10);

};

if(iWidth/iHeight <= sdibHeader.biWidth / sdibHeader.biHeight)

iHeight = iWidth * sdibHeader.biHeight / sdibHeader.biWidth;

else

iWidth = iHeight * sdibHeader.biWidth / sdibHeader.biHeight;

int iOffX = ((int)(e.Graphics.DpiX*e.MarginBounds.Width/100) - iWidth)/2;

int iOffY = ((int)(e.Graphics.DpiY*e.MarginBounds.Height/100)- iHeight)/2;

simage.CIMG_print_bitmap(e.Graphics, iOffX, iOffY, iWidth, iHeight);

// }

// myPrintPageDone(e);

iCurPage++;

if(iCurPage < iPageNumber)

{

e.HasMorePages = true;

myPrintPageDone.BeginInvoke(e,null,sender);

iDecompFlag = 0;

}

else

e.HasMorePages = false;

// Thread.Sleep(3000);

// PrintPageDone += new EventHandler(EventArgs.Empty);

// PrintPageDone += new EventHandler(this.OnPrintPageDone);

// EventArgs ePrintPageDone = new EventArgs();

// OnPrintPageDone(this,ePrintPageDone);

// myPrintPageDone(this, e);

}

private void btnPreImage_Click(object sender, System.EventArgs e)

{

if(myPrintPageDone == null)

myPrintPageDone += new PrintPageDoneEventHandler(this.OnPrintPageDone);

// myPrintPageDone(this,e);

iCurPage = 0;

status = simage.CIMG_decompress_bitmap_page(fileName, iCurPage);

if(status >= 0)

{

sdibHeader = new CDIB_HEADER();

status = simage.CIMG_bitmap_info(sdibHeader);

iDecompFlag = 1;

}

printPreImageDialog.Document = printImage;

printPreImageDialog.Document.PrinterSettings.FromPage = 0;

printPreImageDialog.ShowDialog();

}

private void btnPageSetup_Click(object sender, System.EventArgs e)

{

pageSetupDialog.PageSettings = printImage.DefaultPageSettings;

pageSetupDialog.PrinterSettings = printImage.PrinterSettings;

DialogResult result = pageSetupDialog.ShowDialog();

if (result == DialogResult.OK)

{

printImage.DefaultPageSettings = pageSetupDialog.PageSettings;

printImage.PrinterSettings = pageSetupDialog.PrinterSettings;

}

}

protected void OnPrintPageDone(EventArgs e)

{

int iPageIndex = iCurPage;

status = simage.CIMG_decompress_bitmap_page(fileName, iPageIndex);

if(status >= 0)

{

sdibHeader = new CDIB_HEADER();

status = simage.CIMG_bitmap_info(sdibHeader);

}

iDecompFlag = 1;

}

}

}

Category:

Online Demo