用ScottPlot打印

打印绘图的最简单方法是将其渲染为Bitmap打电话Plot.Render(),然后打印Bitmap.网上有很多资源可以用来说明如何使用您选择的框架打印图像。本页演示我喜欢如何使用WinForms打印绘图。

Windows Forms

public Form1()
{
    InitializeComponent();
    formsPlot1.Plot.AddSignal(ScottPlot.DataGen.Sin(51));
    formsPlot1.Plot.AddSignal(ScottPlot.DataGen.Cos(51));
}

private void PrintPage(object sender, PrintPageEventArgs e)
{
    // Determine how large you want the plot to be on the page and resize accordingly
    int width = e.MarginBounds.Width;
    int height = (int)(e.MarginBounds.Width * .5);
    formsPlot1.Plot.Resize(width, height);

    // Give the plot a white background so it looks good on white paper
    formsPlot1.Plot.Style(figureBackground: Color.White);

    // Render the plot as a Bitmap and draw it onto the page
    Bitmap bmp = formsPlot1.Plot.Render();
    e.Graphics.DrawImage(bmp, e.MarginBounds.Left, e.MarginBounds.Top);
}

private void btnPrint_Click(object sender, EventArgs e)
{
    var printDocument = new PrintDocument();
    printDocument.PrintPage += new PrintPageEventHandler(PrintPage);
    var printDialog = new PrintDialog { Document = printDocument };
    var printDiagResult = printDialog.ShowDialog();
    if (printDiagResult == DialogResult.OK)
        printDocument.Print();
}

private void btnPrintPreview_Click(object sender, EventArgs e)
{
    var printDocument = new PrintDocument();
    printDocument.PrintPage += new PrintPageEventHandler(PrintPage);
    var printDialog = new PrintPreviewDialog { Document = printDocument };
    printDialog.ShowDialog();
}