更新时间:2023-02-02 17:40:43
要画一个旋转位图
你需要做的几个步骤准备图形
目标:
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.
下面是代码的步骤在位置画一个 BMP位图
( XPOS, yPos
):
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();
有一个可能的并发症:如果你的位图
有不同的 DPI
分辨率比屏幕即比图形
您必须首先调整位图
的 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!
要适应位图
常用的 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;
}
和装车后位图$使用C $ C>:
bmp.SetResolution(ScreenDpi , ScreenDpi );
像往常一样,的DrawImage
方法使用顶部在位图
的左上角。您可能需要使用不同的分数
的旋转点,可能也为您的爱车的虚拟位置,也许在其前..
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..