Click here to Skip to main content
Rate this: bad
good
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 1-Feb-13 0:50am
Edited 1-Feb-13 2:18am
v3
Comments
Matthew Faithfull at 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 at 1-Feb-13 6:04am
   
And... Where is the question?
toms from newdelhi at 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 at 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 at 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 at 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 at 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 at 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 at 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
0 George Jonsson 175
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 11 Feb 2013
Copyright © CodeProject, 1999-2014
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