Bitmap Display from Buffer received by TCP socket

This is a discussion on Bitmap Display from Buffer received by TCP socket within the C++ Programming forums, part of the General Programming Boards category; I want to display my image on window without saving it. When data is received window size changes but there ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    15

    Bitmap Display from Buffer received by TCP socket

    I want to display my image on window without saving it.
    When data is received window size changes but there is no display
    on window.
    My Code is:
    Code:
    int iBufferLength;
        int iEnd;
        int iSpaceRemaining;
        
        int i;
    
        iBufferLength = iSpaceRemaining = sizeof(chIncomingDataBuffer);
        iEnd = 0;
        iSpaceRemaining -= iEnd;
    
        iBytesRead = recv(Socket, chIncomingDataBuffer+iEnd, iSpaceRemaining, 0);
        
        iEnd+=iBytesRead;
        if (iBytesRead == SOCKET_ERROR)
            MessageBox(hWnd,
                            "Socket Error",
                            "Connection strt",
                            MB_ICONINFORMATION|MB_OK);
            chIncomingDataBuffer[iEnd] = '\0';
    
        if (lstrlen(chIncomingDataBuffer) != 0)
        {
            /*FILE* pfile;
                        
                        pfile =     fopen("test.jpeg", "wb");
                    fwrite(chIncomingDataBuffer,1, iBytesRead ,pfile);
                    fclose(pfile);*/
    
                    GetWindowRect(hWnd, &rect);
                    SetWindowPos(hWnd, NULL, rect.left, rect.top, cBitmap.bmWidth, cBitmap.bmHeight, 0);
                      HDC ThisDC = GetDC(hWnd);
    
                  DeleteDC(RemoteDC);
                  RemoteDC = CreateCompatibleDC(ThisDC);
                  DeleteObject(hbitmap);
                  hbitmap= CreateCompatibleBitmap(ThisDC, cBitmap.bmWidth, cBitmap.bmHeight);
    
                  SelectObject(RemoteDC, hbitmap);
    
                  ReleaseDC(hWnd, ThisDC);
    
    
    
                  BITMAPINFO bi;
                  HBITMAP hbmap;
                  int bisize = sizeof(BITMAPINFO);
                  memcpy(&bi, chIncomingDataBuffer+iEnd, bisize );
                  SetDIBits(RemoteDC, hbitmap, 0,  cBitmap.bmHeight, chIncomingDataBuffer+iEnd+bisize,  &bi, DIB_RGB_COLORS);
    
    
                  InvalidateRect(hWnd, NULL, false);
    Can you find my error,,,where I'm doing wrong?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > chIncomingDataBuffer[iEnd] = '\0';
    > if (lstrlen(chIncomingDataBuffer) != 0)
    Well, treating binary image data as a string for one thing is wrong.

    2, as in your previous threads, you're STILL assuming that recv receives the whole damn message in a single call.

    3. Your image fetching is inline with your display function.
    If you're wanting to redraw this many times (window resized), then you need to fetch the image just ONCE, and then go into some loop which displays the image.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    15
    But i tried writing an image using fwrite and that is correct- if recv is wrong then how's that possible.
    Thanks!

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Code:
    static char buff[MAX_SIZE];
    static size_t buffUsed = 0;
    static size_t buffFree = MAX_SIZE;
    static int imageReceived = 0;
    
    ///
    
      case WM_SOCKET:
      {
        switch(WSAGETSELECTEVENT(lParam))
        {
          case FD_READ:
          num=recv(Socket, &buff[buffUsed], buffFree, 0);
          if ( num > 0 ) {
              // progress the buffer
              buffUsed += num;
              buffFree -= num;
          } else if ( num == 0 ) {
              // end of connection, do stuff with buff
              close(Socket);
              imageReceived = 1;
          } else {
              close(Socket);
              // some error dialog
          }
        }
      }
      case ...
        if ( imageReceived ) {
          GetWindowRect(hWnd, &rect);
          SetWindowPos(hWnd, NULL, rect.left, rect.top, cBitmap.bmWidth, cBitmap.bmHeight, 0);
          HDC ThisDC = GetDC(hWnd);
          DeleteDC(RemoteDC);
          RemoteDC = CreateCompatibleDC(ThisDC);
          DeleteObject(hbitmap);
          hbitmap= CreateCompatibleBitmap(ThisDC, cBitmap.bmWidth, cBitmap.bmHeight);
          SelectObject(RemoteDC, hbitmap);
          ReleaseDC(hWnd, ThisDC);
          // or whatever
        }
    The only code which does anything BEFORE imageReceived is true is the network code to fetch a number of messages and store them in buff.
    After successful termination of the link, the socket is closed and imageReceived is set to TRUE.

    At this point, you can doodle on the screen as much as you want, with the data stored in buff
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    In addition....

    Please post your WM_PAINT handler, because none of the code posted actually draws anything to the screen.

    BTW
    You are not freeing your GDI objects properly and if the program runs for any period of time you may run out of GDI resources. Search for the correct way to use SelectObject().

    You should not create a new DC each image. Create one memory DC the max screen resolution when the app starts and re-use it.

    Use AdjustWindowRect() [AdjustWindowRectEx()] to set the client area to the required image size. SetWindowPos() sets the whole window size, which inc menus, borders, tool bars etc (and so your window will not be big enough).

    Always follow an InvalidateRect() with an UpdateWindow(), as this bypasses the OS message queue and posts the paint msg directly to the windows callback (so you get smooth drawing).
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    15
    This is my code for WM_PAINT:
    Code:
    case WM_PAINT:
                {
                HDC hdc;
                PAINTSTRUCT ps;
                HGDIOBJ hobj;
    
                hdc = BeginPaint(hWnd,&ps);
                if(hbitmap!= 0)
                {
                hobj = SelectObject(RemoteDC, hbitmap);
                BitBlt(hdc, 10, 10, cBitmap.bmWidth, cBitmap.bmHeight, hdc, 0, 0, SRCCOPY);
                
                }
                EndPaint (hWnd, &ps);
                }
                break;
    
            }

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    Try a paint more like (NOTE this is written from memory and I have not coded in WIN32 for years...)

    Code:
    case WM_PAINT:             
    	PAINTSTRUCT ps;             
    
    	BeginPaint(hWnd,&ps);
    
    	BitBlt(		ps.hdc, 				--use the HDC in the PAINTSTRUCT	
    			ps.rcPaint.left, ps.rcPaint.top, 	--redraw ONLY the invalidated area
    			ps.rcPaint.right - ps.rcPaint.left, 	--redraw ONLY the invalidated area
    			ps.rcPaint.bottom - ps.rcPaint.top, 
    			RemoteDC, 				--your memory DC that contains the image 
    			ps.rcPaint.left, ps.rcPaint.top, 	
    			SRCCOPY); 
    
    	EndPaint (hWnd, &ps); 
      
            return 0;//show we processed this msg
    break;
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    15
    @salem,,


    I did trying according to your code but there is nothing....
    and also unable to write the image to check the data...
    >MAX_SIZE is undefined and after defining it there is nothing,, is there any limit for this?
    >and when socket is closed in which case this will be the right way to create bitmapinfo..
    >am I right on bitmap info? or there is also something wrong.

    Thanks For your help!

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    15
    @novacain,,
    I get through this and implemented this on WM_PAINT but when I apply this code,,,
    on executing the program there is no window?

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    The "something wrong" would appear to be that you don't yet know enough about programming, and all you're doing is copy/pasting likely looking snippets from wherever in the hope that it'll either work, or you can convince someone else to fix it up for you.

    What working programs do you have?

    - recv() + write to a file
    - read from a file + display on screen

    Can you make either of these work?

    Part of the process is to extract the area of difficulty, create a small program which tests just ONE idea at a time.
    The benefits of doing this are
    - less distraction from other bits which may be suspect
    - faster turnaround time between tests
    - much easier to post the WHOLE code on a forum
    - therefore much easier for us to help you with it.

    Random un-compilable snippets and vague "it doesn't work" won't get you far.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with Bitmap Display
    By The Brain in forum Windows Programming
    Replies: 7
    Last Post: 03-23-2009, 05:33 AM
  2. Getting a Bitmap's Buffer
    By Brad0407 in forum Windows Programming
    Replies: 8
    Last Post: 03-25-2007, 01:43 PM
  3. Dynamic Bitmap Display
    By Jaken Veina in forum Windows Programming
    Replies: 5
    Last Post: 07-04-2005, 10:57 AM
  4. Bitmap Display Probelm
    By hemanth.balaji in forum Windows Programming
    Replies: 1
    Last Post: 06-09-2005, 12:17 PM
  5. problem with UDP WSAAsyncSelect socket, no callback message received???
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-29-2004, 10:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21