gpt4 book ai didi

c# - 计算GDI+绘图的边界框

转载 作者:行者123 更新时间:2023-11-30 18:10:09 32 4
gpt4 key购买 nike

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

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

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

最佳答案

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

然后你可以这样做:

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

关于c# - 计算GDI+绘图的边界框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1779539/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com