0

A dashed line has a different count of dashes depending on the resolution of printer. Why isn't it dpi independent? How can I fix it?

var line = new Line();
line.Stroke = Brushes.Black;
line.StrokeThickness = 1;
line.X1 = line.Y1 = 100;
line.X2 = line.Y2 = 200;
line.StrokeDashArray = new DoubleCollection(new[] { 8.0 });

var dialog = new PrintDialog();

if (dialog.ShowDialog() == true)
{
    Size pageSize = new Size(
        dialog.PrintableAreaWidth,  dialog.PrintableAreaHeight);

    line.Measure(pageSize);
    line.Arrange(new Rect(0, 0, pageSize.Width, pageSize.Height));

    dialog.PrintVisual(line, "description");
}

I've used PrintVisual, but you can to create FixedDocument with dashed lines and use PrintDocument. The result will be the same.

Andrey Potapov
  • 29
  • 2
  • 14

1 Answers1

0

WPF is inherently responsive to DPI changes, and pretty much every control (unless specified otherwise) is a vector graphic - I would expect your printer is smart enough to interpret these vector graphics rather than print a dump of what is on your screen.

From my experience with WPF there are at least two options:

Option 1 - Create a bitmap and print it
You can capture WPF controls as bitmap objects, which will be completely unaware of DPI using RenderTargetBitmap and then print them as normal. It is worth noting that the generated bitmap will be different depending on the display resolution/scaling of the system is was generated on & anything you want to capture as a bitmap must be visible on screen (not off screen in a scrollbox etc).

Option 2 - Make your app DPI unaware You probably don't want to use this option, as it will prevent scaling on high resolution displays (There may be a way to achieve this temporarily just for printing, however your printer may ignore this option as it's more of a UI thing, i'm not currently able to test it) but there are several ways to prevent your app from acknowledging DPI all together.

I'm not entirely sure these options will work as I can't currently test them. I would have posted this as a comment if I could. Good luck!

Community
  • 1
  • 1
JLangford
  • 141
  • 6
  • I'm interested why it is so :) **About option 1** I can't to use this advise because I save my vector graphics in FixedDocument and then as XPS file. It's important file size to be smaller and print quality to be same for any page size. **About option 2** This way don't suit me. – Andrey Potapov Mar 15 '17 at 14:39
  • For option 1, you should be able to insert your bitmap into a fixed document. I'm curious as to why option 2 does not suit you if it is not immediately clear to you why WPF apps are DPI responsive in the first place. They're responsive due to the massive range of screen sizes/resolutions that people use, theoretically any WPF app should look crisp and scaled correctly on any display. – JLangford Mar 15 '17 at 15:01