MFC->串口接收数据->显示为位图解决方案
MFC->串口接收数据->显示为位图
最近在研究飞思卡尔的上位机,看了很多上位机,用起来感觉还不错,想自己写一个,多学习一点儿VC,现在串口发送接收都木有问题了,但是如何把串口接收到的8位灰度数据(不包括BMP文件的信息头、调色板)变成位图显示出来呢……也就是说数据在数组里,怎么转化为位图显示出来呢,在这儿卡了好久了,好心人帮帮忙……感激不尽!!
------解决方案--------------------
给点代码参考:
最近在研究飞思卡尔的上位机,看了很多上位机,用起来感觉还不错,想自己写一个,多学习一点儿VC,现在串口发送接收都木有问题了,但是如何把串口接收到的8位灰度数据(不包括BMP文件的信息头、调色板)变成位图显示出来呢……也就是说数据在数组里,怎么转化为位图显示出来呢,在这儿卡了好久了,好心人帮帮忙……感激不尽!!
------解决方案--------------------
给点代码参考:
- C/C++ code
void CDrawbmpView::OnDraw(CDC* pDC) { CDrawbmpDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); pDoc->SetTitle("802D Simulator"); // TODO: add draw code for native data here int hei=508; int wid=648; COLORREF color_table[16]={// 0x00bbggrr ! 0x000000, //0 0x000080, //1 0x008000, //2 0x008080, //3 0x800000, //4 0x800080, //5 0x808000, //6 0x808080, //7 0xC0C0C0, //8 0x0000FF, //9 0x00FF00, //10 0x00FFFF, //11 0xFF0000, //12 0xFF00FF, //13 0xFFFF00, //14 0xFFFFFF };//15 // open CFile if_hnd; if(!if_hnd.Open("snapbmp.dat",CFile::modeRead | CFile::shareExclusive)) { AfxMessageBox("Cann't open image file!"); return; } // get mem unsigned fsize; fsize =if_hnd.GetLength();// BYTE *ScreenPtr1=0; ScreenPtr1=(BYTE *)new char[fsize]; // // read all in if_hnd.Read(ScreenPtr1,fsize);// // image data are in buffer if(if_hnd) if_hnd.Close(); #if 1 //def USE_API // fast way ! // make bitmap info LPBITMAPINFO pBMPinfo; pBMPinfo=(LPBITMAPINFO)new BYTE[sizeof(BITMAPINFOHEADER)+16*sizeof(RGBQUAD)]; ZeroMemory(&pBMPinfo->bmiHeader,sizeof(BITMAPINFOHEADER)); pBMPinfo->bmiHeader.biSize=sizeof(BITMAPINFOHEADER); pBMPinfo->bmiHeader.biBitCount=4; pBMPinfo->bmiHeader.biWidth=648; pBMPinfo->bmiHeader.biHeight=508; pBMPinfo->bmiHeader.biPlanes=1; pBMPinfo->bmiHeader.biSizeImage=648*508/2; // copy colors for(int jj=0; jj<16; jj++) {// reversed ! pBMPinfo->bmiColors[jj].rgbReserved=0; pBMPinfo->bmiColors[jj].rgbRed=(BYTE)(color_table[jj] & 0x0000FF); pBMPinfo->bmiColors[jj].rgbGreen=(BYTE)((color_table[jj] & 0x00FF00) >> 8); pBMPinfo->bmiColors[jj].rgbBlue=(BYTE)((color_table[jj] & 0xFF0000) >> 16); } // draw bmp pDC->SetStretchBltMode(COLORONCOLOR); StretchDIBits(pDC->GetSafeHdc(),0,0,wid,hei,0,0,wid,hei, ScreenPtr1, pBMPinfo, DIB_RGB_COLORS,SRCCOPY); // free delete pBMPinfo; delete [] ScreenPtr1; return; #endif // get mem, slow way ! BYTE *ScreenPtr0=0; ScreenPtr0=(BYTE *)new char[fsize]; // // Vertical flop int BytesOfLine=wid/2;// color 16: 648 pixels=324 bytes for(int kk=0; kk < hei; kk++) { memcpy(ScreenPtr0+kk*BytesOfLine,ScreenPtr1+fsize-(kk+1)*BytesOfLine,BytesOfLine); } // data in ScreenPtr0 now ! if (ScreenPtr1) delete [] ScreenPtr1; // draw bmp int bytepos,x,y,count,ofs; for (bytepos=0,x=0,y=0,count=0; count<hei*wid; count++) { if(!(count & 1)) { ofs=(ScreenPtr0[bytepos]>>4) & 0x0F; } else { ofs=ScreenPtr0[bytepos++] & 0x0F; } SetPixel(pDC->m_hDC,x,y,color_table[ofs]); ++x; if(x>=wid) { x=0; y++; } } if(ScreenPtr0) delete []ScreenPtr0; }