if (string.IsNullOrWhiteSpace(textBox1.Text)) return; // Prevents execution of below statements if filename is not selected. PrintDocument pd = new PrintDocument(); PrintController printController = new StandardPrintController(); pd.PrintController = printController; pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0); pd.PrinterSettings.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0); pd.PrintPage += (sndr, args) => { System.Drawing.Image i = System.Drawing.Image.FromFile(textBox1.Text); System.Drawing.Rectangle m = args.MarginBounds; //Logic below maintains Aspect Ratio if ((double)i.Width / (double)i.Height > (double)m.Width / (double)m.Height) // image is wider { m.Height = (int)((double)i.Height / (double)i.Width * (double)m.Width); } else { m.Width = (int)((double)i.Width / (double)i.Height * (double)m.Height); } //Calculating optimal orientation. pd.DefaultPageSettings.Landscape = m.Height > m.Width; // Putting image in center of page. m.Y = (int)((((System.Drawing.Printing.PrintDocument)(sndr)).DefaultPageSettings.PaperSize.Height - m.Height) / 2); m.X = (int)((((System.Drawing.Printing.PrintDocument)(sndr)).DefaultPageSettings.PaperSize.Width - m.Width) / 2); args.Graphics.DrawImage(i, m); }; pd.Print();
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)