SetWorldTransform和BitBlt不起作用 [英] SetWorldTransform and BitBlt not working

查看:109
本文介绍了SetWorldTransform和BitBlt不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过SetWorldTransform旋转图像,但它无法工作......



以下是我编写的代码。它是最简单的形式,但仍然无法使它工作..提前感谢任何帮助..



I am trying to rotate images via SetWorldTransform, but its not working...

Below is the code i have written. It is in its simplest form, but still cant make it to work.. any help appreciated in advance..

int iAngleInDeg = 90;
HBITMAP hBitmap;
CString m_csBitmap;
m_csBitmap = "C://Aircraft.bmp";
hBitmap = (HBITMAP)LoadImage(NULL,m_csBitmap.GetBuffer(m_csBitmap.GetLength()),IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
CDC sourceDC, destDC;
sourceDC.CreateCompatibleDC( NULL );
destDC.CreateCompatibleDC( NULL );
// Get logical coordinates
BITMAP bm;
::GetObject( hBitmap, sizeof( bm ), &bm );

float radians = radians=(2*3.1416*iAngleInDeg)/360; 
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(CClientDC(NULL), w, h);
HBITMAP hbmOldSource = (HBITMAP)::SelectObject( sourceDC.m_hDC, hBitmap );
HBITMAP hbmOldDest = (HBITMAP)::SelectObject( destDC.m_hDC, hbmResult );
// Draw the background color before we change mapping mode
HBRUSH hbrBack = CreateSolidBrush( RGB(0,0,0) );
HBRUSH hbrOld = (HBRUSH)::SelectObject( destDC.m_hDC, hbrBack );
//destDC.PatBlt( 0, 0, w, h, PATCOPY );
::DeleteObject( ::SelectObject( destDC.m_hDC, hbrOld ) );
// We will use world transform to rotate the bitmap
SetGraphicsMode(destDC.m_hDC, 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.m_hDC, &xform );

	
// Now do the actual rotating - a pixel at a time
destDC.BitBlt(0,0,bm.bmWidth, bm.bmHeight, &sourceDC, 0, 0, SRCCOPY) ;
//now draw the rotated bitmap on final DC, i.e. pDC
pDC->BitBlt(0, 0,bm.bmWidth, bm.bmHeight, &destDC, 0, 0, SRCCOPY);
	
	
// Restore DCs
::SelectObject( sourceDC.m_hDC, hbmOldSource );
::SelectObject( destDC.m_hDC, hbmOldDest );

推荐答案

据我所知,你已经看过文章建议在我回答你之前关于同一件事的问题时(转动一个数字)。所以你必须确保SetWorldTransform和BitBlt工作正常。



这是什么意思不工作???
As I understand, you''ve already look at article suggested in my answer to your previous question about the same thing (rotating a figure). So you HAVE TO BE SURE that SetWorldTransform and BitBlt works fine.

What does it mean "not working"???


通过不工作,我的意思是我已经根据我的理解实现了代码,它应该工作。但是如果运行代码,源DC不会旋转。这就是为什么我写不工作。你能试试代码并建议一个解决方案吗?



谢谢
By not working, i mean that i have implemented the code as according to my understanding and that it should work. But if you run the code, the source DC is not rotated. Thats why I wrote "Not Working". Can you try the code and suggest a solution?

Thanks


当位图变大时会出现问题,减少我的样本中的大小

The Problem happens when the bitmap is to large, reduce the size as in my sample
#define	MAXBLTSIZEX	800	
#define	MAXBLTSIZEY	800	

BitBlt(CDC *pDC, int x, int y, int nWidth, int nHeight, CDC *pSrcDC, int xSrc, int ySrc, DWORD dwRop)
int xFrom,yFrom ;
int xSrcFrom ,ySrcFrom  ;
int w   ;
int h   ;
for (xFrom = x;xFrom < x+nWidth;xFrom   +=  MAXBLTSIZEX)
{
    xSrcFrom    =   xSrc    +   (xFrom - x) ;
    w   =   nWidth  -   (xFrom - x) ;
    if (w > MAXBLTSIZEX)
        w   =   MAXBLTSIZEX ;
    for (yFrom=y;yFrom < y + nHeight;yFrom +=   MAXBLTSIZEY)
    {
        ySrcFrom    =   ySrc    +   (yFrom - y) ;
        h   =   nHeight -   (yFrom - y) ;
        if (h > MAXBLTSIZEY)
            h   =   MAXBLTSIZEY ;
        pDC->BitBlt (xFrom,yFrom,w,h,pSrcDC,xSrcFrom,ySrcFrom,dwRop)    ;
    }

}





问候



Heinz



regards

Heinz


这篇关于SetWorldTransform和BitBlt不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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