/// <summary> /// Start() combines Print() and Save() to do all of the work /// </summary> /// <param name="Ctrl">Control (form or other) that is to be printed or saved</param> /// <param name="bPrint">Desides whether to print (true) or save (false)</param> /// <param name="fImage">System.Drawing.Imaging.ImageFormat by which the bitmap /// has to be saved (printing uses png format as standard)</param> /// <param name="f8bitPixel">decides how to convert bitmap to 8 bit ColorDepth</param> /// <param name="sPrinterName">the name of the printer that is to be used</param> /// <param name="aMargins">the page margins that are to be used</param> /// <param name="bPortrait">Printing as portrait (true) or landscape (false)</param> /// <param name="sFileName">String with the complete filename to which the bitmap /// is to be saved (used only if bPrint == true)</param> private static void Start(Control Ctrl, bool bPrint, // formatings ImageFormat fImage, ColorDepthReduction f8bitPixel, // additional parameters for printing string sPrinterName, Margins aMargins, bool bPortrait, // additional parameter for saving string sFileName ) { // add required standard parameters if necessary if (fImage == null) fImage = ImageFormat.Png; if (bPrint) { if (sPrinterName == null) sPrinterName = ""; // orientation and margins are set to the printer standard automatically } else { #region NET 2.0 a shorter way to check string.IsNullOrEmpty(sFileName) // NET 2.0 - you can use next 'if'-query as follows: // if (String.IsNullOrEmpty(sFileName)) // pay attention: String.IsNullOrEmpty() is new in NET 2.0 // NET 1.1 - you must use next 'if'-query as follows: // if ((sFileName == null) || (sFileName == "")) #endregion // no filename => user's folder, control's name, imageformat as extension if ((sFileName == null) || (sFileName == "")) { sFileName = System.IO.Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), Ctrl.Name + "." + fImage.ToString() ); } } // check possible errors, otherwise execute if (Ctrl == null) // reserved for further checks { if (Ctrl == null) MessageBox.Show("Control is not defined", "FormPrint.Start"); else MessageBox.Show("Any other error", "FormPrint.Start"); } else { // create the private formprint class cls cls = new FormPrint(Ctrl, bPrint, fImage, f8bitPixel, sPrinterName, aMargins, bPortrait, sFileName); // execute the required printing resp. saving method try { if (bPrint) { cls.StartPrinting(); } else { cls.StartSaving(); } } finally { // free resources cls.bmp.Dispose(); cls.bmp = null; // in some situations, the next command is useful Ctrl.Refresh(); } } }
/// <summary> /// Start() combines Print() and Save() to do all of the work /// </summary> /// <param name="Ctrl">Control (form or other) that is to be printed or saved</param> /// <param name="bPrint">Desides whether to print (true) or save (false)</param> /// <param name="fImage">System.Drawing.Imaging.ImageFormat by which the bitmap /// has to be saved (printing uses png format as standard)</param> /// <param name="f8bitPixel">decides how to convert bitmap to 8 bit ColorDepth</param> /// <param name="sPrinterName">the name of the printer that is to be used</param> /// <param name="aMargins">the page margins that are to be used</param> /// <param name="bPortrait">Printing as portrait (true) or landscape (false)</param> /// <param name="sFileName">String with the complete filename to which the bitmap /// is to be saved (used only if bPrint == true)</param> private static void Start(Control Ctrl, bool bPrint, // formatings ImageFormat fImage, ColorDepthReduction f8bitPixel, // additional parameters for printing string sPrinterName, Margins aMargins, bool bPortrait, // additional parameter for saving string sFileName ) { // add required standard parameters if necessary if (fImage == null) { fImage = ImageFormat.Png; } if (bPrint) { if (sPrinterName == null) { sPrinterName = ""; } // orientation and margins are set to the printer standard automatically } else { #region NET 2.0 a shorter way to check string.IsNullOrEmpty(sFileName) // NET 2.0 - you can use next 'if'-query as follows: // if (String.IsNullOrEmpty(sFileName)) // pay attention: String.IsNullOrEmpty() is new in NET 2.0 // NET 1.1 - you must use next 'if'-query as follows: // if ((sFileName == null) || (sFileName == "")) #endregion // no filename => user's folder, control's name, imageformat as extension if ((sFileName == null) || (sFileName == "")) { sFileName = System.IO.Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), Ctrl.Name + "." + fImage.ToString()); } } // check possible errors, otherwise execute if (Ctrl == null) // reserved for further checks { if (Ctrl == null) { MessageBox.Show("Control is not defined", "FormPrint.Start"); } else { MessageBox.Show("Any other error", "FormPrint.Start"); } } else { // create the private formprint class cls cls = new FormPrint(Ctrl, bPrint, fImage, f8bitPixel, sPrinterName, aMargins, bPortrait, sFileName); // execute the required printing resp. saving method try { if (bPrint) { cls.StartPrinting(); } else { cls.StartSaving(); } } finally { // free resources cls.bmp.Dispose(); cls.bmp = null; // in some situations, the next command is useful Ctrl.Refresh(); } } }