Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Can you please explain me why this code is not working ?

Can you please explain me why this code is not working ?

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsdata-structuresquestion
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kapardhi
    wrote on last edited by
    #1

    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!

    S C 2 Replies Last reply
    0
    • K kapardhi

      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!

      C Offline
      C Offline
      Code o mat
      wrote on last edited by
      #2

      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. <

      K 1 Reply Last reply
      0
      • K kapardhi

        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!

        S Offline
        S Offline
        Stuart Dootson
        wrote on last edited by
        #3

        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

        K 1 Reply Last reply
        0
        • S Stuart Dootson

          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

          K Offline
          K Offline
          kapardhi
          wrote on last edited by
          #4

          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 !

          CPalliniC C 2 Replies Last reply
          0
          • C Code o mat

            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. <

            K Offline
            K Offline
            kapardhi
            wrote on last edited by
            #5

            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!

            C 1 Reply Last reply
            0
            • K kapardhi

              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 !

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              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]

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              • K kapardhi

                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!

                C Offline
                C Offline
                Code o mat
                wrote on last edited by
                #7

                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. <

                1 Reply Last reply
                0
                • K kapardhi

                  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 !

                  C Offline
                  C Offline
                  Chris Losinger
                  wrote on last edited by
                  #8

                  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[^]

                  image processing toolkits | batch image processing

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups