rtWhy the bitmap just don't show?!
-
I try to create and show a bitmap based on one short array.No error occours during compile,but the bitmap just don't show on the view. My code as follow: void CReadfile1View::DrawDIBSection(HDC hDC , int xDest,int yDest,int w, int h,signed short*image) { BITMAPINFOHEADER *bih=0; // HDC hDC=0; HBITMAP hbm; void *bits; try { // allocate room for BITMAPINFOHEADER and color mask's if(!(bih = (BITMAPINFOHEADER*)malloc(sizeof(BITMAPINFOHEADER)))) throw 0; ZeroMemory(bih,sizeof(BITMAPINFOHEADER)); bih->biSize = sizeof(BITMAPINFOHEADER); bih->biWidth = w; // . bih->biHeight = h; // match array-size bih->biPlanes = 1; bih->biBitCount = 16; bih->biCompression = BI_RGB; // we use 5:5:5 format bih->biSizeImage = bih->biWidth * 2 * bih->biHeight; if(!(hDC = ::GetDC(0))) throw 0; // create the 16-bit DIBSECTION if(!(hbm = ::CreateDIBSection(hDC,(BITMAPINFO*)bih,DIB_RGB_COLORS,&bits,0,0))) // throw 0; // get the array into the DIBSECTION if(::SetDIBits(hDC,hbm,0,h,image,(BITMAPINFO*)bih, DIB_RGB_COLORS) != h) throw 0; } catch(...) { // cout<<"unknown type of exception throw"<<'\n'; } HDC hDCMem=::CreateCompatibleDC(hDC); HGDIOBJ hBmpOld=::SelectObject(hDCMem,hbm); BitBlt(hDC,xDest,yDest,bih->biWidth,bih->biHeight,hDCMem,0,0,SRCCOPY); ::SelectObject(hDCMem, hBmpOld); ::DeleteDC(hDCMem); if(hDC) ::ReleaseDC(0,hDC); if(bih) free(bih); } void CReadfile1View::OnDraw(CDC* pDC) { CReadfile1Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); DrawDIBSection(*pDC,10,10,WIDTH,HEIGHT,pDoc->Image[0]); } Who can tell me why it don't works and how to improve?! thanx! :eek: :eek: fly against the wind
-
I try to create and show a bitmap based on one short array.No error occours during compile,but the bitmap just don't show on the view. My code as follow: void CReadfile1View::DrawDIBSection(HDC hDC , int xDest,int yDest,int w, int h,signed short*image) { BITMAPINFOHEADER *bih=0; // HDC hDC=0; HBITMAP hbm; void *bits; try { // allocate room for BITMAPINFOHEADER and color mask's if(!(bih = (BITMAPINFOHEADER*)malloc(sizeof(BITMAPINFOHEADER)))) throw 0; ZeroMemory(bih,sizeof(BITMAPINFOHEADER)); bih->biSize = sizeof(BITMAPINFOHEADER); bih->biWidth = w; // . bih->biHeight = h; // match array-size bih->biPlanes = 1; bih->biBitCount = 16; bih->biCompression = BI_RGB; // we use 5:5:5 format bih->biSizeImage = bih->biWidth * 2 * bih->biHeight; if(!(hDC = ::GetDC(0))) throw 0; // create the 16-bit DIBSECTION if(!(hbm = ::CreateDIBSection(hDC,(BITMAPINFO*)bih,DIB_RGB_COLORS,&bits,0,0))) // throw 0; // get the array into the DIBSECTION if(::SetDIBits(hDC,hbm,0,h,image,(BITMAPINFO*)bih, DIB_RGB_COLORS) != h) throw 0; } catch(...) { // cout<<"unknown type of exception throw"<<'\n'; } HDC hDCMem=::CreateCompatibleDC(hDC); HGDIOBJ hBmpOld=::SelectObject(hDCMem,hbm); BitBlt(hDC,xDest,yDest,bih->biWidth,bih->biHeight,hDCMem,0,0,SRCCOPY); ::SelectObject(hDCMem, hBmpOld); ::DeleteDC(hDCMem); if(hDC) ::ReleaseDC(0,hDC); if(bih) free(bih); } void CReadfile1View::OnDraw(CDC* pDC) { CReadfile1Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); DrawDIBSection(*pDC,10,10,WIDTH,HEIGHT,pDoc->Image[0]); } Who can tell me why it don't works and how to improve?! thanx! :eek: :eek: fly against the wind
Hi, I didn't have much time to check your code completely, but I think the source line: if(!(hDC = ::GetDC(0))) throw 0; // create the 16-bit DIBSECTION is incorrect. You re-initialize the local parameter hDC, which was sent by the call to your DrawDIBSection(..) function in the OnDraw method. It will be set to a DC for the desktop, by ::GetDC(0). This hDC is used for the BitBlt(..) command later. Not a sollution, but just a hint...:) Istvan Eperjesy