CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 45
  1. #1
    Join Date
    Oct 2013
    Posts
    39

    [win32] Send bitmap over socket

    I want to send a bitmap over socket, however since I kinda work with bitmaps for the first time, I dont really have an idea how to do that, so far I have code that gets the screenshot of the screen and stores it to HBITMAP (works fine), then fills BITMAPINFO struct and then creates a BYTE buffer from that struct, so I can send it via socket and then somehow (i still dont know how) recreate the bitmap using the BYTE buffer I created

    Code:
    				RECT rc;
    				GetClientRect(GetDesktopWindow(), &rc);
    
    				HDC hDC = GetDC(0);
    				HBITMAP hBmp = CreateCompatibleBitmap(hDC, rc.right, rc.bottom);
    
    				SelectObject(hDC, hBmp);
    
    				//Draws screenshot to the hBmp
    				BitBlt(hDC, 0, 0, rc.right, rc.bottom, GetDC(NULL), 0, 0, SRCCOPY);
    
    				BYTE* buf = new BYTE[256*256*256];
    				BITMAPINFO bmi;
    
    				int a = GetDIBits(hDC, hBmp, 0, rc.bottom, NULL, &bmi, DIB_RGB_COLORS);
    				if(a == 0)
    					MessageBox(hWnd, "Error when filling BMI", NULL, NULL);
    
    				int b = GetDIBits(hDC, hBmp, 0, rc.bottom, buf, &bmi, DIB_RGB_COLORS);
    				if(b == 0)
    					MessageBox(hWnd, "Error when filling buffer", NULL, NULL);
    				
    				//hBmp == screenshot
    
    				ReleaseDC(hWnd, hDC);
    				DeleteDC(hDC);
    				DeleteObject(hBmp);
    so the code is supposed to:

    1. get dimensions of screen
    2. create DC and compatible bitmap to store the screenshot
    3. draw screen to bitmap
    4. fill BITMAPINFO struct bmi with hBmp properties
    5. fill BYTE buffer buff with BITMAPINFO properties we got from point 4
    6. at this point, I should be able to send buffer, that contains everything needed to recreate bitmap (or?)
    7. free memory

    HOWEVER I'm getting "Error when filling BMI" error, and I have no idea why, also, how do I recreate bitmap from the "buf" after I get it from
    Code:
    GetDIBits(hDC, hBmp, 0, rc.bottom, buf, &bmi, DIB_RGB_COLORS);
    Last edited by DaigonoYouso; January 5th, 2014 at 12:23 PM.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: [win32] Send bitmap over socket

    Quote Originally Posted by DaigonoYouso View Post
    I want to send a bitmap over socket, however since I kinda work with bitmaps for the first time,
    Then you should be attempting to save the bitmap to a file, and then attempt to read it back. You don't need to add complexity by introducing sockets. Saving and reading from a file is essentially all you're doing, and the socket stuff is just another way of saving/reading. If you can successfully save the bitmap to a file in one program, and have another program attempt to read the file you just saved and display the bitmap, then you basically have the problem solved.

    There are a huge number of examples of saving and reading bitmaps, as this is a common functionality of Windows image processing. Please do a google search and you can't miss them.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 5th, 2014 at 02:37 PM.

  3. #3
    Join Date
    Oct 2013
    Posts
    39

    Re: [win32] Send bitmap over socket

    Quote Originally Posted by Paul McKenzie View Post
    Then you should be attempting to save the bitmap to a file, and then attempt to read it back. You don't need to add complexity by introducing sockets. Saving and reading from a file is essentially all you're doing, and the socket stuff is just another way of saving/reading.

    There are a huge number of examples of saving and reading bitmaps, as this is a common functionality of Windows image processing. Please do a google search and you can't miss them.

    Regards,

    Paul McKenzie
    but I want to send the bitmap over socket, not to save it ...

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: [win32] Send bitmap over socket

    Quote Originally Posted by DaigonoYouso View Post
    but I want to send the bitmap over socket, not to save it ...
    So where is the problem with the socket? I didn't see anything in your first post where you were having a problem with the socket. The act of saving and reading to a file is no different than what you're doing with a socket.

    You're concentrating on the means of getting a bitmap from point A to point B. That isn't what you're having a problem with -- your problem is purely an error with putting together data to save to a file, send over a socket, whatever. That's why if you can save the data to a file and read it back, then all you need to do after that is send that very same data to a socket, and have the other side read it back and put together the bitmap.

    If you can't save the data to a file, then you can't send the data over a socket. So that's why you should concentrate on saving to a file, as there are thousands, if not millions of examples of saving to a file. Once you learn that, you take those very same bytes you used to save to a file, and just send them over the socket.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 5th, 2014 at 02:47 PM.

  5. #5
    Join Date
    Oct 2013
    Posts
    39

    Re: [win32] Send bitmap over socket

    Quote Originally Posted by Paul McKenzie View Post
    So where is the problem with the socket? I didn't see anything in your first post where you were having a problem with the socket. The act of saving and reading to a file is no different than what you're doing with a socket.

    You're concentrating on the means of getting a bitmap from point A to point B. That isn't what you're having a problem with -- your problem is purely an error with putting together data to save to a file, send over a socket, whatever. That's why if you can save the data to a file and read it back, then all you need to do after that is send that very same data to a socket, and have the other side read it back and put together the bitmap.
    that is why I'm trying to get the bitmap info to BITMAPINFO struct and then to buffer, using GetDIBits function, which is unfortunatelly giving me an error

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: [win32] Send bitmap over socket

    Quote Originally Posted by DaigonoYouso View Post
    that is why I'm trying to get the bitmap info to BITMAPINFO struct and then to buffer, using GetDIBits function, which is unfortunatelly giving me an error
    Then again, take one of the many examples of saving HBITMAP data, compile it, run it. Once you have that working, you just change the code from doing file writing to "socket sending". Saving HBITMAP's to a file is one of the examples you can't miss if you do a google search -- again this is a fundamental of Windows bitmap processing.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Oct 2013
    Posts
    39

    Re: [win32] Send bitmap over socket

    okay, I have this function now

    Code:
    				RECT rc;
    				GetClientRect(GetDesktopWindow(), &rc);
    
    				HDC hDC = GetDC(NULL);
    				HBITMAP hBmp = CreateCompatibleBitmap(hDC, rc.right, rc.bottom);
    
    				SelectObject(hDC, hBmp);
    				
    				BitBlt(hDC, 0, 0, rc.right, rc.bottom, GetDC(NULL), 0, 0, SRCCOPY);
    
    				BYTE* buff = new BYTE[256*256*256];
    				LPBITMAPINFO pbi = CreateBitmapInfoStruct(hBmp);
    
    				PBITMAPINFOHEADER pbih;
    				LPBYTE lpBits;
    				DWORD dwTotal;
    				DWORD cb;
    				BYTE *hp;
    				DWORD dwTmp;
    
    				pbih = (PBITMAPINFOHEADER)pbi;
    				lpBits = (LPBYTE)LocalAlloc(LMEM_FIXED, pbih->biSizeImage);
    
    				int a = GetDIBits(hDC, hBmp, 0, (WORD)pbih->biHeight, lpBits, pbi, DIB_RGB_COLORS);
    				if(a == 0)
    					MessageBox(hWnd, NULL, NULL, NULL);
    
    				WriteFile((HANDLE)ps.GetSocket(-1), (LPVOID)pbih, sizeof(BITMAPINFOHEADER) + pbih->biClrUsed*sizeof(RGBQUAD), (LPDWORD)&dwTmp, (NULL));
    				
    				dwTotal = cb = pbih->biSizeImage;
    				hp = lpBits;
    
    				WriteFile((HANDLE)ps.GetSocket(-1), (LPSTR)hp, (int)cb, (LPDWORD)&dwTmp, NULL);
    
    				//hBmp = screenshot
    
    				LocalFree((HLOCAL)lpBits);
    				ReleaseDC(hWnd, hDC);
    				DeleteDC(hDC);
    				DeleteObject(hBmp);
    now I want to know 3 things:

    1. is there a way to "WriteFile" those 2 things at ONCE?
    2. how do I recreate the bitmap?
    3. how do I retrieve this info using ReadFile when I'm not getting FD_READ when sending it via WriteFile ?

    edit: also I'm getting std::bad_alloc debug error after like 5 seconds (~100 bitmaps sent)
    Last edited by DaigonoYouso; January 5th, 2014 at 03:49 PM.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: [win32] Send bitmap over socket

    Quote Originally Posted by DaigonoYouso View Post
    1. is there a way to "WriteFile" those 2 things at ONCE?
    But how would the receiver know what is data and what is header information? You will need to send one item at a time, unless you have a way to inform the receiver that the data sent is header as opposed to the bitmap info data.
    2. how do I recreate the bitmap?
    Look at code that reads a bitmap from a file and recreates the HBITMAP by reading the header and data.
    3. WriteFile sends a packets via socket? (I know it can accept a socket handle, but I dont know if it sends it or what does it do with it?)
    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 5th, 2014 at 04:46 PM.

  9. #9
    Join Date
    Oct 2013
    Posts
    39

    Re: [win32] Send bitmap over socket

    when I read your responses, I really want to punch you in the face, really hard, with a chair

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: [win32] Send bitmap over socket

    Great response. What in my posts make you so angry? Did you not read that the agreement on this website is not to post threats or harass other members?

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 5th, 2014 at 04:52 PM.

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449

    Re: [win32] Send bitmap over socket

    Quote Originally Posted by DaigonoYouso View Post
    edit: also I'm getting std::bad_alloc debug error after like 5 seconds (~100 bitmaps sent)
    That is because you never delete the memory you allocated here:
    Code:
    BYTE* buff = new BYTE[256*256*256];
    Every call to new[] must have a corresponding call to delete[]. Or use a safer construct such as a vector.
    Code:
    std::vector<BYTE> buff(256*256*256);
    Then pass &buff[0] to all of the functions that require a BYTE*. Then there is need to call delete[] afterwards.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 5th, 2014 at 04:53 PM.

  12. #12
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: [win32] Send bitmap over socket

    "Then there is no need to call delete[] afterwards."
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  13. #13
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: [win32] Send bitmap over socket

    Quote Originally Posted by DaigonoYouso View Post
    when I read your responses, I really want to punch you in the face, really hard, with a chair
    This attitude does not endear yourself to other guru members who might have been been willing to spend some time looking through your code to try to provide advice and guidance.

    I certainly won't now be answering this or any future post from yourself.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    Join Date
    Apr 1999
    Posts
    27,449

    Re: [win32] Send bitmap over socket

    Quote Originally Posted by 2kaud View Post
    "Then there is no need to call delete[] afterwards."
    Yep, forgot to say "no".

    Regards,

    Paul McKenzie

  15. #15
    Join Date
    Oct 2013
    Posts
    39

    Re: [win32] Send bitmap over socket

    Quote Originally Posted by Paul McKenzie View Post
    Great response. What in my posts make you so angry? Did you not read that the agreement on this website is not to post threats or harass other members?

    Regards,

    Paul McKenzie
    the way you reply, also I didnt threat you nor harassed you in any way, I just expressed my feelings

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured