How do I draw a bitmap to a DC, while rotating it by a specified angle?
如何将位图绘制到DC,同时将其旋转指定的角度?
3 个解决方案
#1
I agree with Al - he deserves the answer, but this (admittedly untested) code fragment should do what you asked for:
我同意Al - 他应该得到答案,但是这个(公认的未经测试的)代码片段应该按照你的要求做:
def write_bmp_to_dc_rotated( dc, bitmap, angle ):
'''
Rotate a bitmap and write it to the supplied device context.
'''
img = bitmap.ConvertToImage()
img_centre = wx.Point( img.GetWidth()/2, img.GetHeight()/2 )
img = img.Rotate( angle, img_centre )
dc.WriteBitmap( img.ConvertToBitmap(), 0, 0 )
One thing to note though from the docs:
从文档中可以注意到一件事:
...using wxImage is the preferred way to load images in wxWidgets, with the exception of resources...
...使用wxImage是在wxWidgets中加载图像的首选方法,但资源除外...
Was there a particular reason to load it as a bitmap rather than a wx.Image?
有没有特别的理由将其加载为位图而不是wx.Image?
#2
I'm not sure that this is the best way of doing it, but one option would be to convert it to a wx.Image with ConvertToImage (wxWidgets help) and then use the rotate function (wxWidgets help). You could then (if necessary) convert it back with ConvertToBitmap (wxWidgets help).
我不确定这是最好的方法,但一种选择是使用ConvertToImage(wxWidgets帮助)将其转换为wx.Image,然后使用rotate函数(wxWidgets帮助)。然后,您可以(如有必要)使用ConvertToBitmap(wxWidgets帮助)将其转换回来。
I couldn't see an obvious function that could be used to apply a coordinate transform to the drawing context (DC), but there may be one in there somewhere...
我看不到可以用来将坐标变换应用于绘图上下文(DC)的明显函数,但是某处可能存在...
Hope that helps.
希望有所帮助。
#3
Better way would be to use Graphics context if you want a generic rotation e.g. rotate bitmap or text or any other drawing path
如果你想要一个通用的旋转,更好的方法是使用Graphics上下文,例如旋转位图或文本或任何其他绘图路径
gc = wx.GCDC(dc)
gc.Rotate(angle)
gc.DrawText("anurag", 100, 100)
#1
I agree with Al - he deserves the answer, but this (admittedly untested) code fragment should do what you asked for:
我同意Al - 他应该得到答案,但是这个(公认的未经测试的)代码片段应该按照你的要求做:
def write_bmp_to_dc_rotated( dc, bitmap, angle ):
'''
Rotate a bitmap and write it to the supplied device context.
'''
img = bitmap.ConvertToImage()
img_centre = wx.Point( img.GetWidth()/2, img.GetHeight()/2 )
img = img.Rotate( angle, img_centre )
dc.WriteBitmap( img.ConvertToBitmap(), 0, 0 )
One thing to note though from the docs:
从文档中可以注意到一件事:
...using wxImage is the preferred way to load images in wxWidgets, with the exception of resources...
...使用wxImage是在wxWidgets中加载图像的首选方法,但资源除外...
Was there a particular reason to load it as a bitmap rather than a wx.Image?
有没有特别的理由将其加载为位图而不是wx.Image?
#2
I'm not sure that this is the best way of doing it, but one option would be to convert it to a wx.Image with ConvertToImage (wxWidgets help) and then use the rotate function (wxWidgets help). You could then (if necessary) convert it back with ConvertToBitmap (wxWidgets help).
我不确定这是最好的方法,但一种选择是使用ConvertToImage(wxWidgets帮助)将其转换为wx.Image,然后使用rotate函数(wxWidgets帮助)。然后,您可以(如有必要)使用ConvertToBitmap(wxWidgets帮助)将其转换回来。
I couldn't see an obvious function that could be used to apply a coordinate transform to the drawing context (DC), but there may be one in there somewhere...
我看不到可以用来将坐标变换应用于绘图上下文(DC)的明显函数,但是某处可能存在...
Hope that helps.
希望有所帮助。
#3
Better way would be to use Graphics context if you want a generic rotation e.g. rotate bitmap or text or any other drawing path
如果你想要一个通用的旋转,更好的方法是使用Graphics上下文,例如旋转位图或文本或任何其他绘图路径
gc = wx.GCDC(dc)
gc.Rotate(angle)
gc.DrawText("anurag", 100, 100)