CImage 獲取圖片RGB 、圖片高和寬;


復制代碼

 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     }
復制代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM