Print stars in C#

[Print stars]

This example shows how you can print stars with specific sizes in C#. Not long ago someone contacted me asking if I knew of a program that could print stars. He wanted to create flags of different countries and needed to be able print stars with different numbers of points at various sizes. He had found the example Draw a star with a given number of points in C# and wanted something similar but with printing. See that example for information about how to generate a star’s points.

This example adds the ability to print. At design time, I added a PrintPreviewDialog and a PrintDocument to the form. The PrintPreviewDialog object’s Document property is set to the PrintDocument object.

Enter the number of points the star should have, the skip number (explained in the earlier example), and the diameter the star should have in millimeters. When you click the Preview button, the following code displays the PrintPreviewDialog.

// Display the print dialog.
private void btnPreview_Click(object sender, EventArgs e)
{
    ppdStar.ShowDialog();
}

When the dialog needs to generate a page, it raises the document object’s PrintPage event. The following code shows the event handler the program uses to draw the star on the printout.

// Draw the star.
private void pdocStar_PrintPage(object sender, PrintPageEventArgs e)
{
    try
    {
        // Convert mm to inches * 100.
        float diameter = float.Parse(txtRadius.Text);
        diameter = diameter / 25.4f * 100f;

        float cx = (e.MarginBounds.Left + e.MarginBounds.Right) / 2f;
        float cy = (e.MarginBounds.Top + e.MarginBounds.Bottom) / 2f;
        float x = cx - diameter / 2f;
        float y = cy - diameter / 2f;

        RectangleF rect = new RectangleF(x, y, diameter, diameter);

        // Draw the star.
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
        DrawStar(e.Graphics, Pens.Red, Brushes.Yellow,
            (int)nudPoints.Value, (int)nudSkip.Value,
            rect);

        e.HasMorePages = false;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

This code parses the diameter you entered. It then converts the distance from millimeters into 100ths of an inch, which is the unit the print document uses.

Next the code calculates the bounds for a squares with side length equal to the desired diameter and centered on the printed page. It uses those bounds to make a RectangleF representing that square.

The code then calls the DrawStar method to draw the star in that RectangleF. See the earlier example for a description of that method.

The code finishes by setting e.HasMorePages = false to indicate that this is the last page of the printout.

When the dialog appears, you can view the star. You can then click the print button on the upper left to send the star to the printer.

Note that a printer might not be exactly calibrated, so a 100mm diameter star might not be exactly 100mm in diameter on your printer. You may need to adjust the diameter slightly to get exactly the result you need.


Download Example   Follow me on Twitter   RSS feed   Donate




About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.

This entry was posted in drawing, graphics, mathematics, printing and tagged , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *