Click here to Skip to main content
12,402,666 members (71,177 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VC6 VC7 VC7.1 VC8.0 C++ C++/CLI , +
Hello all!
I'm working on a program in which, I'm receiving the bytes of an image and after storing this creating a bitmap to display.
This is my Main code Please see this..
static char buff[MAX_SIZE];
	int num;
			
//receiving the bytes from a client		 
          num=recv(Socket, buff, sizeof(buff), 0);
 
		 
    // progress the buffer
  
				
 
					tagBITMAPFILEHEADER bfh = *(tagBITMAPFILEHEADER*)buff;
					tagBITMAPINFOHEADER bih = *(tagBITMAPINFOHEADER*)(buff+sizeof(tagBITMAPFILEHEADER));
 
					RGBQUAD             rgb = *(RGBQUAD*)(buff+sizeof(tagBITMAPFILEHEADER)+sizeof(tagBITMAPINFOHEADER));
 

					BITMAPINFO bi;
bi.bmiColors[0] = rgb;
bi.bmiHeader = bih;
char* pPixels = (buff+bfh.bfOffBits);
 
char* ppvBits;
 

hbitmap = CreateDIBSection(NULL, &bi, DIB_RGB_COLORS, (void**) &ppvBits, NULL, 0);
SetDIBits(NULL, hbitmap, 0, bih.biHeight, pPixels, &bi, DIB_RGB_COLORS);
 
GetObject(hbitmap, sizeof(BITMAP), &cBitmap);


I'm unable to create a bitmap from the data received..
I think something wrong with code.

Thanks in advance for your help!
Posted 31-Jan-13 23:50pm
Updated 1-Feb-13 1:18am
v3
Comments
Matthew Faithfull 1-Feb-13 5:58am
   
You're going to need to indicate what is wrong in order to get some help.
Does it compile? Does it run? Do you get a result? What doesn't work?
CPallini 1-Feb-13 6:04am
   
And... Where is the question?
toms from newdelhi 1-Feb-13 6:10am
   
its there...

On checking the bytes received,,its ok to be there from both client and server side, But my code is unable to read these bytes to create a bitmap. That's the problem. Connection is OK. Compilation is OK. Have you any idea for this code?
toms from newdelhi 1-Feb-13 6:06am
   
On checking the bytes received,,its ok to be there from both client and server side,
But my code is unable to read these bytes to create a bitmap.
That's the problem.
Connection is OK.
Compilation is OK.
Have you any idea for this code?
Matthew Faithfull 1-Feb-13 6:16am
   
OK so I'm guessing you get a blank bitmap or one filled with rubbish?
You're offsets into the recieved data could be wrong or there could be extra headers, missing headers. If all that's wrong is the data then I'm afraid without knowing what data is in your stream I can't help further.
toms from newdelhi 1-Feb-13 6:48am
   
as I've checked to receive bytes in a text file,these are bytes but there is nothing on window panel.
Matthew Faithfull 1-Feb-13 7:02am
   
Ah, that sounds like a display problem rather than a problem with the bitmap. You could try testing your display code separately with a bitmap you know is good, for example from a local resource. If your display code is not working of course you'll need to post that to get a fix.
toms from newdelhi 1-Feb-13 7:14am
   
for WM_PAINT code is
[code]
case WM_PAINT:
{

PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd,&ps);

if(hbitmap != 0)
{


HDC hdcMem = CreateCompatibleDC(hdc);

SelectObject(hdcMem, hbitmap);

BitBlt(hdc, 10, 10, cBitmap.bmWidth, cBitmap.bmHeight, hdcMem, 0, 0, SRCCOPY);

}

EndPaint (hWnd, &ps);
}
break;
[/code]
toms from newdelhi 1-Feb-13 7:16am
   
for creating a bitmap from a buffer is:
tagBITMAPFILEHEADER bfh = *(tagBITMAPFILEHEADER*)buff;
tagBITMAPINFOHEADER bih = *(tagBITMAPINFOHEADER*)(buff+sizeof(tagBITMAPFILEHEADER));

RGBQUAD rgb = *(RGBQUAD*)(buff+sizeof(tagBITMAPFILEHEADER)+sizeof(tagBITMAPINFOHEADER));


BITMAPINFO bi;
bi.bmiColors[0] = rgb;
bi.bmiHeader = bih;
char* pPixels = (buff+bfh.bfOffBits);

char* ppvBits;


hbitmap = CreateDIBSection(NULL, &bi, DIB_RGB_COLORS, (void**) &ppvBits, NULL, 0);
SetDIBits(NULL, hbitmap, 0, bih.biHeight, pPixels, &bi, DIB_RGB_COLORS);

GetObject(hbitmap, sizeof(BITMAP), &cBitmap);

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

read the docs carefully: http://msdn.microsoft.com/en-us/library/windows/desktop/dd183376(v=vs.85).aspx[^]

You better code with a clear and known sample image, and check whether the header and data are transmitted and retreived corectly.

My tip is, that the backcast goes wrong.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 11 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100