private void PrintPreview(SamplePrintType printType, String documentName) { this.samplePrintType = printType; document.DocumentName = documentName; this.printPreviewDialog.Document = this.document; // Call the ShowDialog method. This will trigger the document's // PrintPage event. this.printPreviewDialog.ShowDialog(this); }
private void HelperPrint(Graphics g, SamplePrintType printType, bool isPreview, RectangleF printerArea) { float width = (float)imageXView1.Image.ImageXData.Width; float height = (float)imageXView1.Image.ImageXData.Height; imageXView1.Image.ImageXData.Resolution.Units = GraphicsUnit.Inch; double resx = Math.Round(imageXView1.Image.ImageXData.Resolution.Dimensions.Width); double resy = Math.Round(imageXView1.Image.ImageXData.Resolution.Dimensions.Height); // Printable area for printers is in 1/100th of an inch, convert to inches printerArea.X /= 100; printerArea.Y /= 100; printerArea.Width /= 100; printerArea.Height /= 100; // put the graphics into inches for convenience g.PageUnit = GraphicsUnit.Inch; // put width and height of image in terms of inches width = (float)(width / resx); height = (float)(height / resy); // default to upper right float x = 0.0f; float y = 0.0f; float interSpacing = 0.25f; // spacing between images float topBottomSpacing = 0.0f; // spacing before first image and after second image float scaleFactor = 1.0f; float oldValue = 0.0f; // keep the preview looking similiar to the hardcopy if (isPreview) { x = printerArea.X; y = printerArea.Y; } switch (printType) { case SamplePrintType.Centered: // allow for larger images, let both checks happen, may need to scale in both directions if (height > printerArea.Height) { oldValue = height; height = printerArea.Height; scaleFactor = height / oldValue; width *= scaleFactor; } if (width > printerArea.Width) { oldValue = width; width = printerArea.Width; scaleFactor = width / oldValue; height *= scaleFactor; } x += (printerArea.Width - width) / 2; y += (printerArea.Height - height) / 2; imageXView1.Print(g, new RectangleF(x, y, width, height)); break; case SamplePrintType.TwoBy: // trying to keep this simple, but allow for large images if (((height * 2) + interSpacing) > printerArea.Height) { oldValue = height; height = (printerArea.Height / 2) - (interSpacing / 2); scaleFactor = height / oldValue; width *= scaleFactor; } if (width > printerArea.Width) { oldValue = width; width = printerArea.Width; scaleFactor = width / oldValue; height *= scaleFactor; } interSpacing = (float)(printerArea.Height - (height * 2)); if (interSpacing >= 0.25f * 3) { interSpacing /= 3; topBottomSpacing = interSpacing; } else { topBottomSpacing = (interSpacing - 0.25f) / 2; if (topBottomSpacing < 0) { topBottomSpacing = 0; } interSpacing = 0.25f; } x += (printerArea.Width - width) / 2; y += topBottomSpacing; imageXView1.Print(g, new RectangleF(x, y, width, height)); y += (height + interSpacing); imageXView1.Print(g, new RectangleF(x, y, width, height)); break; case SamplePrintType.FullPage: imageXView1.Print(g, new RectangleF(x, y, printerArea.Width, printerArea.Height)); break; } }