首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >winapi位图旋转

winapi位图旋转
EN

Stack Overflow用户
提问于 2012-03-17 23:06:29
回答 1查看 3.6K关注 0票数 1

我有一个BitMap (一个小雪碧),我必须在一个特定的角度旋转。我找到了这段代码,我试着将它修改为我的应用程序,但它似乎不起作用。精灵一点也不旋转,而是移动一点点。

下面是函数代码:

代码语言:javascript
复制
void Sprite::Rotate(float radians, const char* szImageFile)
{
    // Create a memory DC compatible with the display
HDC sourceDC, destDC;
sourceDC = CreateCompatibleDC( mpBackBuffer->getDC() );
destDC   = CreateCompatibleDC( mpBackBuffer->getDC() );

// Get logical coordinates
HBITMAP hBitmap = (HBITMAP)LoadImage(g_hInst, szImageFile, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE);
BITMAP bm;
::GetObject( hBitmap, sizeof( bm ), &bm );

float cosine = (float)cos(radians);
float sine = (float)sin(radians);

// Compute dimensions of the resulting bitmap
// First get the coordinates of the 3 corners other than origin
int x1 = (int)(bm.bmHeight * sine);
int y1 = (int)(bm.bmHeight * cosine);
int x2 = (int)(bm.bmWidth * cosine + bm.bmHeight * sine);
int y2 = (int)(bm.bmHeight * cosine - bm.bmWidth * sine);
int x3 = (int)(bm.bmWidth * cosine);
int y3 = (int)(-bm.bmWidth * sine);

int minx = min(0,min(x1, min(x2,x3)));
int miny = min(0,min(y1, min(y2,y3)));
int maxx = max(0,max(x1, max(x2,x3)));
int maxy = max(0,max(y1, max(y2,y3)));

int w = maxx - minx;
int h = maxy - miny;

// Create a bitmap to hold the result
HBITMAP hbmResult = ::CreateCompatibleBitmap(mpBackBuffer->getDC(), w, h);

HBITMAP hbmOldSource = (HBITMAP)::SelectObject( sourceDC, hBitmap );
HBITMAP hbmOldDest = (HBITMAP)::SelectObject( destDC, hbmResult );

// Draw the background color before we change mapping mode
HBRUSH hbrBack = CreateSolidBrush( mcTransparentColor );
HBRUSH hbrOld = (HBRUSH)::SelectObject( destDC, hbrBack );
PatBlt( destDC, 0, 0, w, h, PATCOPY);
::DeleteObject( ::SelectObject( destDC, hbrOld ) );




// We will use world transform to rotate the bitmap
SetGraphicsMode(destDC, GM_ADVANCED);
XFORM xform;
xform.eM11 = cosine;
xform.eM12 = -sine;
xform.eM21 = sine;
xform.eM22 = cosine;
xform.eDx = (float)-minx;
xform.eDy = (float)-miny;

SetWorldTransform( destDC, &xform );

// Now do the actual rotating - a pixel at a time
BitBlt(destDC, 0, 0, w, h, sourceDC, 0, 0, SRCCOPY );
// Restore DCs
::SelectObject( sourceDC, hbmOldSource );
::SelectObject( destDC, hbmOldDest );

BITMAP btm;
 ::GetObject( hbmResult, sizeof( mImageBM ), &mImageBM );

 }

我不知道为什么它不旋转图像,而是它移动它。此外,如果您知道另一种方法,我愿意听取建议,但请记住,我只能使用winapi,不需要使用其他库,而且我是windows编程的初学者。

编辑:这里是我得到代码的地方:http://www.codeguru.com/cpp/g-m/bitmap/specialeffects/article.php/c1743/。我不得不稍微改变一下,但我不认为是因为这个原因,我只是把CDCs换成了HDC。我提出的一件重要的事情是,(0,0)协调。因为代码中的图像是在角落中考虑的,但在我的应用程序中,它位于sprite的中心。即使这是一个问题,我认为这是一个计算位图的新维数的问题,我看不出与旋转的联系。

EN

回答 1

Stack Overflow用户

发布于 2012-03-17 23:22:05

如果位图是移动的,在我看来,世界变换和blit都起作用了。

它的移动量是由于转换的eDx/eDy参数造成的吗?

仅仅对“旋转”示例这里中的值进行硬编码是否会产生影响?正厅是用相反的标志经过的。因此,它可能只是忽略了变换的旋转部分,只进行了平移。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9754589

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档