简体   繁体   English

如何使用毫米单位而不是像素在 DrawingVisual 中绘制某些内容?

[英]How to draw something in DrawingVisual with millimeter unit instead of pixels?

I'm trying to draw something in a System.Windows.Media.DrawingVisual but I need to draw thins in millimeter unit.我试图在System.Windows.Media.DrawingVisual绘制一些东西,但我需要以毫米为单位绘制细线。 How can I do that?我怎样才能做到这一点?

In WPF, you can't even draw something in pixel units without at least some extra effort.在 WPF 中,如果不付出一些额外的努力,您甚至无法以像素为单位绘制某些内容。 WPF uses "device independent units" where each unit is 1/96th of an inch. WPF 使用“设备独立单位”,其中每个单位为 1/96 英寸。 Even that is only a theoretical relationship, as it depends on the display device correctly reporting its resolution, which in turn is dependent on the display, its configuration, and what the user has set eg in the "large fonts" setting (ie in the screen resolution settings, clicking the link that reads "Make text and other items larger or smaller").即使这只是一个理论上的关系,因为它取决于正确报告其分辨率的显示设备,而后者又取决于显示器、其配置以及用户在“大字体”设置中(即在屏幕分辨率设置,单击“放大或缩小文本和其他项目”链接)。

All of these affect WPF's interpretation of the available display resolution information, which in turn affect how WPF chooses to render its "device independent" 1/96th of a inch units.所有这些都会影响 WPF 对可用显示分辨率信息的解释,进而影响 WPF 如何选择以 1/96 英寸为单位呈现其“设备独立”。

The bottom line is that the link commenter Sheridan offered really is the closest you can come to displaying in millimeters, barring a lot of extra work and help from the user.最重要的是,除非用户进行大量额外工作和帮助,否则 Sheridan 提供的链接评论者确实是最接近以毫米显示的链接。 By scaling your input units, intended as millimeters, by the factor value provided (ie 96/ 25.4 …in the expression, you can see the 25.4 to convert from millimeters to inches, then the 96 that converts inches to 1/96ths of an inch), you can convert your millimeters into the 96 dpi units that WPF uses natively.通过按提供的factor值缩放您的输入单位(以毫米为单位)(即96/ 25.4 ...在表达式中,您可以看到25.4将毫米转换为英寸,然后96将英寸转换为 1/96 英寸),您可以将毫米转换为 WPF 本机使用的 96 dpi 单位。

Assuming the display is configured correctly (an optimistic assumption, but it does happen :) ), this will result in reasonably accurate presentation on the screen according to your desired millimeter-based dimensions.假设显示器配置正确(一个乐观的假设,但它确实发生了:)),这将根据您所需的基于毫米的尺寸在屏幕上产生相当准确的显示。


Note that you can accomplish this scaling through the use of a transform on your rendered UI elements.请注意,您可以通过对呈现的 UI 元素使用变换来完成此缩放。 The easiest thing to do would be to set the LayoutTransform property of the outer-most container object where you want the millimeter-based rendering.最简单的方法是设置最外层容器对象的LayoutTransform属性,您希望在其中进行基于毫米的渲染。 Then you can just lay out your objects in that container using the millimeters values for their location and size, and WPF will use the transform to present the container and the rendered objects within at the scale you want.然后,您可以使用毫米值作为对象的位置和大小在该容器中布置对象,WPF 将使用变换以您想要的比例呈现容器和渲染对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM