RasterIO的用法参考:
在MFC项目中添加一个对话框DlgFastShow,添加一个按钮 ,在头文件中添加如下代码:
1 public: 2 GDALDataset* m_pDataset; 3 double m_dScale; //现有图框与图像的比值 4 5 int m_iMinx; 6 int m_iMiny; 7 int m_iMaxx; 8 int m_iMaxy; 9 void ShowRaster();
在实现文件中添加如下代码:
1 void DlgFastShow::OnBnClickedBtnOpenimage() 2 { 3 CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,_T("IMG影像文件(*.img)|*.img|TIFF影像文件(*.tif)|*.tif||"),AfxGetMainWnd()); 4 CString str; 5 if (dlg.DoModal()==IDOK) 6 { 7 str=dlg.GetPathName(); 8 GDALAllRegister(); 9 CPLSetConfigOption("GDAL_FILENAME_IS_UTF8","NO"); 10 const char* pszFile =(LPCTSTR)str; 11 12 //使用只读方式打开图像 13 m_pDataset = (GDALDataset*)GDALOpen(pszFile,GA_ReadOnly); 14 if( m_pDataset == NULL ) 15 { 16 CString ex; 17 ex.AppendFormat("File: %s不能打开!\n",str); 18 } 19 double padfTransform[6] = {0.0}; 20 m_pDataset->GetGeoTransform(padfTransform); 21 ShowRaster(); 22 } 23 } 24 void DlgFastShow::ShowRaster() 25 { 26 int iStartCol = 0; 27 int iStartRow = 0; 28 int dataWidth = m_pDataset->GetRasterXSize();//spinEndCol->value() - iStartCol; 29 int dataHeight =m_pDataset->GetRasterYSize();// spinEndRow->value() - iStartRow; 30 int dataBands = m_pDataset->GetRasterCount(); 31 CString ex; 32 ex.AppendFormat("行列数目: %d %d %d",dataWidth,dataHeight,dataBands); 33 int band_list[3] = {3,2,1}; 34 35 m_dScale = dataHeight > dataWidth ? dataHeight : dataWidth; 36 int iViewHeight = 478; 37 m_dScale = iViewHeight/m_dScale; 38 39 int iSize = GDALGetDataTypeSize(GDT_Byte) / 8;//以字节为单位 40 int iScaleWidth = static_cast<int>(dataWidth*m_dScale+0.5); 41 int iScaleHeight = static_cast<int>(dataHeight*m_dScale+0.5); 42 ex.AppendFormat("缓存数目: %d %d",iScaleWidth,iScaleHeight); 43 iScaleWidth = (iScaleWidth*8+31)/32*4; 44 ex.AppendFormat("取整数目: %d ",iScaleWidth); 45 AfxMessageBox(ex); 46 unsigned char* pBuffer = new unsigned char[iScaleWidth*iScaleHeight*3]; 47 CPLErr err = m_pDataset->RasterIO(GF_Read, 0, 0,dataWidth, dataHeight, pBuffer, iScaleWidth, iScaleHeight,GDT_Byte, 3, band_list, iSize*3, iSize*iScaleWidth*3, iSize); //读取3个波段的数据 48 49 unsigned char* pDataBuffer = NULL; 50 if (dataBands >=3 ) 51 { 52 pDataBuffer = pBuffer; 53 } 54 else 55 { 56 pDataBuffer = new unsigned char[iScaleWidth*iScaleHeight*3]; 57 for (int i=0; i<iScaleWidth*iScaleHeight*3; i++) 58 pDataBuffer[i] = pBuffer[i/3]; 59 delete []pBuffer; 60 } 61 62 /*CBitmap bitmap; 63 int flag=bitmap.CreateBitmap(iScaleWidth, iScaleHeight,1,24,pDataBuffer );*/ 64 //int flag=bitmap.CreateCompatibleBitmap(iScaleWidth, iScaleHeight,1,32,pDataBuffer ); 65 /*if (flag==0) 66 { 67 AfxMessageBox("创建位图失败!"); 68 }*/ 69 //HBITMAP hBmp; // 保存CBitmap加载的位图的句柄 70 //hBmp = (HBITMAP)bitmap.GetSafeHandle(); // 获取bitmap加载位图的句柄 71 72 //picImage.SetBitmap(hBmp); 73 //CClientDC dc(this); 74 //BITMAP bm; 75 //bitmap.GetObject(sizeof (BITMAP),&bm); 76 //CDC dcMem; 77 //dcMem.CreateCompatibleDC(&dc); 78 //CBitmap *pOldbmp=dcMem.SelectObject(&bitmap); 79 //CDC *pDC=&dc; 80 //pDC->BitBlt(0,0,bm.bmWidth,bm.bmHeight,&dcMem,0,0,SRCCOPY); 81 //dcMem.SelectObject(pOldbmp); 82 83 84 CDC dc; 85 CClientDC cdc(this); 86 87 dc.Attach(cdc.m_hDC); 88 89 CDC memDC; 90 memDC.CreateCompatibleDC(&dc); 91 92 CBitmap bmp; 93 bmp.CreateCompatibleBitmap(&dc,500,500); 94 memDC.SelectObject(&bmp); 95 96 BITMAPINFO bmpInfo; 97 bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 98 bmpInfo.bmiHeader.biWidth = iScaleWidth; 99 bmpInfo.bmiHeader.biHeight = -iScaleHeight; 100 //bmpInfo.bmiHeader.biWidth = 487; 101 //bmpInfo.bmiHeader.biHeight = -500; 102 bmpInfo.bmiHeader.biPlanes = 1; 103 bmpInfo.bmiHeader.biBitCount = 24;//24位色 104 bmpInfo.bmiHeader.biCompression = BI_RGB; 105 bmpInfo.bmiHeader.biSizeImage = 0; 106 bmpInfo.bmiHeader.biXPelsPerMeter = 3000; 107 bmpInfo.bmiHeader.biYPelsPerMeter = 3000; 108 bmpInfo.bmiHeader.biClrUsed = 0; 109 bmpInfo.bmiHeader.biClrImportant = 0; 110 111 //每行字节数,4字节对齐 112 /*long nLnBytes = (487+3)/4*4*3; 113 114 BYTE *pData = new BYTE[nLnBytes*500]; 115 memset(pData,0,nLnBytes*500); 116 for(int i=10; i<90; i++) 117 { 118 pData[50*nLnBytes+i*3]=255; 119 pData[i*nLnBytes+50*3+2]=255; 120 }*/ 121 122 SetDIBits(dc.m_hDC,bmp,0,iScaleHeight,pDataBuffer,&bmpInfo,DIB_RGB_COLORS); 123 //delete []pData; 124 cdc.BitBlt(0,0,500,500,&memDC,0,0,SRCCOPY); 125 delete []pDataBuffer; 126 127 }
存在问题:屏幕一刷新,绘制的图像就没有了。
主要DDB(设备相关位图)和DIB(设备无关位图)的区别。采用了CreateCompatibleBitmap将byte数组创建为位图,采用CreateBitmap始终创只能建黑白图像。