计算 GDI 的边界框+绘画
我正在从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确的话 - 您需要弄清楚旋转如何影响图像的边界框,以便您可以相应地缩放它。
然后你可以这样做:
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:
使用 GDI 的方式是:
GDI+ 具有等效项 - GraphicsPath 和 Region 类可以执行上述操作
The way it would be done with GDI is :
GDI+ has equivalents - The GraphicsPath and Region classes can do the above