1 CImage img , img1 ,imDest; 2 img1.Load( 图片路径); 3 img.Load( 图片路径); 4 为了防止图片失真,先处理一下在把图片显示出来 5 SetStretchBltMode(pDC->m_hDC , HALFTONE); 6 SetBrushOrgEx( pDC->m_hDC , 0, 0, NULL);//第一个参数用什么dc画图就是它的m_hDC;比如\ 7 8 ********************************************************************* 9 10 imDest.Create( pDoc->m_wdOrigWeigh , pDoc->m_wdOrigHeih , 32 , 0 ); 11 SetStretchBltMode(imDest.GetDC(), HALFTONE);//因为要用imDest来获取imDest.GetPixel();所以就填imDest.GetDC(); 12 SetBrushOrgEx(imDest.GetDC(), 0, 0, NULL); 13 img1.StretchBlt(imDest.GetDC(), 0 , 0 , pDoc->m_wdOrigWeigh , pDoc->m_wdOrigHeih , 0 , 0 , nptWith , nPtHeig ,SRCCOPY ); 14 ************************************************************************ 15 img.Draw( pDC->GetSafeHdc() ,rt1.TopLeft().x , rt1.TopLeft().y , rt1.Width() , rt1.Height() , 0 , 0 , rt1.Width() ,rt1.Height() ); 16 17 2.在内存里获取图片的RGB 18 19 GetRGB( CMapDefineView *pDefinedview ,CImage &img , int nPitWide , int nPitHeigh )//注意这里CImage &img传递的是引用,而不是对象,否则会弹出m_hDC== 0的警告 20 21 { 22 int nPtHeig = img1.GetWidth(); 23 int nptWith = img1.GetHeight(); 24 25 imDest.Create( pDoc->m_wdOrigWeigh , pDoc->m_wdOrigHeih , 32 , 0 );//首先要创建一个CImage,规定了图片的大小。
HDC hdc = imDest.GetDC();//下面要用共同获取的hdc,如果每个都直接用imDest.GetDC()的话就要释放一下DC,imDest.ReleaseDC();
SetStretchBltMode(hdc, HALFTONE );//因为要用imDest来获取imDest.GetPixel();所以就填imDest获取的HDC ,为了防止创建的CImage里面图片失真,
SetBrushOrgEx(hdc, 0, 0, NULL);;//为了防止创建的CImage里面图片失真,
imgGetRGB.StretchBlt(hdc, 0 , 0 , pDoc->m_wdOrigWeigh , pDoc->m_wdOrigHeih , 0 , 0 , nptWith , nPtHeig ,SRCCOPY );//把加载的图片按照大小贴进新创建的imDest中。其中 nptWith , nPtHeig 是图片原来的宽度和高度,pDoc->m_wdOrigWeigh , pDoc->m_wdOrigHeih 是目的高度和宽度;
29 30 31 COLORREF colr; 32 int nlen = 0; 33 BYTE *Rbuf = new BYTE[ nPtHeig * nptWith ]; 34 BYTE *Gbuf = new BYTE[ nPtHeig * nptWith ]; 35 BYTE *Bbuf = new BYTE[ nPtHeig * nptWith ]; 36 for (int i = 0 ; i <pDoc->m_wdOrigHeigh; i ++)//注意是nPtHeig 而不是nptWith ,因为这里是一行一行的获取图片RGB 37 38 { 39 40 41 42 for ( int j = 0; j < pDoc->m_wdOrigWeigh; i ++ ) 43 44 45 46 { 47 colr = imDest.GetPixel( j , i );//注意j 和i 不能交换; 48 Rbuf[ nlen ] = GetRValue( colr ) 49 Gbuf[ nlen ] = GetGValue( colr ) 50 Bbuf[ nlen ] = GetBValue( colr ) 51 nlen ++ ; 52 } 53 } 54 }