Click here to Skip to main content
15,891,431 members
Articles / Desktop Programming / Windows Forms

Output graphics files using your printing code

Rate me:
Please Sign up or sign in to vote.
4.88/5 (25 votes)
21 Mar 2005CPOL3 min read 116.5K   3.2K   56  
"Print" your PrintDocument to common graphics file formats.
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Printing;
using System.IO;
using System.Diagnostics;

using Common;

namespace PrintControllerFileDemo
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox _TextBoxSource;
		private System.Windows.Forms.Button _ButtonSource;
		private System.Windows.Forms.Button _ButtonPreview;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox _TextBoxOutput;
		private System.Windows.Forms.Button _ButtonOutput;
		private System.Windows.Forms.ListView _ListView;
		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.ColumnHeader columnHeader2;
		private System.Windows.Forms.ColumnHeader columnHeader3;
		private System.Windows.Forms.ColumnHeader columnHeader4;
		private System.Windows.Forms.ColumnHeader columnHeader5;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Button _ButtonOK;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.NumericUpDown _Quality;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.NumericUpDown _Scale;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this._TextBoxSource = new System.Windows.Forms.TextBox();
			this._ButtonSource = new System.Windows.Forms.Button();
			this._ButtonPreview = new System.Windows.Forms.Button();
			this.label2 = new System.Windows.Forms.Label();
			this._TextBoxOutput = new System.Windows.Forms.TextBox();
			this._ButtonOutput = new System.Windows.Forms.Button();
			this._ListView = new System.Windows.Forms.ListView();
			this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader5 = new System.Windows.Forms.ColumnHeader();
			this.label3 = new System.Windows.Forms.Label();
			this._ButtonOK = new System.Windows.Forms.Button();
			this.label4 = new System.Windows.Forms.Label();
			this._Quality = new System.Windows.Forms.NumericUpDown();
			this.label5 = new System.Windows.Forms.Label();
			this._Scale = new System.Windows.Forms.NumericUpDown();
			((System.ComponentModel.ISupportInitialize)(this._Quality)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this._Scale)).BeginInit();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 8);
			this.label1.Name = "label1";
			this.label1.TabIndex = 0;
			this.label1.Text = "Source File";
			// 
			// _TextBoxSource
			// 
			this._TextBoxSource.Location = new System.Drawing.Point(112, 8);
			this._TextBoxSource.Name = "_TextBoxSource";
			this._TextBoxSource.Size = new System.Drawing.Size(376, 20);
			this._TextBoxSource.TabIndex = 1;
			this._TextBoxSource.Text = "_TextBoxSource";
			// 
			// _ButtonSource
			// 
			this._ButtonSource.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this._ButtonSource.Location = new System.Drawing.Point(488, 8);
			this._ButtonSource.Name = "_ButtonSource";
			this._ButtonSource.Size = new System.Drawing.Size(24, 23);
			this._ButtonSource.TabIndex = 2;
			this._ButtonSource.Text = "...";
			this._ButtonSource.Click += new System.EventHandler(this._ButtonSource_Click);
			// 
			// _ButtonPreview
			// 
			this._ButtonPreview.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this._ButtonPreview.Location = new System.Drawing.Point(8, 312);
			this._ButtonPreview.Name = "_ButtonPreview";
			this._ButtonPreview.TabIndex = 8;
			this._ButtonPreview.Text = "&Preview...";
			this._ButtonPreview.Click += new System.EventHandler(this._ButtonPreview_Click);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 40);
			this.label2.Name = "label2";
			this.label2.TabIndex = 3;
			this.label2.Text = "Output File";
			// 
			// _TextBoxOutput
			// 
			this._TextBoxOutput.Location = new System.Drawing.Point(112, 40);
			this._TextBoxOutput.Name = "_TextBoxOutput";
			this._TextBoxOutput.Size = new System.Drawing.Size(376, 20);
			this._TextBoxOutput.TabIndex = 4;
			this._TextBoxOutput.Text = "_TextBoxOutput";
			// 
			// _ButtonOutput
			// 
			this._ButtonOutput.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this._ButtonOutput.Location = new System.Drawing.Point(488, 40);
			this._ButtonOutput.Name = "_ButtonOutput";
			this._ButtonOutput.Size = new System.Drawing.Size(24, 23);
			this._ButtonOutput.TabIndex = 5;
			this._ButtonOutput.Text = "...";
			// 
			// _ListView
			// 
			this._ListView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																						this.columnHeader1,
																						this.columnHeader2,
																						this.columnHeader3,
																						this.columnHeader4,
																						this.columnHeader5});
			this._ListView.FullRowSelect = true;
			this._ListView.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
			this._ListView.HideSelection = false;
			this._ListView.Location = new System.Drawing.Point(8, 96);
			this._ListView.MultiSelect = false;
			this._ListView.Name = "_ListView";
			this._ListView.Size = new System.Drawing.Size(504, 200);
			this._ListView.TabIndex = 7;
			this._ListView.View = System.Windows.Forms.View.Details;
			this._ListView.DoubleClick += new System.EventHandler(this._ListView_DoubleClick);
			// 
			// columnHeader1
			// 
			this.columnHeader1.Text = "Format";
			this.columnHeader1.Width = 80;
			// 
			// columnHeader2
			// 
			this.columnHeader2.Text = "Codec";
			this.columnHeader2.Width = 120;
			// 
			// columnHeader3
			// 
			this.columnHeader3.Text = "Mime";
			this.columnHeader3.Width = 80;
			// 
			// columnHeader4
			// 
			this.columnHeader4.Text = "Extensions";
			this.columnHeader4.Width = 140;
			// 
			// columnHeader5
			// 
			this.columnHeader5.Text = "Description";
			this.columnHeader5.Width = 80;
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 72);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(504, 23);
			this.label3.TabIndex = 6;
			this.label3.Text = "Formats: Double-click to export in selected format";
			// 
			// _ButtonOK
			// 
			this._ButtonOK.DialogResult = System.Windows.Forms.DialogResult.OK;
			this._ButtonOK.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this._ButtonOK.Location = new System.Drawing.Point(440, 312);
			this._ButtonOK.Name = "_ButtonOK";
			this._ButtonOK.TabIndex = 11;
			this._ButtonOK.Text = "&Close";
			this._ButtonOK.Click += new System.EventHandler(this._ButtonOK_Click);
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(264, 312);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(72, 23);
			this.label4.TabIndex = 9;
			this.label4.Text = "Jpeg quality";
			// 
			// _Quality
			// 
			this._Quality.Location = new System.Drawing.Point(336, 312);
			this._Quality.Name = "_Quality";
			this._Quality.Size = new System.Drawing.Size(56, 20);
			this._Quality.TabIndex = 10;
			this._Quality.Value = new System.Decimal(new int[] {
																   75,
																   0,
																   0,
																   0});
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(128, 312);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(56, 23);
			this.label5.TabIndex = 12;
			this.label5.Text = "Scale %";
			// 
			// _Scale
			// 
			this._Scale.Location = new System.Drawing.Point(184, 312);
			this._Scale.Maximum = new System.Decimal(new int[] {
																   1000,
																   0,
																   0,
																   0});
			this._Scale.Name = "_Scale";
			this._Scale.Size = new System.Drawing.Size(56, 20);
			this._Scale.TabIndex = 13;
			this._Scale.Value = new System.Decimal(new int[] {
																 100,
																 0,
																 0,
																 0});
			// 
			// Form1
			// 
			this.AcceptButton = this._ButtonOK;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(520, 350);
			this.Controls.Add(this._Scale);
			this.Controls.Add(this.label5);
			this.Controls.Add(this._Quality);
			this.Controls.Add(this.label4);
			this.Controls.Add(this._ButtonOK);
			this.Controls.Add(this.label3);
			this.Controls.Add(this._ListView);
			this.Controls.Add(this.label2);
			this.Controls.Add(this._ButtonPreview);
			this.Controls.Add(this._ButtonSource);
			this.Controls.Add(this._TextBoxSource);
			this.Controls.Add(this.label1);
			this.Controls.Add(this._TextBoxOutput);
			this.Controls.Add(this._ButtonOutput);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.Name = "Form1";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "PrintControllerFileDemo";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
			this.Load += new System.EventHandler(this.Form1_Load);
			((System.ComponentModel.ISupportInitialize)(this._Quality)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this._Scale)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.EnableVisualStyles();
			Application.DoEvents();

			Application.Run( new Form1() );
		}

		private void Form1_Load( object sender, System.EventArgs e )
		{
			_TextBoxSource.Text = Glob.Source.String;
			_TextBoxOutput.Text = Glob.Output.String;

			LoadListView();
		}

		private void LoadListView()
		{
			PrintControllerFormat[] formats = PrintControllerFormat.Formats;

			foreach ( PrintControllerFormat format in formats )
			{
				ListViewItem item = _ListView.Items.Add( format.Name );
				item.Tag = format.Format;

				ImageCodecInfo codec = format.Codec;
				if ( codec != null )
				{
					item.SubItems.Add( codec.CodecName );
					item.SubItems.Add( codec.MimeType );
					item.SubItems.Add( codec.FilenameExtension );
					item.SubItems.Add( codec.FormatDescription );
				}
			}
		}

		private void _ButtonSource_Click( object sender, System.EventArgs e )
		{
			OpenFileDialog dlg = new OpenFileDialog();
			dlg.InitialDirectory = Path.GetDirectoryName( _TextBoxSource.Text );
			dlg.Filter = "Text files ( *.txt )|*.txt";
			if ( dlg.ShowDialog() != DialogResult.OK ) return;

			_TextBoxSource.Text = dlg.FileName;
		}

		private void _ListView_DoubleClick( object sender, System.EventArgs e )
		{
			ListView.SelectedListViewItemCollection items = _ListView.SelectedItems;
			if ( items.Count < 1 ) { Debug.Assert( false ); return; }

			ListViewItem item = items[ 0 ];
			ImageFormat format = ( ImageFormat ) item.Tag;

			using ( PrintDocument doc = new PrintDocumentTextFile( _TextBoxSource.Text, 20 ) )
			{
				float scale = Convert.ToSingle( _Scale.Value ) / 100f;
				long quality = Convert.ToInt64( _Quality.Value );
				string output = _TextBoxOutput.Text;

				PrintController controller = new PrintControllerFile( format, scale, quality, output );

				doc.PrintController = new PrintControllerWithStatusDialog( controller, "Exporting" );

				doc.Print();
			}
		}

		private void _ButtonPreview_Click( object sender, System.EventArgs e )
		{
			PrintPreviewDialog dlg = new PrintPreviewDialog();
			dlg.Document = new PrintDocumentTextFile( _TextBoxSource.Text, 20 );
			dlg.ShowDialog();
		}

		private void Form1_Closing( object sender, System.ComponentModel.CancelEventArgs e )
		{
			Glob.Source.Set( _TextBoxSource.Text );
			Glob.Output.Set( _TextBoxOutput.Text );
		}

		private void _ButtonOK_Click( object sender, System.EventArgs e )
		{
			Close();
		}
	}
}

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
I discovered C# and .NET 1.0 Beta 1 in late 2000 and loved them immediately.
I have been writing software professionally in C# ever since

In real life, I have spent 3 years travelling abroad,
I have held a UK Private Pilots Licence for 20 years,
and I am a PADI Divemaster.

I now live near idyllic Bournemouth in England.

I can work 'virtually' anywhere!

Comments and Discussions