using System;
using System.Drawing;
using System.Windows.Forms;

namespace _14_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.FileName = string.Empty;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Load(openFileDialog1.FileName);
                pictureBox1.Refresh();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button1.Text = "Open";
            button2.Text = "Preview";
            button3.Text = "Print";
            button4.Text = "Print Dialog";

            pictureBox1.BackColor = Color.Blue;
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            pictureBox1.BorderStyle = BorderStyle.Fixed3D;

            openFileDialog1.Title = "Select File";
            openFileDialog1.InitialDirectory = "C:\\";
            openFileDialog1.Filter = "Picture Files(*.jpg)|*.jpg|All Files(*.*)|*.*";

            printDocument1.DocumentName = "Picture";
            printDocument1.DefaultPageSettings.Landscape = true;
            printPreviewDialog1.Document = printDocument1;
            printDialog1.Document = printDocument1;
            printDialog1.AllowCurrentPage = false;
            printDialog1.AllowPrintToFile = false;
            printDialog1.AllowSelection = false;
            printDialog1.AllowSomePages = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image != null)
                printPreviewDialog1.ShowDialog();
            else
                MessageBox.Show("Please select file with open button.");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image != null)
                printDocument1.Print();
            else
                MessageBox.Show("Please select file with open button.");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image != null)
            {
                if (printDialog1.ShowDialog() == DialogResult.OK)
                {
                    printDocument1.PrinterSettings = printDialog1.PrinterSettings;
                    printDocument1.Print();
                }
            }
            else
                MessageBox.Show("Please select file with open button.");
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            e.Graphics.DrawImage(pictureBox1.Image, e.MarginBounds);
        }
    }
}