devDept Eyeshot Documentation Send comments on this topic.
Advanced Printing
Tutorials > Advanced Printing

Glossary Item Box

This article explains how to take the full control over the Eyeshot print support. The Eyeshot built-in print support doesn't address all the requirements of a professional printing. If you need more, just follow the instructions below.

 

Step 1 Creating a new Windows Application Project

Follow the instructions of the Quick Start article to create a new Windows Application.

 

Step 2 Adding a new printDocument to your application Form

Open the Visual Studio Toolbox expand the Printing tab and double click the printDocument component.

Adding a printDocument component to your application Form.

Figure 1: Adding a printDocument component to your application Form.

 

Step 3 Adding the printDocument1 PrintPage event handler

Select the printDocument1 in the Visual Studio component tray, open the Properties window, switch to Events and double click the PrintPage item to add its event handler.

Adding the printDocument1 PrintPage event handler

Figure 2: Adding the printDocument1 PrintPage event handler

 

Step 4 Adding the Print Preview

Add a button called printPreviewButton on the Form, then its printPreviewButton_Click event handler.

Adding the Print Preview button

Figure 3: Adding the Print Preview button.

Then add the following function body:

C# Copy Code
private void printPreviewButton_Click(object sender,
      EventArgs e)
{

  PrintPreviewDialog ppDialog =
new PrintPreviewDialog();

  ppDialog.ClientSize =
new Size(400,500);
  ppDialog.Document = printDocument1;

  ppDialog.ShowDialog();

}
Visual Basic Copy Code
Private Sub Button1_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles Button1.Click

   Dim ppDialog As PrintPreviewDialog = New PrintPreviewDialog()

   ppDialog.ClientSize = New Size(400, 500)
   ppDialog.Document = PrintDocument1

   ppDialog.ShowDialog()

End Sub

After running your app and clicking on the Print Preview button you should see the following empty print preview:

Empty print preview

Figure 4: Empty print preview.

 

Step 5 Some fun with GDI+

In this step we will try to give a professional look to our print document. Just copy and paste the following standard GDI+ calls to your printDocument1_PrintPage() method.

C# Copy Code
private void printDocument1_PrintPage(object sender,
      System.Drawing.Printing.PrintPageEventArgs e)
{

  
// Gets printable area
  
Rectangle insideMargins = new Rectangle(
      e.MarginBounds.X, e.MarginBounds.Y,
      e.MarginBounds.Width, e.MarginBounds.Height);

  Rectangle leftPane = insideMargins;
  leftPane.Width  = 180;

  Rectangle rightPane = insideMargins;
  rightPane.Width -= 180;
  rightPane.X     += 180;

  e.Graphics.FillRectangle(
      
new System.Drawing.Drawing2D.LinearGradientBrush(
      
new System.Drawing.Point(0, 0),
      
new System.Drawing.Point(0, e.MarginBounds.Y + insideMargins.Height),
      Color.DarkBlue, Color.LightGray),
      leftPane);           

  e.Graphics.FillRectangle(
new SolidBrush(Color.Black),
      insideMargins.X, insideMargins.Y + 60, insideMargins.Width, 4);  
 
  System.Drawing.Point titlePos =
new System.Drawing.Point(
      e.MarginBounds.X + 10, e.MarginBounds.Y + 10);

  e.Graphics.DrawString(
"DocTitle",
      
new Font("Tahoma", 24, FontStyle.Bold), new SolidBrush(Color.White),
      titlePos);

  System.Drawing.Point specTitlePos =
new System.Drawing.Point(
      leftPane.Right + 10, e.MarginBounds.Y + 10);

  e.Graphics.DrawString(
"Specifications",
      
new Font("Tahoma", 24), new SolidBrush(Color.Black),
      specTitlePos);

}
Visual Basic Copy Code
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, _
       ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
       Handles PrintDocument1.PrintPage

   ' Gets printable area
   Dim insideMargins As New Rectangle(e.MarginBounds.X, e.MarginBounds.Y,
       e.MarginBounds.Width, e.MarginBounds.Height)

   Dim leftPane As Rectangle = insideMargins
   leftPane.Width = 180

   Dim rightPane As Rectangle = insideMargins
   rightPane.Width -= 180
   rightPane.X += 180

   e.Graphics.FillRectangle( _
       New System.Drawing.Drawing2D.LinearGradientBrush( _
          New System.Drawing.Point(0, 0), _
          New System.Drawing.Point(0, e.MarginBounds.Y + insideMargins.Height), _
          Color.DarkBlue, Color.LightGray), leftPane)

   e.Graphics.FillRectangle(New SolidBrush(Color.Black), _
       insideMargins.X, insideMargins.Y + 60, insideMargins.Width, 4)

   Dim titlePos As New System.Drawing.Point( _
       e.MarginBounds.X + 10, e.MarginBounds.Y + 10)

   e.Graphics.DrawString("DocTitle", _
       New Font("Tahoma", 24, FontStyle.Bold), New SolidBrush(Color.White), _
       titlePos) Dim specTitlePos As New System.Drawing.Point( _
       leftPane.Right + 10, e.MarginBounds.Y + 10)

   e.Graphics.DrawString("Specifications", _
       New Font("Tahoma", 24), New SolidBrush(Color.Black), _
       specTitlePos)

End Sub

 Here is the resulting print preview:

Some GDI+ drawing on the print document

Figure 5: Some GDI+ drawing on the print document.

 

Step 6 Adding the model 3D view

Now we want to add our model 3D view on the right pane. To do this we need to call the RenderToBitmap() method. The only parameter of this method is the dpiScale. With dpiScale you change the size of the image it will return. Using 1 you'll get an image with the same dimensions (in pixel) of the viewport client area, using 2 you'll get an image with two times the dimensions of the viewport control and so on. We will use 3, a reasonable resolution for our Letter sized print document.

C# Copy Code
private void printDocument1_PrintPage(object sender,
      System.Drawing.Printing.PrintPageEventArgs e)
{

  ...

  e.Graphics.DrawString(
"Specifications",
      
new Font("Tahoma", 24),
      
new SolidBrush(Color.Black),
      specTitlePos);

  
// copies current background style
  
backgroundStyleType prev = viewportProfessional1.Background.StyleMode;
  
// sets background as solid white
  
viewportProfessional1.Background.StyleMode = backgroundStyleType.None;

  
// Generates the scene bitmap (3 times bigger than the viewport, this affects the print quality)
  
Bitmap bitmap = viewportProfessional1.RenderToBitmap(3);

  
// restore previous background style
  
viewportProfessional1.Background.StyleMode = prev;

  
// Computes the required image scale
  
float hScale = (float) rightPane.Width / bitmap.Width;
  
float vScale = (float) rightPane.Height / bitmap.Height;
  
float scale = Math.Min(hScale, vScale);

  
// Draws the bitmap on the print document
  
e.Graphics.DrawImage(bitmap,
      rightPane.X, rightPane.Y + 100,
      rightPane.Width, bitmap.Height * scale);

}
Visual Basic Copy Code
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, _
       ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
       Handles PrintDocument1.PrintPage

   ...

   e.Graphics.DrawString("Specifications", _
       New Font("Tahoma", 24), New SolidBrush(Color.Black), _
       specTitlePos)

   ' copies current background style
   Dim prev As backgroundStyleType = viewportProfessional1.Background.StyleMode
   ' sets background as solid white
   viewportProfessional1.Background.StyleMode = backgroundStyleType.None

   ' Generates the scene bitmap (3 times bigger than the viewport, this affects the print quality)
   Dim bitmap As Bitmap = viewportProfessional1.RenderToBitmap(3)

   ' restore previous background style
   viewportProfessional1.Background.StyleMode = prev

   ' Computes the required image scale
   Dim hScale As Single = CSng(rightPane.Width) / bitmap.Width
   Dim vScale As Single = CSng(rightPane.Height) / bitmap.Height

   Dim scale As Single = Math.Min(hScale, vScale)

   ' Draws the bitmap on the print document
   e.Graphics.DrawImage(bitmap, _
       rightPane.X, rightPane.Y + 100, rightPane.Width,
       bitmap.Height * scale)

End Sub

You should now see something like this: (in this picture with a real-life model)

Final print preview

Figure 6: Final print preview.


Step 7 Adding the Page Setup button

In this step we will add the code for a complete Page Setup. Add a button called pageSetupButton to your Form and its pageSetupButton_Click event handler.

Adding the Page Setup button

Figure 7: Adding the Page Setup button.

Then insert the following function body:

C# Copy Code
private void pageSetupButton_Click(object sender, EventArgs e)
{

  PageSetupDialog pageSetupDialog =
new PageSetupDialog();
  
  pageSetupDialog.Document = printDocument1;

  
if (pageSetupDialog.ShowDialog() == DialogResult.OK)

     printDocument1 = pageSetupDialog.Document;

}  
Visual Basic Copy Code
Private Sub pageSetupButton_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles pageSetupButton.Click

   Dim myPageSetupDialog As New PageSetupDialog()

   myPageSetupDialog.Document = PrintDocument1

   If myPageSetupDialog.ShowDialog() = DialogResult.OK Then

      printDocument1 = myPageSetupDialog.Document

   End If

End Sub

 

Step 8 Adding the Print button

As final step we will add the Print button. Add a button called printButton and its printButton_Click event handler.

Adding the Print button

Figure 8: Adding the Print button.

Then insert the following function body:

C# Copy Code
private void printButton_Click(object sender, EventArgs e)
{

  PrintDialog printDialog =
new PrintDialog();

  printDialog.Document = printDocument1;

  
if (printDialog.ShowDialog() == DialogResult.OK)
  {

     printDocument1 = printDialog.Document;
     printDocument1.Print();
  }

}
Visual Basic Copy Code
Private Sub printButton_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles printButton.Click

   Dim myPrintDialog As PrintDialog = New PrintDialog()

   myPrintDialog.Document = PrintDocument1

   If myPrintDialog.ShowDialog() = DialogResult.OK Then

      PrintDocument1 = myPrintDialog.Document
      PrintDocument1.Print()

   End If

End Sub

 

Step 9 Now it's your turn

Try to add more 3D views on this print document, calling the viewport's SetCameraView() method between the calls to RenderToBitmap() one. You can even design your own Print Preview dialog using the standard PrintPreviewControl.