更新时间:2023-02-02 17:37:08
要绘制旋转的Bitmap
,您需要做几个步骤来准备 Graphics
对象:
To draw a rotated Bitmap
you need to do a few steps to prepare the Graphics
object:
Bitmap
Graphics
这需要为每个位图完成.
This needs to be done for each bitmap.
以下是在 (xPos, yPos
) 位置绘制 Bitmap bmp
的代码步骤:
Here are the steps in code to draw a Bitmap bmp
at position (xPos, yPos
):
float moveX = bmp.Width / 2f + xPos;
float moveY = bmp.Height / 2f+ xPosf;
e.Graphics.TranslateTransform(moveX , moveY );
e.Graphics.RotateTransform(angle);
e.Graphics.TranslateTransform(-moveX , -moveY );
e.Graphics.DrawImage(bmp, xPos, yPos);
e.Graphics.ResetTransform();
有一种可能的复杂情况:如果您的 Bitmap
具有与屏幕不同的 dpi
分辨率,即与 Graphics
不同,您必须首先调整 dpi
code>Bitmap的dpi
设置!
There is one possible complication: If your Bitmap
has different dpi
resolution than the screen i.e. than the Graphics
you must first adapt the Bitmap
's dpi
setting!
要使 Bitmap
适应通常的 96dpi
,您只需执行
To adapt the Bitmap
to the usual 96dpi
you can simply do a
bmp.SetResolution(96,96);
为了为将来的类似视网膜显示做好准备,您可以创建一个在启动时设置的类变量:
To be prepared for future retina-like displays you can create a class variable you set at startup:
int ScreenDpi = 96;
private void Form1_Load(object sender, EventArgs e)
{
using (Graphics G = this.CreateGraphics()) ScreenDpi = (int)G.DpiX;
}
并在加载Bitmap
后使用:
bmp.SetResolution(ScreenDpi , ScreenDpi );
像往常一样,DrawImage
方法使用Bitmap
的左上角.您可能需要为旋转点使用不同的 Points
,也可能需要为您的汽车的虚拟位置使用不同的 Points
,也许在它的前部中间..
As usual the DrawImage
method uses the top left corner of the Bitmap
. You may need to use different Points
for the rotation point and possibly also for the virtual position of your car, maybe in the middle of its front..