Can you please explain me why this code is not working ?
-
Hai! I am taking a image from a location, converting into byte array and displaying it back on my dialog by converting into bitmap image! HBITMAP hBitMap; hBitMap = (HBITMAP) LoadImage (NULL, \ "c:\\Documents and Settings\\Desktop\\Image\\bitmap1.bmp", \ IMAGE_BITMAP, SM_CXICON, SM_CYICON, LR_LOADFROMFILE); CBitmap bmp; bmp.Attach((HBITMAP)hBitMap); // handle I got from LoadBitmap BITMAP bitmap; bmp.GetBitmap(&bitmap); int size = bitmap.bmHeight*bitmap.bmWidth*bitmap.bmBitsPixel/8; BYTE *lpBits = new BYTE [ size ]; // Here i convert the image to byte array ::GetBitmapBits((HBITMAP)hBitMap,size,lpBits ); // Here i get the handle to the picture control on my dialog HWND hImage = this->GetDlgItem(IDC_STATIC_IMAGE)->GetSafeHwnd(); // I Call the following function to paint the converted byte array SetRawBitsToImage (hImage,bitmap.bmWidth,bitmap.bmHeight,lpBits ,32); //The defination of the above function is // hwnd = handle to the image control // W = width of the raw bits Image // H = height of the raw bits Image // lpBits = pointer to the raw bits image // BitCount = 32,24 etc ( depend on the bit count ) defualt =32 void CMyTryImageDlg:: SetRawBitsToImage (HWND hwnd,int W,int H,BYTE *lpBits, int BitCount) { HDC hDC = ::GetDC(hwnd); ::SetWindowPos(hwnd,0,0,0,W,H,SWP_NOMOVE); BITMAPINFO bi; memset(&bi,0,sizeof(BITMAPINFO)); bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bi.bmiHeader.biWidth = W; bi.bmiHeader.biHeight = H; bi.bmiHeader.biPlanes = 1; bi.bmiHeader.biBitCount = BitCount; bi.bmiHeader.biCompression = BI_RGB; bi.bmiHeader.biSizeImage = bi.bmiHeader.biWidth*bi.bmiHeader.biHeight*BitCount/8; ::SetDIBitsToDevice(hDC, 0,0, W, H, 0, 0, 0,bi.bmiHeader.biHeight, &lpBits,&bi,DIB_RGB_COLORS); ::ReleaseDC(hwnd,hDC); } // But nothing is getting displayed? why? Is there any mistake in code? // I donot have knowledge in images Thanks!
-
Hai! I am taking a image from a location, converting into byte array and displaying it back on my dialog by converting into bitmap image! HBITMAP hBitMap; hBitMap = (HBITMAP) LoadImage (NULL, \ "c:\\Documents and Settings\\Desktop\\Image\\bitmap1.bmp", \ IMAGE_BITMAP, SM_CXICON, SM_CYICON, LR_LOADFROMFILE); CBitmap bmp; bmp.Attach((HBITMAP)hBitMap); // handle I got from LoadBitmap BITMAP bitmap; bmp.GetBitmap(&bitmap); int size = bitmap.bmHeight*bitmap.bmWidth*bitmap.bmBitsPixel/8; BYTE *lpBits = new BYTE [ size ]; // Here i convert the image to byte array ::GetBitmapBits((HBITMAP)hBitMap,size,lpBits ); // Here i get the handle to the picture control on my dialog HWND hImage = this->GetDlgItem(IDC_STATIC_IMAGE)->GetSafeHwnd(); // I Call the following function to paint the converted byte array SetRawBitsToImage (hImage,bitmap.bmWidth,bitmap.bmHeight,lpBits ,32); //The defination of the above function is // hwnd = handle to the image control // W = width of the raw bits Image // H = height of the raw bits Image // lpBits = pointer to the raw bits image // BitCount = 32,24 etc ( depend on the bit count ) defualt =32 void CMyTryImageDlg:: SetRawBitsToImage (HWND hwnd,int W,int H,BYTE *lpBits, int BitCount) { HDC hDC = ::GetDC(hwnd); ::SetWindowPos(hwnd,0,0,0,W,H,SWP_NOMOVE); BITMAPINFO bi; memset(&bi,0,sizeof(BITMAPINFO)); bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bi.bmiHeader.biWidth = W; bi.bmiHeader.biHeight = H; bi.bmiHeader.biPlanes = 1; bi.bmiHeader.biBitCount = BitCount; bi.bmiHeader.biCompression = BI_RGB; bi.bmiHeader.biSizeImage = bi.bmiHeader.biWidth*bi.bmiHeader.biHeight*BitCount/8; ::SetDIBitsToDevice(hDC, 0,0, W, H, 0, 0, 0,bi.bmiHeader.biHeight, &lpBits,&bi,DIB_RGB_COLORS); ::ReleaseDC(hwnd,hDC); } // But nothing is getting displayed? why? Is there any mistake in code? // I donot have knowledge in images Thanks!
Where exactly does it seem to fail, did you try stepping thorough the code with the debugger to see what happens?
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <
-
Hai! I am taking a image from a location, converting into byte array and displaying it back on my dialog by converting into bitmap image! HBITMAP hBitMap; hBitMap = (HBITMAP) LoadImage (NULL, \ "c:\\Documents and Settings\\Desktop\\Image\\bitmap1.bmp", \ IMAGE_BITMAP, SM_CXICON, SM_CYICON, LR_LOADFROMFILE); CBitmap bmp; bmp.Attach((HBITMAP)hBitMap); // handle I got from LoadBitmap BITMAP bitmap; bmp.GetBitmap(&bitmap); int size = bitmap.bmHeight*bitmap.bmWidth*bitmap.bmBitsPixel/8; BYTE *lpBits = new BYTE [ size ]; // Here i convert the image to byte array ::GetBitmapBits((HBITMAP)hBitMap,size,lpBits ); // Here i get the handle to the picture control on my dialog HWND hImage = this->GetDlgItem(IDC_STATIC_IMAGE)->GetSafeHwnd(); // I Call the following function to paint the converted byte array SetRawBitsToImage (hImage,bitmap.bmWidth,bitmap.bmHeight,lpBits ,32); //The defination of the above function is // hwnd = handle to the image control // W = width of the raw bits Image // H = height of the raw bits Image // lpBits = pointer to the raw bits image // BitCount = 32,24 etc ( depend on the bit count ) defualt =32 void CMyTryImageDlg:: SetRawBitsToImage (HWND hwnd,int W,int H,BYTE *lpBits, int BitCount) { HDC hDC = ::GetDC(hwnd); ::SetWindowPos(hwnd,0,0,0,W,H,SWP_NOMOVE); BITMAPINFO bi; memset(&bi,0,sizeof(BITMAPINFO)); bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bi.bmiHeader.biWidth = W; bi.bmiHeader.biHeight = H; bi.bmiHeader.biPlanes = 1; bi.bmiHeader.biBitCount = BitCount; bi.bmiHeader.biCompression = BI_RGB; bi.bmiHeader.biSizeImage = bi.bmiHeader.biWidth*bi.bmiHeader.biHeight*BitCount/8; ::SetDIBitsToDevice(hDC, 0,0, W, H, 0, 0, 0,bi.bmiHeader.biHeight, &lpBits,&bi,DIB_RGB_COLORS); ::ReleaseDC(hwnd,hDC); } // But nothing is getting displayed? why? Is there any mistake in code? // I donot have knowledge in images Thanks!
Why the flip do that at all? You've loaded the image into a bitmap - why not just display that with BitBlt[^] or StretchBlt[^]?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Why the flip do that at all? You've loaded the image into a bitmap - why not just display that with BitBlt[^] or StretchBlt[^]?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
Actually this is a sample to check "How to display image from an byte array?" In my actual requirement, I will be having an byte array, i have to display image from it? I was just trying withe the function , but nothing gets displayed !! Any other simple technique to display Byte Array to image on my dialog box !! Please its very urgent!! thanks !
-
Where exactly does it seem to fail, did you try stepping thorough the code with the debugger to see what happens?
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <
Yes, Actually inside SetRawBitsToImage (), there is SetDIBitsToDevice (), it must return no of rows scanned, but it returns zero, I tried with GetLastError (), but it returns zero, stating no error, i tried while debuggging to see the value od hdc , it stated "Unused:Expression cannot be solved"!! Thanks!
-
Actually this is a sample to check "How to display image from an byte array?" In my actual requirement, I will be having an byte array, i have to display image from it? I was just trying withe the function , but nothing gets displayed !! Any other simple technique to display Byte Array to image on my dialog box !! Please its very urgent!! thanks !
You code doesn't make sense. As I said again and again you should at least know how the image data is stored in your buffer before even attempting to display it. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Yes, Actually inside SetRawBitsToImage (), there is SetDIBitsToDevice (), it must return no of rows scanned, but it returns zero, I tried with GetLastError (), but it returns zero, stating no error, i tried while debuggging to see the value od hdc , it stated "Unused:Expression cannot be solved"!! Thanks!
I tried to run your code and see what happens (in a completely new dialog-based project) but for some reason if i try to load a bitmap either from resource or from file with LoadImage or LoadBitmap i get back NULL and zero for GetLastError or either "the parameter is incorrect" or "there is not enough storage to execute the requested command" (not literally quoted)... so anyways, you know you might get negative heights for a bitmap if it is stored upside-down, this nice feature can make one's life ...less enjoyable... so maybe try checking out the heights...
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <
-
Actually this is a sample to check "How to display image from an byte array?" In my actual requirement, I will be having an byte array, i have to display image from it? I was just trying withe the function , but nothing gets displayed !! Any other simple technique to display Byte Array to image on my dialog box !! Please its very urgent!! thanks !
kapardhi wrote:
Any other simple technique to display Byte Array to image on my dialog box !!
i use GetDIBits. here's a sample in VB that might help: http://www.vb-helper.com/howto_make_gray.html[^]