我曾经看过哪个函数有设置倾斜角的参数让我找一下

解决方案 »

  1.   

    rotate a bitmap image--------------------------------------------------------------------------------if you are targeting your application for windows nt alone, then you have access to api functions to help you rotate the bitmap. you can either use world transformation and bitblt() or use plgblt() for rotating the bitmap. a function using the first combination is shown below. if you are targeting multiple platforms, then your task becomes tougher. you could either rotate each pixel in the source bitmap one at a time or directly manipulate the dib bits to get the rotated bitmap. the first method is very slow to the point of being useless and the second is more complex but is fast enough. we cover both these methods below. note that all the functions shown below create a new bitmap ( in one case it creates a dib ). if instead of a new bitmap you want the rotated image to be drawn directly, you will need to modify the functions. function 1: getrotatedbitmapnt() this function is specific to nt and will not work on windows95. it's the simplest and probably the fastest of the three functions listed in this topic. the other two functions share some of the same initial code. all the three functions expect the angle of rotation to be in radians. if you have degrees to begin with you can convert from degrees to radians by using the formula radian = (2*pi *degree)/360 here are the steps that we take to rotate the bitmap. create a couple of device contexts compatible with the display. one of them will be used to hold the source bitmap and the other will hold the destination bitmap (for the rotated image). 
    precompute the cosine and the sine of the angle. this will help save a few microseconds in subsequent computations. 
    compute the bounding rectangle of the rotated image. we use the formula 
    newx = x.cos(angle) + y.sin(angle) 
    newy = y.cos(angle) - x.sin(angle) 
    we assume one of the corners (0,0) to be the center of rotation and therefore need to calculate the new co-ordinates of the other three corners. based on this we can determine the widht and height of the new bitmap. 
    since the rotated image will not occupy the entire area of the new bitmap, we fill the destination bitmap with the background../di2001.jpgr specified through the function argument. 
    since we will use nts support for linear transformation we set the graphic mode of the destination dc to support this. 
    we set up the xform struction and call setworldtransform() to activate the transformation. the setworldtransform() function sets up the stage for bitblt(), which is when the transformation takes place. the algorithm used for the linear transformation is 
    newx = x * em11 + y * em21 + edx 
    newy = x * em12 + y * em22 + edy 
    for rotation em11 and em22 should be the cosine of the rotation angle, em12 should be the sine of the rotation angle and em21 should be -em12. in the dc we are using, since the +ve y direction is downwards, we reverse the signs of em12 and em21. we also set the translation components (edx & edy) so that rotated image fits inside the new bitmap without part of it getting clipped. 
    we finally call bitblt() to do the actual rotation. this call in itself looks like it will simply copy the image. however, the previous call to setworldtransform() causes the image to be rotated. // getrotatedbitmapnt - create a new bitmap with rotated image
    // returns - returns new bitmap with rotated image
    // hbitmap - bitmap to rotate
    // radians - angle of rotation in radians
    // clrback - color of pixels in the resulting bitmap that do
    //   not get covered by source pixels
    hbitmap getrotatedbitmapnt( hbitmap hbitmap, float radians, colorref clrback )
    {
    // create a memory dc compatible with the display
    cdc sourcedc, destdc;
    sourcedc.createcompatibledc( null );
    destdc.createcompatibledc( null ); // get logical coordinates
    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(cclientdc(null), w, h); hbitmap hbmoldsource = (hbitmap)::selectobject( sourcedc.m_hdc, hbitmap );
    hbitmap hbmolddest = (hbitmap)::selectobject( destdc.m_hdc, hbmresult ); // draw the background../di2001.jpgr before we change mapping mode
    hbrush hbrback = createsolidbrush( clrback );
    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 ); // restore dcs
    ::selectobject( sourcedc.m_hdc, hbmoldsource );
    ::selectobject( destdc.m_hdc, hbmolddest ); return hbmresult;
    }