Thursday 28 April 2011

Print Priview Page For PictureBox



Print Priview Page For PictureBox



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace print_dialog_boxes
{
    public partial class Form4 : Form
    {
        private PrintPreviewControl ppc;
        private PrintDocument docToPrint = new PrintDocument();
        public Form4()
        {
            InitializeComponent();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofdlg = new OpenFileDialog();

            ofdlg.InitialDirectory = @"C:\";
            ofdlg.RestoreDirectory = true;
            ofdlg.Title = "Open Images Files";
            //ofdlg.DefaultExt = ".bmp";

            ofdlg.Filter = "All Image files | *.bmp; *.gif; *.jpg; *.ico;";

            ofdlg.FilterIndex = 2;
            ofdlg.CheckFileExists = true;
            ofdlg.CheckPathExists = true;

            if (ofdlg.ShowDialog() == DialogResult.OK)
            {
                this.Text = ofdlg.FileName.ToString();
                pictureBox1.BackColor = Color.Pink;
                pictureBox1.BorderStyle = BorderStyle.Fixed3D;
                pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
               
                pictureBox1.Image = new Bitmap(ofdlg.FileName);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            CreatePrintPreviewControl();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            pictureBox1.Refresh();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
        private void PrintPage(object sender, PrintPageEventArgs e)
        {
            string text = "This text to be printed. ";
            e.Graphics.DrawString(text, new Font("Georgia", 35, FontStyle.Bold), Brushes.Black, 10, 10);
           
           //e.Graphics.DrawImage(this.pictureBox1.Image, 0, 0, this.pictureBox1.Size.Height, this.pictureBox1.Size.Width);
           e.Graphics.DrawImage(this.pictureBox1.Image, 0, 0, ppc.Width ,ppc.Height);
        }

        private void Form4_Load(object sender, EventArgs e)
        {
            this.docToPrint.PrintPage += new PrintPageEventHandler(PrintPage);
        }
        private void CreatePrintPreviewControl()
        {
            ppc = new PrintPreviewControl();
            ppc.Name = "PrintPreviewControl1";
            ppc.Dock = DockStyle.Fill;
            ppc.Location = new Point(100, 100);

            ppc.Document = docToPrint;
            ppc.Zoom = 100;
            ppc.Document.DocumentName = "c:\\";
            ppc.UseAntiAlias = true;
            ppc.UseWaitCursor = true;
            ppc.AutoZoom = true;
            ppc.AllowDrop = true;


            // Add PrintPreviewControl to Form
            Controls.Add(this.ppc);
        }
    }
}

No comments:

Post a Comment