【C#】WinFormsの印刷プレビュー(GDI)

下記を参照設定に追加する必要があります

  • System.Drawing

private void button1_Click(object sender, EventArgs e)
{
    var printDoc = new PrintDocument();
    printDoc.DocumentName = "線の印刷サンプル";
    printDoc.PrintPage += PrintDoc_PrintPage;

    PrintPreviewDialog previewDialog = new PrintPreviewDialog();
    previewDialog.Height = 600;
    previewDialog.Width = 800;
    previewDialog.Document = printDoc;
    previewDialog.Show(this);
}

private void PrintDoc_PrintPage(object sender, PrintPageEventArgs e)
{
    e.Graphics.PageUnit = GraphicsUnit.Millimeter;
    var pen = new Pen(System.Drawing.Color.Black, 1);
    e.Graphics.DrawLine(pen, new Point(20, 20), new Point(180, 20));
}

【C#】ミリメートル指定で線を印刷してみる(GDI)

参照設定で System.Drawing を追加する必要があります。

private void button1_Click(object sender, RoutedEventArgs e)
{
    var printDoc = new System.Drawing.Printing.PrintDocument();
    printDoc.DocumentName = "線の印刷サンプル";
    printDoc.PrintPage += PrintDoc_PrintPage;
    printDoc.Print();
}

private void PrintDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    //単位をミリメートルにする
    e.Graphics.PageUnit = System.Drawing.GraphicsUnit.Millimeter;
    //1mmのペンを作成する
    var pen = new System.Drawing.Pen(System.Drawing.Color.Black, 1);
    //ラインを引く
    e.Graphics.DrawLine(pen, new System.Drawing.Point(20,20), new System.Drawing.Point(180,20));
}