2

I'm new here so I hope my message will be clearly editted...

I tried to show you a very explaining picture here, but new users are not allowed to post images. Well, in words then: I draw 3 lines of exactly 10 centimeter when printed on paper or to PDF. However on screen, the middle line should look the same as the lowest line. The only difference between them is the Width property. The first red line .Width is 0.1 millimeter and the second red line .Width is 0.5 millimeter.

I give both red lines a DashPattern of: 4mm dash- 1mm space - 1mm dash - 1mm space.

As I wrote; when printed, the Dash pattern is exactly the same on the red lines! I think it's a bug when the graphics are shown on screen, but maybe I'm missing something... Below you'll find the complete code for a C# sample project to copy/paste.

Thanks in advance!

Paul

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;
  }


}
Paul H.
  • 21
  • 3

2 Answers2

2

Somehow, if you change the thickness value of the thinPenRed to anything >= 0.4f, you will get the same pattern both on screen and on print. Can it be a rounding issue when dividing by w?

Timothée Bourguignon
  • 2,190
  • 3
  • 23
  • 39
0

Here is what your program showed when i compiled it:enter image description here

Sorry, i see what you are talking about. When printed, image looks like this:enter image description here

Tim Bourguignon's answer seems correct.

weegth
  • 41
  • 3
  • Now if you try to print it (as a pdf for instance) you will notice that the two red lines have the exact same pattern on the doc, the second one being thicker than the first. – Timothée Bourguignon Dec 07 '11 at 11:01
  • Thanks Tim for the pictures. I was not allowed (yet) to upload them. Indeed it's strange that a penwidth of 0.395 millimeter acts completely different from a penwidth of 0.4mm (on screen!). The reason that I want to use millimeters is that the lineresults are meant for printing. On screen I want WYSIWYG... – Paul H. Dec 07 '11 at 14:44
  • Hmm... A not so very nice workaround could be to divide the g.PageScale by 10 (and set it to 0.1 instead of 1.0) and to multiply every millimeter value by 10. – Paul H. Dec 07 '11 at 15:52