图形容器如何工作?

How do graphic containers work?

我想弄清楚 gdi+ 图形容器如何与不同的图形单元一起工作。看看下面的代码。它会编译,您可以将其粘贴到一个全新的表格中。

void Form2_Paint(object sender, PaintEventArgs e)
{
    var gfx = e.Graphics;

    System.Diagnostics.Debug.WriteLine("DpiX={0}, DpiY={1}", gfx.DpiX, gfx.DpiY);

    gfx.PageUnit = GraphicsUnit.Inch;

    var pen = new Pen(Color.Black, 0.01f);

    // Create outer container, 2 inches in size with X and Y set to 0.1 inches
    var outerContainer = gfx.BeginContainer(
        new RectangleF(0.1f, 0.1f, 2, 2),
        new RectangleF(0, 0, 2, 2),
        GraphicsUnit.Pixel);

    // Draw the outer rectangle
    gfx.DrawRectangle(pen, new Rectangle(0, 0, 2, 2));

    // Create inner container, 1 inch in size with X and Y set to 0.1 inches
    var innerContainer = gfx.BeginContainer(
        new RectangleF(0.1f, 0.1f, 1, 1),
        new RectangleF(0, 0, 1, 1),
        GraphicsUnit.Pixel);

    // Draw the inner rectangle
    gfx.DrawRectangle(pen, new Rectangle(0, 0, 1, 1));

    gfx.EndContainer(innerContainer);

    gfx.EndContainer(outerContainer);
}

以上代码是嵌套图形容器的一个非常简单的示例,我没有使用缩放变换。这是使用上述绘制处理程序时表单的样子:

这很简单。现在,我将尝试描述问题所在。

这是BeginContainer方法的签名:

public GraphicsContainer BeginContainer(
    RectangleF dstrect,
    RectangleF srcrect,
    GraphicsUnit unit
)

我不明白的是GraphicsUnit unit论点。

来自 MSDN:

Member of the GraphicsUnit enumeration that specifies the unit of measure for the container.

这似乎不是是真的!

正如您在我的代码中看到的那样,我使用的是英寸单位:gfx.PageUnit = GraphicsUnit.Inch

但是,当我创建容器时,这就是我作为单位参数传递给 BeginContainer 方法的内容:GraphicsUnit.Pixel。创建容器后会发生什么? 正在使用英寸(我真正想要的)。但是,如果我将 GraphicsUnit.Inch(或毫米或 任何东西 传递给参数,则使用 像素 。所以,似乎为了完成我想要的(使用英寸)我必须指定像素?

这对我来说毫无意义。您可以尝试更改上面代码中 BeginContainer 方法中的单位,并观察奇怪的结果。我已经阅读了 MSDN 以及我可以收集到的所有信息,但我仍然一无所知。

我正在编写使用 gdi+ 绘制大量内容的软件,它使用毫米单位进行打印 - 当我开始使用容器时,我很惊讶我显然需要将像素指定为单位。我真的很怀疑任何提到 pixels 的打印代码。一定是我对这件事有很大的误解。

所以,综上所述,我的问题是:此方法中 unit 参数的目的是什么?

不幸的是,据我所知,GDI+ 是 Windows 中记录最差的 API 之一。在网上浏览了一些发现很少有人真正使用它,也没有深入了解你的问题。

更不幸的是,GDI+ API 基本上被直接复制到 .NET Graphics 对象。甚至文档也大部分只是逐字复制。请注意 .NET Graphics.BeginContainer Method (RectangleF, RectangleF, GraphicsUnit) and the Windows Graphics.BeginContainer(const RectF, const RectF, Unit) method 页面之间的相似之处。

更复杂的是,还有 this blog entry 诱人地读着:

Another example is the Save and BeginContainer methods in Graphics class, which perform very differently, yet have the same MSDN documentation that fails to differentiate between the two calls

…但没有详细说明这两种方法实际上有何不同。

现在,综上所述,通过一些实验,我想我已经解码了参数和行为:

  • unit 参数似乎用于指定用于 srcrect 参数的单位
  • dstrect 参数以当前用于 Graphics 对象的单位指定
  • 新容器结束时其 Graphics.PageUnit 设置回默认值 GraphicsUnit.Display

我在文档中找不到上述前两点的任何提示。我只是通过实验和仔细观察才了解到这一点,坦率地说,没有实际文件支持我的结论,我仍然不是 100% 确定。

BeginContainer()方法文档中第三点的暗示:

The graphics state established by the BeginContainer method includes the rendering qualities of the default graphics state; any rendering-quality state changes existing when the method is called are reset to the default values.

这句话乍一看好像是说只有"rendering-quality state changes"被reset了。但是更仔细地阅读,你会发现状态的一部分只是被特别调用,因为它 included 在所有被重置的状态中。当然,更仔细的阅读不会让自己获得任何额外的理解:(,但至少可以看出这句话不应该被当作一切的最后一句话 已重置。


因此,让事情正常工作的关键是在每个步骤中指定您想要使用的单位(例如 GraphicsUnit.Inch):在创建容器之前的初始设置中,在调用 BeginContainer()(但这里只是为了控制 srcrect 被解释的方式),然后最后在 BeginContainer() 之后,再次设置 Graphics.PageUnit 属性。

当我那样做时,我可以使用任何我想绘制的单位。我什至可以混合搭配,当然,与我绘制的矩形相比,这会导致为容器矩形传递一些非直观的值。

例如,这是一个片段,其中我使用英寸表示初始 Graphics 状态,使用毫米表示容器:

gfx.PageUnit = GraphicsUnit.Inch;

using (Pen blackPen = new Pen(Color.Black, 0.01f))
using (Pen redPen = new Pen(Color.Red, 0.01f))
{
    gfx.DrawRectangle(blackPen, .25f, .25f, 2, 2);

    var outerContainer = gfx.BeginContainer(
        new RectangleF(.25f, .25f, 2, 2),
        new RectangleF(0, 0, 2 * 25.4f, 2 * 25.4f),
        GraphicsUnit.Millimeter);

    gfx.PageUnit = GraphicsUnit.Millimeter;
    gfx.DrawRectangle(redPen, .25f * 25.4f, .25f * 25.4f, 1.5f * 25.4f, 1.5f * 25.4f);

    gfx.EndContainer(outerContainer);
}

生成此图像的对象:

所以我可以在容器外绘制一个 2x2 英寸的矩形,然后在容器内绘制一个 1.5x1.5 英寸的矩形,但使用毫米(在参数中明确转换,只是为了我自己更清楚自己在做什么)。

或许,你根本不需要在意。

我也未能将正确的参数传递给 Graphics.BeginContainer。 但是,使用 Transform 方法(最终 ResetTransform )完美地解决了我必须解决的问题。

因此,在尝试理解容器之前,首先考虑 Transform 方法。