VC MFC GDI 位图旋转算法.docx
- 文档编号:752791
- 上传时间:2022-10-12
- 格式:DOCX
- 页数:15
- 大小:19.53KB
VC MFC GDI 位图旋转算法.docx
《VC MFC GDI 位图旋转算法.docx》由会员分享,可在线阅读,更多相关《VC MFC GDI 位图旋转算法.docx(15页珍藏版)》请在冰豆网上搜索。
VCMFCGDI位图旋转算法
VCMFCGDI位图旋转算法
2010-04-3019:
39
Function1
newx=x.cos(angle+y.sin(angle
newy=y.cos(angle-x.sin(angle
是以位图左上角为原点逆时针旋转X弧度
newx=x.cos(angle-y.sin(angle
newy=y.cos(angle+x.sin(angle
是以位图左上角为原点顺时针旋转X弧度
Herearethestepsthatwetaketorotatethebitmap.
1.Createacoupleofdevicecontextscompatiblewiththedisplay.Oneofthemwillbeusedtoholdthesourcebitmapandtheotherwillholdthedestinationbitmap(fortherotatedimage.
2.Precomputethecosineandthesineoftheangle.Thiswillhelpsaveafewmicrosecondsinsubsequentcomputations.
3.Computetheboundingrectangleoftherotatedimage.Weusetheformula
newx=x.cos(angle+y.sin(angle
newy=y.cos(angle-x.sin(angle
Weassumeoneofthecorners(0,0tobethecenterofrotationandthereforeneedtocalculatethenewco-ordinatesoftheotherthreecorners.Basedonthiswecandeterminethewidhtandheightofthenewbitmap.
4.Sincetherotatedimagewillnotoccupytheentireareaofthenewbitmap,wefillthedestinationbitmapwiththebackgroundcolorspecifiedthroughthefunctionargument.
5.SincewewilluseNTssupportforlineartransformationwesetthegraphicmodeofthedestinationDCtosupportthis.
6.WesetuptheXFORMstructionandcallSetWorldTransform(toactivatethetransformation.TheSetWorldTransform(functionsetsupthestageforBitBlt(,whichiswhenthetransformationtakesplace.Thealgorithmusedforthelineartransformationis
newx=x*eM11+y*eM21+eDx
newy=x*eM12+y*eM22+eDy
ForrotationeM11andeM22shouldbethecosineoftherotationangle,eM12shouldbethesineoftherotationangleandeM21shouldbe-eM12.IntheDCweareusing,sincethe+veydirectionisdownwards,wereversethesignsofeM12andeM21.Wealsosetthetranslationcomponents(eDx&eDysothatrotatedimagefitsinsidethenewbitmapwithoutpartofitgettingclipped.
7.WefinallycallBitBlt(todotheactualrotation.Thiscallinitselflookslikeitwillsimplycopytheimage.However,thepreviouscalltoSetWorldTransform(causestheimagetoberotated.
//GetRotatedBitmapNT-Createanewbitmapwithrotatedimage
//Returns-Returnsnewbitmapwithrotatedimage
//hBitmap-Bitmaptorotate
//radians-Angleofrotationinradians
//clrBack-Colorofpixelsintheresultingbitmapthatdo
//notgetcoveredbysourcepixels
HBITMAPGetRotatedBitmapNT(HBITMAPhBitmap,floatradians,COLORREFclrBack
{
//CreateamemoryDCcompatiblewiththedisplay
CDCsourceDC,destDC;
sourceDC.CreateCompatibleDC(NULL;
destDC.CreateCompatibleDC(NULL;
//Getlogicalcoordinates
BITMAPbm;
:
:
GetObject(hBitmap,sizeof(bm,&bm;
floatcosine=(floatcos(radians;
floatsine=(floatsin(radians;
//Computedimensionsoftheresultingbitmap
//Firstgetthecoordinatesofthe3cornersotherthanorigin
intx1=(int(bm.bmHeight*sine;
inty1=(int(bm.bmHeight*cosine;
intx2=(int(bm.bmWidth*cosine+bm.bmHeight*sine;
inty2=(int(bm.bmHeight*cosine-bm.bmWidth*sine;
intx3=(int(bm.bmWidth*cosine;
inty3=(int(-bm.bmWidth*sine;
intminx=min(0,min(x1,min(x2,x3;
intminy=min(0,min(y1,min(y2,y3;
intmaxx=max(0,max(x1,max(x2,x3;
intmaxy=max(0,max(y1,max(y2,y3;
intw=maxx-minx;
inth=maxy-miny;
//Createabitmaptoholdtheresult
HBITMAPhbmResult=:
:
CreateCompatibleBitmap(CClientDC(NULL,w,h;
HBITMAPhbmOldSource=(HBITMAP:
:
SelectObject(sourceDC.m_hDC,hBitmap;
HBITMAPhbmOldDest=(HBITMAP:
:
SelectObject(destDC.m_hDC,hbmResult;
//Drawthebackgroundcolorbeforewechangemappingmode
HBRUSHhbrBack=CreateSolidBrush(clrBack;
HBRUSHhbrOld=(HBRUSH:
:
SelectObject(destDC.m_hDC,hbrBack;
destDC.PatBlt(0,0,w,h,PATCOPY;
:
:
DeleteObject(:
:
SelectObject(destDC.m_hDC,hbrOld;
//Wewilluseworldtransformtorotatethebitmap
SetGraphicsMode(destDC.m_hDC,GM_ADVANCED;
XFORMxform;
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;
//Nowdotheactualrotating-apixelatatime
destDC.BitBlt(0,0,bm.bmWidth,bm.bmHeight,&sourceDC,0,0,SRCCOPY;
//RestoreDCs
:
:
SelectObject(sourceDC.m_hDC,hbmOldSource;
:
:
SelectObject(destDC.m_hDC,hbmOldDest;
returnhbmResult;
}
Function2:
GetRotatedBitmap(usingGetPixel&SetPixel
Thisfunctionismoreforeducationpurpose.Itdoesworkbutittakesafewsecondstofinish.Itmightevenbeusefulforverysmallbitmaps.
Here'swhatthefunctiondoes:
1.Createacoupleofdevicecontextstoworkwith.
2.Computestheboundingrectangleoftherotatedimageandfillsitwiththebackgroundcolor.
3.Wesetthemappingmodeinboththedevicecontextssothatthe+vedirectionofthey-axisgoesup.Wealsoset
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- VC MFC GDI 位图旋转算法 位图 旋转 算法