This routine demonstrates how to print an image. There is also provisions for resizing the output and changing the orientation. Download Program and Source Code - Tutor23 - Image Printing using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Tutor23 { public partial class Form1 : Form { string fname = ""; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //MessageBox.Show(fname); Print dlg = new Print(fname); dlg.ShowDialog(this); } private void button2_Click(object sender, EventArgs e) { using (OpenFileDialog dlg = new OpenFileDialog()) { dlg.Filter = "Image files (*.jpg, *.tif, *.bmp)|*.BMP;*.TIF;*.JPG|All files|*.*"; dlg.RestoreDirectory = true; if (dlg.ShowDialog() == DialogResult.OK) { fname = dlg.FileName; pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; Bitmap pic = new Bitmap(fname); pictureBox1.Image = pic; } } } private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { System.Diagnostics.Process.Start("http://sites.google.com/site/electricfarmcsharp/"); } } } // The actual printing I have made a separate thread so that I can use it in other programs. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Printing; using System.Text; using System.Windows.Forms; namespace Tutor23 { public partial class Print : Form { string fname; int x = 0; int y = 0; int orgx = 0; int orgy = 0; int ret = 0; public Print(string filename) { InitializeComponent(); fname = filename; int indx = fname.LastIndexOf("\\") + 1; Bitmap pic = new Bitmap(fname); orgx = pic.Width; orgy = pic.Height; pic.Dispose(); label1.Text = fname.Substring(indx); label2.Text = " Width: " + orgx; label3.Text = "Height: " + orgy; numericUpDown1.Value = orgx; numericUpDown2.Value = orgy; } private void button1_Click(object sender, EventArgs e) { Form.ActiveForm.Close(); } private void fitToPage() { ret=0; numericUpDown1.Value = orgx; numericUpDown2.Value = orgy; if (checkBox1.Checked == true) { // Here is where we fit to page // Do the calculation int maxx; int maxy; if (radioButton1.Checked == true) { maxx = 1050; maxy = 800; } else { maxx = 800; maxy = 1050; } x = (int)maxx; y = (int)maxx * orgy / orgx; if (y > maxy) { y = maxy; x = (int)maxy * orgx / orgy; } } if (checkBox1.Checked == false) { x = orgx; y = orgy; } numericUpDown1.Value = (int)x; numericUpDown2.Value = (int)y; ret = 1; } private void checkBox1_CheckedChanged(object sender, EventArgs e) { fitToPage(); } private void radioButton1_CheckedChanged(object sender, EventArgs e) { fitToPage(); } private void radioButton2_CheckedChanged(object sender, EventArgs e) { fitToPage(); } private void numericUpDown1_ValueChanged(object sender, EventArgs e) { if (ret == 1) { ret = 0; int tempx = (int)numericUpDown1.Value; int tempy = tempx * orgy / orgx; numericUpDown2.Value = (int)tempy; ret = 1; } } private void numericUpDown2_ValueChanged(object sender, EventArgs e) { if (ret == 1) { ret = 0; int tempy = (int)numericUpDown2.Value; int tempx = tempy * orgx / orgy; numericUpDown1.Value = (int)tempx; ret = 1; } } #region-------------------------------- Print the picture // Specifies what happens when the PrintPage event is raised. private void pd_PrintPage(object sender, PrintPageEventArgs ev) { // Draw a picture. Bitmap pic = new Bitmap(fname); x= (int) numericUpDown1.Value; y= (int) numericUpDown2.Value; ev.Graphics.DrawImage(pic, 0, 0, x, y); // Indicate that there is another page to print. ev.HasMorePages = false; } private void button2_Click(object sender, EventArgs e) { try { PrintDocument pd = new PrintDocument(); PrintDialog dlg = new PrintDialog(); dlg.Document = pd; DialogResult result = dlg.ShowDialog(); if (radioButton1.Checked == true) pd.DefaultPageSettings.Landscape = true; if (radioButton2.Checked == true) pd.DefaultPageSettings.Landscape = false; pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage); if (result == DialogResult.OK) { pd.Print(); } } catch (Exception ex) { MessageBox.Show("An error occurred while printing ", ex.ToString()); } } #endregion } } |
