【问题标题】:How to change the origin for rotation in VB.net如何在 VB.net 中更改旋转原点
【发布时间】:2020-10-21 07:00:04
【问题描述】:

我正在尝试使用以下命令旋转一个名为 player1 的图片框

  e.Graphics.RotateTransform(angle)

e.Graphics.DrawImage(BMP, New Point(-player1.Width \ 2, -player1.Height \ 2))

player1.Refresh()

但是,它似乎将我绘制的图片放在图片框左上角附近的图片框之外,我相信那是当前的原点。此外,它仅围绕位于左上角的原点旋转。 我想将图片框的旋转点/原点设置为中心。谢谢!

【问题讨论】:

  • 您不是在旋转 PictureBox,而是在尝试旋转在其图形上下文中绘制的图像。如前所述,使用Matrix.RotateAt(),处理起来更简单。从那里删除player1.Refresh():当鼠标移动时,你需要Invalidate()你的PictureBox。您应该提及 PictureBox 的 SizeMode(如果它是 StretchImageZoom)。
  • 参见,例如。在这里:Zoom and translate an Image from the mouse location。 TrackBar 定义旋转角度。查看使用Matrix.RotateAt() 旋转、缩放和平移该位图需要多少时间(canvas_Paint() 中需要多少行代码来生成所有这些 效果 -> canvas 是一个图片框)。
  • 感谢您告知我有关 Matrix.RotateAt() 命令的信息。如果我删除 player1.Refresh() 一切都停止移动。另外,您所说的 Invalidate() 是什么意思?谢谢
  • player1.Invalidate(),将插入符号放在Invalidate() 部分,然后按F1。我还链接了不止一处的代码。
  • 你真的应该多读一点 GDI+ 的工作原理。 Invalidate 方法告诉系统控件的哪个区域在下一个Paint 事件中被重新绘制。这个想法是,当您想要更改绘图时,您更改表示绘图的数据,然后调用Invalidate。然后将Paint 事件排入队列,并在事件处理程序中进行绘图。如果您想立即引发Paint 事件,您可以调用UpdateRefresh 方法只调用 InvalidateUpdate。为什么要在 Paint 事件处理程序中强制使用 Paint 事件?

标签: vb.net


【解决方案1】:

我想将图片框的旋转点/原点设置为 中心。

在旋转之前使用TranslateTransform

e.Graphics.TranslateTransform(player1.Width / 2, player1.Height / 2)
e.Graphics.RotateTransform(angle)
e.Graphics.DrawImage(BMP, New Point(-player1.Width \ 2, -player1.Height \ 2))

根据您之前使用e.Graphics 执行的操作,有时您必须ResetTransform() 将事情恢复到在您翻译/旋转/绘制之前在 Paint() 事件开始时的方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    • 1970-01-01
    • 2016-07-31
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多