Windows Print Metric/Unit Systems in System.Printing and System.Drawing.Priting

本文探讨了Windows打印系统的两个层面:GDI系统中的度量单位体系,用于管理视觉元素;设备独立的WPF视觉系统,使用物理单位进行测量。此外,还介绍了打印机设备系统及其与像素系统的关系,并提供了一个匹配页面大小的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

There are two worlds in the windows priting system. 

 

One is the metric/unit system in the GDI system, in which visuals, text, pictures are managed, manipulated, transformed in memory and rendered by the Video card. 

 

With advent of WPF, a device independant visual system is unified. with this system, unit/metrics are measure in terms of physical unit, such as inch, millimeter.... The namespace which is related to printing in WPF world is System.Drawing.Printing;

 

While the other system is the printer device system, on which resolution, quality, fidelity varies across device to device. On this system, it is more often used that the system of pixels. The namespace is System.Printing;

 

 

 

So given a PageMediaSize (from System.Printing), and PaperSize (from System.Drawing.Printing), there is no wonder that you may do the following transformation if you want to find a match PaperSize for a given PagemediaSize.

 

 

    //@Comment: find a matching PageSize given a list of PageSize and one System.Drawing.Print.PageMediaSize
    //PrintQueue.UserTicket.PageMediaSize --> PaperSize -- consumed by --> VisualPaginator
    //@param rotated: this is set to true if the PaperSize returned is a rotated one of the param pagemediaSize;
    public static PaperSize FindMatchingPageMediaSize(PageMediaSize pageMediaSize, IList<PaperSize> paperSizes, out bool rotated)
    {
      if (pageMediaSize == null) throw new ArgumentNullException("pageMediaSize");
      if (paperSizes == null) throw new ArgumentNullException("paperSizes");

      rotated = false;
      
      var widthInInch = Math.Round(pageMediaSize.Width.Value / 96 * 100);
      var heightInInch = Math.Round(pageMediaSize.Height.Value / 96 * 100);

      var paperSize = paperSizes.FirstOrDefault(p => p.Width == widthInInch && p.Height == heightInInch);
      if (paperSize == null)
      {
        paperSize = paperSizes.FirstOrDefault(p => p.Width == heightInInch && p.Height == widthInInch);
        if (paperSize != null) rotated = true;
      }

      if (paperSize != null)
        return paperSize;

      return null;
    }
 

You may wonder why there is 96 in the code, it is just the DPI value that is standard in WPF or Printing?. Dived by 96 then multiple 100 will give you the width that Device Indepedant width (screen width) in WPF.

 

 

there is a good reading on the blog about "Where does 96 DPI come from in Windows?"

Glossary/Acronyms

 

DPI: Dots per inch

PPI: Pixels per inch

Em: unit of measurement

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值