在其中心旋转图形位图 [英] Rotate a Graphics bitmap at its center

查看:27
本文介绍了在其中心旋转图形位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为学校做一个项目,我们需要在不使用 XNA 的情况下用 C# 制作一个基本的自上而下的赛车游戏.首先让我告诉你,到目前为止我们学到的关于编程的东西与制作即使远程看起来像赛车游戏的东西几乎没有关系.没有比数组、循环等更难的了.所以我们没有学习图形或类似的东西.

I am working on a project for school, we need to make a basic top down race game in C# without using XNA. First of all let me tell you that the stuff we have learned about programming so far has little to do with making something that even remotely looks like a racegame. It didn't get any more difficult than array's, loops etc. So we didn't learn about graphics or anything like that.

说了这么多,我遇到了以下问题.

Having said all that I am having the following problem.

我们已经创建了一个 Graphics 对象,然后使用 DrawImage 并使用来自 car.jpg 的位图.

We have created a Graphics object, and then use DrawImage and use a bitmap from a car.jpg.

graphics = e.Graphics;
graphics.RotateTransform(angle);
graphics.DrawImage(car, xPos, yPos, car.Width, car.Height);

然后我们等待一个按键,例如右

Then we wait for a key press e.g Right

case Keys.Right:
  if (angle != 360)
  {
    angle += 10;
  }
  else
  {
    angle = 0;
  }
  this.Refresh();
  break;

我们遇到的问题是旋转的轴心点在左上角.因此,一旦我们将汽车移动到类似于 (20,25) 的位置并开始旋转它,它将使用 (0,0) 作为旋转中心.我们想要实现的是让旋转中心点在我们汽车的中心.

The problem we have is that the pivot point for the rotation is in the top left corner. So as soon as we move the car to something like (20,25) and start to rotate it, it will use (0,0) as the center of rotation. What we want to achieve is to have the center point of rotation at the center of our car.

我们已经尝试寻找改变 RotateTransformcenterXcenterY 的方法,但得出的结论是这不是可能与位图.我们已经为这个问题苦苦挣扎了 2 天多,似乎找不到任何解决方案来实现我们想要的东西.

We have tried looking for ways to change the centerX and centerY of the RotateTransform but have come to the conclusion that this isn't possible with the bitmap. We have been struggling with this problem for over 2 days and can't seem to find any solution for achieving the thing we want.

是不是我们在创建 Graphics 对象时做错了什么,或者是否有完全不同的方法来更改汽车的 centerXcenterY?

Is there something we are doing wrong creating the Graphics object, or is there a totally different way to change centerX and centerY for the car?

推荐答案

要绘制旋转的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 不同,您必须首先调整 dpicode>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 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;
}

并在加载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..

这篇关于在其中心旋转图形位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆