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

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

问题描述

我工作的学校的一个项目,我们需要在C#中的基本自上而下的赛车游戏,而无需使用XNA。
:首先让我告诉你,到目前为止,我们已经了解了编程的东西,没有什么与做的东西,甚至远程看起来像一个racegame。它没有得到任何难度比数组,循环等
所以我们并不了解显卡之类的东西。

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.

我们已经尝试寻找各种方法来修改的centerX centerY RotateTransform 但得出的结论,这是不可能的位图。
,我们一直在努力解决这个问题超过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.

有什么,我们都做错了创建图形对象,或者是有一个完全不同的方式来改变的centerX centerY 的车?

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?

推荐答案

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

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;
}

和装车后位图

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..

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

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