System.Drawing.Pen错误?

时间:2011-12-07 10:13:39

标签: c# .net drawing system pen

我是新来的,所以我希望我的信息能够清晰地编辑......

我试图在这里向您展示一张非常具有解释性的图片,但不允许新用户发布图片。那么,用文字表示:我在纸上或PDF上打印时画出3行10厘米。但是在屏幕上,中间线应该与最低线看起来相同。它们之间的唯一区别是Width属性。第一条红线。宽度为0.1毫米,第二条红线为。宽度为0.5毫米。

我给两条红线都是DashPattern:4mm dash-1mm space-1mm dash-1mm space。

正如我写的那样;打印时,Dash图案在红线上完全相同! 我认为这是一个错误,当图形显示在屏幕上,但也许我错过了一些东西......下面你会找到一个C#样本项目的完整代码来复制/粘贴。

提前致谢!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;

public partial class Form1 : Form
{
  PrintDialog dialog1 = new PrintDialog();
  PrintDocument printdocument1 = new PrintDocument();

  public Form1()
  {
    this.Text = "System.Drawing.Pen:  Bug?";
    this.Width = 600;
    this.Height = 400;


    // add panel on form
    Panel panel1 = new Panel();
    panel1.Width = 500;
    panel1.Height = 300;
    panel1.BackColor = Color.White;
    panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
    this.Controls.Add(panel1);

    // add print button on form
    Button butPrint = new Button();
    butPrint.Size = new Size(72, 28);
    butPrint.Location = new Point(510, 20);
    butPrint.Text = "Print";
    butPrint.Click += new System.EventHandler(this.butPrint_Click);
    this.Controls.Add(butPrint);

    // add printpage handler
    this.printdocument1.PrintPage += new PrintPageEventHandler(this.printdocument1_PrintPage);
  }

  private void makeSomeGraphics(Graphics g)
  {
    g.Clear(Color.White);
    //g.SmoothingMode = SmoothingMode.AntiAlias;

    g.PageUnit = GraphicsUnit.Millimeter;
    g.PageScale = 1.0f;

    Pen thinPenBlack = new Pen(Brushes.Black, 0.1f);  // penWidth = 0.1mm
    Pen thinPenRed = new Pen(Brushes.Red, 0.1f);      // penWidth = 0.1mm
    Pen thickPenRed = new Pen(Brushes.Red, 0.5f);     // penWidth = 0.5mm

    float y = 20.0f;
    thinPenBlack.DashStyle = DashStyle.Solid;
    g.DrawLine(thinPenBlack, 10, y, 110, y);

    y = 30.0f;
    // The length of each dash and space in the dash pattern is the product of the element value in the array and the width of the Pen
    // so divide float by penWidth
    float w = thinPenRed.Width;
    thinPenRed.DashPattern = new float[] { 4.0f / w, 1.0f / w, 1.0f / w, 1.0f / w };
    g.DrawLine(thinPenRed, 10, y, 110, y);

    // now, a wider pen
    y = 40.0f;
    w = thickPenRed.Width;
    thickPenRed.DashPattern = new float[] { 4.0f / w, 1.0f / w, 1.0f / w, 1.0f / w };
    g.DrawLine(thickPenRed, 10, y, 110, y);
  }

  private void panel1_Paint(object sender, PaintEventArgs e)
  {
    makeSomeGraphics(e.Graphics);
  }

  private void butPrint_Click(object sender, EventArgs e)
  {
    dialog1.UseEXDialog = true;
    dialog1.Document = printdocument1;
    if (dialog1.ShowDialog() == DialogResult.OK)
    {
      printdocument1.Print();
    }
  }

  private void printdocument1_PrintPage(object sender, PrintPageEventArgs e)
  {
    makeSomeGraphics(e.Graphics);
    e.HasMorePages = false;
  }


}

2 个答案:

答案 0 :(得分:2)

不知何故,如果您将thinPenRed的厚度值更改为任何> = 0.4f,您将在屏幕和打印时获得相同的模式。除以w时,它是否可以成为舍入问题?

答案 1 :(得分:0)

以下是我的程序在编译时显示的内容:enter image description here

抱歉,我明白你在说什么。打印时,图像如下所示:enter image description here

Tim Bourguignon的回答似乎是正确的。