Monday, July 20, 2015

Print Image using C#

  Hi in this blog I am going to show to print image using PrintDocument in C#.Lets us how to do it


Create a Windows Form Application and design from like this with following controls

Add the following code 

    //Browse Button Handler
    private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                openFileDialog1.Filter = "Jpeg | *.jpg";
                if(openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    textBox1.Text = openFileDialog1.FileName;
                    bmp = new Bitmap(textBox1.Text);
                    pictureBox1.Image = bmp;
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("Exception:"+ex.ToString());
            }
        }

        //Preview Button handler
        private void button2_Click(object sender, EventArgs e)
        {
            printPreviewDialog1.Document = printDocument1;
            printPreviewDialog1.ShowDialog();
        }
         
       //PrintDoument handler
        private void printDocument1_PrintPage(object sender,                     System.Drawing.Printing.PrintPageEventArgs e)
        {           
            e.Graphics.DrawImage(bmp,new Rectangle(0,0,bmp.Width,bmp.Height));
        }

        private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
        

        }

Output:

Download the source code here.

Click here for my previous blog.

Happy Coding.



No comments:

Post a Comment