怎么得到图象的数据数组? 那位大虾会!
如题! 问题点数:20、回复次数:4Top
1 楼rockersz(世上的无奈需要我去忍耐...)回复于 2006-03-13 11:10:51 得分 0
如果是位图,用CBitmap::GetBitmapBits()Top
2 楼xzxiao(起点)回复于 2006-03-13 12:39:11 得分 0
建议去sourceforge.org找一些open source的工具,比如CxImage,用它的库,参考里面的demo,很容易得到。Top
3 楼daotong(Day Day Up(天天向上))回复于 2006-03-14 10:16:46 得分 20
HBITMAP hBmp;
CFileDialog dlg(TRUE,"bmp",NULL,0,"bmp位图(*.bmp)|*.bmp||",this);
if(dlg.DoModal() != IDOK)
{
return;
}
hBmp = (HBITMAP)LoadImage(NULL,dlg.GetPathName(),IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
if(hBmp == NULL)
{
return;
}
BITMAP bm;
PBITMAPINFO bmpInf;
if(GetObject(hBmp,sizeof(bm),&bm)==0)
return;
int nPaletteSize=0;if(bm.bmBitsPixel<16)
nPaletteSize=(int)pow(2,bm.bmBitsPixel);//别忘了“#include <math.>”,pow:求方函数
bmpInf=(PBITMAPINFO)LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER)+
sizeof(RGBQUAD)*nPaletteSize);
//-----------------------------------------------
bmpInf->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInf->bmiHeader.biWidth = bm.bmWidth;
bmpInf->bmiHeader.biHeight = bm.bmHeight;
bmpInf->bmiHeader.biPlanes = bm.bmPlanes;
bmpInf->bmiHeader.biBitCount = bm.bmBitsPixel;
bmpInf->bmiHeader.biCompression = BI_RGB;
bmpInf->bmiHeader.biSizeImage = (bm.bmWidth+7)/8*bm.bmHeight*bm.bmBitsPixel;
bmpInf->bmiHeader.biXPelsPerMeter = 0;
bmpInf->bmiHeader.biYPelsPerMeter = 0;
bmpInf->bmiHeader.biClrUsed = 0;
bmpInf->bmiHeader.biClrImportant = 0;
//-----------------------------------------------
HDC hDC = ::GetWindowDC(NULL);if(!::GetDIBits(hDC,hBmp,0,(WORD)bm.bmHeight,NULL,bmpInf,DIB_RGB_COLORS))
{
LocalFree(bmpInf);
::ReleaseDC(NULL,hDC);
return ;
}
void* buf=(void*) new char[bmpInf->bmiHeader.biSizeImage];
if(buf==NULL)
{
::ReleaseDC(NULL,hDC);
LocalFree(bmpInf);
return;
}
if(!::GetDIBits(hDC,hBmp,0,(UINT)bm.bmHeight,buf,bmpInf,DIB_RGB_COLORS))
{
::ReleaseDC(NULL,hDC);
delete []buf;
LocalFree(bmpInf);
return;
}
::ReleaseDC(NULL,hDC);
CClientDC dc(this);
if(bm.bmBitsPixel == 8) //8位色位图
{
RGBQUAD rgbQ;
BYTE *pData = (BYTE*)buf;
int nWidth = bm.bmWidth;
while(nWidth%4 != 0)
{
nWidth++;
}
for(int i=0;i<bm.bmHeight;i++)
{
for(int j=0;j<bm.bmWidth;j++)
{//rgbQ的三个分量就是你要保存的象素数据,存入自己的数组(BYTE类型)
rgbQ = bmpInf->bmiColors[pData[i*nWidth+j]];
dc.SetPixel(j,bm.bmHeight-i,RGB(rgbQ.rgbRed,rgbQ.rgbGreen,rgbQ.rgbBlue));
}
}
}
if(bm.bmBitsPixel == 24) // 24位色位图
{
BYTE *pData = (BYTE*)buf;
int nWidth = bm.bmWidth*3;
while(nWidth%4 != 0)
{
nWidth++;
}
for(int i=0;i<bm.bmHeight;i++)
{
for(int j=0;j<bm.bmWidth;j++)
{//pData[i*nWidth+j*3+2],pData[i*nWidth+j*3+1],pData[i*nWidth+j*3])
//分别是当前象素的红、绿、蓝颜色分量,存入自己的数组
dc.SetPixel(j+20,bm.bmHeight-i,RGB(pData[i*nWidth+j*3+2],
pData[i*nWidth+j*3+1],pData[i*nWidth+j*3]));
}
}
}
delete []buf;
ReleaseDC(&dc);
DeleteObject(hBmp);
LocalFree(bmpInf);
****************************************************
中文注释粘贴上乱码,就给你写了几个主要的注释Top
4 楼daotong(Day Day Up(天天向上))回复于 2006-03-14 10:18:35 得分 0
上边的注释写错了,呵呵
应该是 #include <math.h>Top




