且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在其中心旋转图形位

更新时间:2023-02-02 17:40:43

要画一个旋转位图你需要做的几个步骤准备图形目标:

To draw a rotated Bitmap you need to do a few steps to prepare the Graphics object:


  • 第一,你动它的起源到旋转

  • 的中点,那么你所想要的角度转动

  • 接下来你将其移回

  • 现在,你就可以绘制位图

  • 最后你重置图形

  • first you move its origin onto the midpoint of the rotation
  • then you rotate by the desired angle
  • next you move it back
  • now you can draw the Bitmap
  • finally you reset the 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 Bitmapto 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..