计算 GDI 的边界框+绘画

发布于 2024-08-11 22:18:55 字数 1091 浏览 1 评论 0原文

我正在从 MetaFile (emf) 绘制图像,然后在 UserControl 的 OnPaint 中对其应用一些旋转变换。应用这些转换后,我如何计算屏幕坐标中的正常未转换矩形边界框?我需要它能够将旋转图像的大小调整为用户控件的大小。

protected override void OnPaint(PaintEventArgs e)
{
    // rotate around the center of this UserControl
    e.Graphics.TranslateTransform(this.Width / 2.0f, this.Height / 2.0f);
    e.Graphics.RotateTransform(this.Rotation);
    e.Graphics.TranslateTransform(this.Width / -2.0f, this.Height / -2.0f);

    // TODO: now scale so the image so it fits exactly into this UserControl

    // draw the image at the center of this UserControl
    float left = (this.Width - ResourceManager.MyDrawingMetaFile.Width) / 2.0f;
    float top = (this.Height - ResourceManager.MyDrawingMetaFile.Height) / 2.0f;
    e.Graphics.DrawImage(Resources.MyDrawingMetaFile, left, top);
}

这背后的整个想法是,我想在 UserControl 中显示旋转的 .emf 文件,并让 emf 绘图始终填充 UserControl 中的可用空间。也许有更好的方法?

我所追求的填充模式/拉伸模式是 Uniform 和 UniformToFill (就像在 WPF 的 Viewbox 中一样)。电动势不应扭曲,在统一模式下,电动势至少在一维上完全填充用户控件,没有任何内容被裁剪。在 UniformToFill 中,emf 在两个维度上填充 UserControl,如果纵横比不匹配,则 emf 会在一维中被裁剪。

I am drawing an image from MetaFile (emf) and then apply some rotation transformations to it all within the OnPaint of a UserControl. After applying those transformation how can I calculate the normal untransformed rectangular bounding box of this in screen coordinates? I need this to be able to resize the rotated image to the size of the UserControl.

protected override void OnPaint(PaintEventArgs e)
{
    // rotate around the center of this UserControl
    e.Graphics.TranslateTransform(this.Width / 2.0f, this.Height / 2.0f);
    e.Graphics.RotateTransform(this.Rotation);
    e.Graphics.TranslateTransform(this.Width / -2.0f, this.Height / -2.0f);

    // TODO: now scale so the image so it fits exactly into this UserControl

    // draw the image at the center of this UserControl
    float left = (this.Width - ResourceManager.MyDrawingMetaFile.Width) / 2.0f;
    float top = (this.Height - ResourceManager.MyDrawingMetaFile.Height) / 2.0f;
    e.Graphics.DrawImage(Resources.MyDrawingMetaFile, left, top);
}

The whole idea behind this is that I want to display rotated .emf File in a UserControl and have the emf drawing allways fill the available space in the UserControl. Maybe there is a better approach?

The fillmode/stretchmode I am after is Uniform and UniformToFill (like in WPF's Viewbox). The emf should not be distorted an in Uniform mode the emf completely fills the usercontrol at least in one dimension, nothing is cropped. In UniformToFill the emf filles the UserControl in both dimensions and if the aspectratios do not match, the emf is cropped in one dimension.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹉夏雨初晴づ 2024-08-18 22:18:55

如果我理解正确的话 - 您需要弄清楚旋转如何影响图像的边界框,以便您可以相应地缩放它。

然后你可以这样做:

  1. 将边界框的四个坐标填充到 Point[] 中。
  2. 通过旋转 (.RotateAt) 设置一个矩阵,
  3. 让矩阵变换四个点。
  4. 对四个变换后的 X 坐标进行排序,并比较新边界框的宽度(排序后的 pts[3].X - pts[0].X)。
  5. 现在您知道如何缩放宽度以实现完美贴合。
  6. 对于高度也重复步骤 4。

If I understand you correctly - you need to figure out how the rotation affects the bounding box of your image, so that you can scale it accordingly.

Then you can do like this:

  1. Stuff the four coordinates of your bounding box in a Point[].
  2. Set up a Matrix with your rotation (.RotateAt)
  3. Let the matrix transform the four points.
  4. Sort the four transformed X coordinates and compare the width of the new bounding box (pts[3].X - pts[0].X after sort).
  5. Now you know how to scale the width for a perfect fit.
  6. Repeat step 4 for the height as well.
乖乖哒 2024-08-18 22:18:55

使用 GDI 的方式是:

BeginPath()
// Draw stuff
EndPath()
PathToRegion()
GetRgnBox()

GDI+ 具有等效项 - GraphicsPath 和 Region 类可以执行上述操作

The way it would be done with GDI is :

BeginPath()
// Draw stuff
EndPath()
PathToRegion()
GetRgnBox()

GDI+ has equivalents - The GraphicsPath and Region classes can do the above

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文