Question : How Display bitmap data (a variable, not a file)  into a Picture Box with MFC or WIN32 ?

Hello,
Here is my first post, so I made an application using HBITMAP with the LoadImage method () on a bmp file. But this time I would load an image, not from a file (code below) but using data in memory (see comments).

My program works like a wrapper class by calling a main class named Camdev , this class saves a bitmap file on disk -and my application used HBITMAP LoadImage() to display each image from the camera in the Picture box.
I used the code bellow and that works perfect.

But the Camdev class also has a method that saves in memory (SaveToMem ()). I don't want to save the file on disk, so I 'll save the bitmap in a memory buffer then how do it I heard about using LoadImage with parameter LR_CREATEDIBSECTION but how implement that ?

The MFC LoadBitmap() function does only load bitmaps from resource in VC++ so I decided to use LoadImage() but I don't know how?

How do I pass the bitmap data buffer so my fonctions GetcaptureImage () and DisplayImage () use this data properly with MFC or Win API methods ?



Thank you


Here's the code:
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
//Capture Image from file (loadimage)
//
#include "stdafx.h"
 
#include "Cam_conf.h"
#include "Cam_confDlg.h"
 
 
HWND hwnd   = NULL;
 
Camdev cp;  //class
 
HBITMAP eventsavedlgdc = (HBITMAP)::LoadImage(NULL,"readframes01.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
HDC           hMemDC;
RECT rectScaled;
int rectW;
int rectH;
 
 
 
 
void CAboutDlg::OnPaint()
{
   CPaintDC dc(this); // device context for painting
 
   CRect rc( 0, 0, 4, 8 );
 
   MapDialogRect( &rc );
   int baseUnitY = rc.bottom;
   int baseUnitX = rc.right;
   TRACE("baseUnitX = %d\n", baseUnitX);
   TRACE("baseUnitY = %d\n", baseUnitY);
   TRACE("Dans OnPaint\n"); //ajout
}
 
 
CCam_confDlg::~CCam_confDlg() //
{
 
	if (m_Picture)::DeleteObject(m_Picture);
	if (Bmp4.m_hObject)::DeleteObject(Bmp4.m_hObject);
	m_Font.DeleteObject();
	cp.StopCapture();
	cp.SaveSettingsToFile();// 
 
}
 
 
BOOL CCam_confDlg::OnInitDialog()
{
	//Set the icon for this dialog.  The framework does this automatically
	//when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);	//Set big icon
	SetIcon(m_hIcon, FALSE);//Set small icon
 
	//dimensions of the control
	m_Picture.GetWindowRect(&rectScaled);
	rectW=rectScaled.right-rectScaled.left;
	rectH=rectScaled.bottom-rectScaled.top;
	
	store=cp.Init(Gl_ip);  // utiliser l'addresse IP de l'argument
	
	SetTimer(ID_TIMER1, 90, 0);				
}
 
void CCam_confDlg::OnTimer(UINT nIDEvent) 
{
		
	CDialog::OnTimer(nIDEvent);
 
	     if (nIDEvent ==ID_TIMER1) 
		{
			TRACE("ON_TIMER_IN\n");
			UpdateData(TRUE);
 
			//KillTimer(1);
			KillTimer(ID_TIMER1);
	
			//MUTEX			
			if(!GetCaptureImage())
			{
				cm_display.Lock();	//Mutex
				if(!DisplayImage())	;				
				cm_display.Unlock();//Mutex	
			}
			SetTimer(ID_TIMER1,10,0);	
			UpdateData(FALSE);
		}		
}
 
 
			
bool CCam_confDlg::GetCaptureImage()
{
	if(!cp.GetCaptureInfo(&status, &newestframeix, &newesttimems,
						&nframes, &nprocbufs, &nusable, &nlocked, lockitems, &nlockitems))
				{	
					gx_geterror(&ecode, ebuf, sizeof(ebuf));
					return TRUE;
				}
				
	// Lock frame
	if(!cp.LockFrames(FXCAM_LOCK_RELEASE_ALL_OTHER, frame, frame)) 
	{  
		// Error occurred
		return TRUE;
	}
	
	// Read frame
	if(!cp.ReadFrame(FXCAM_READFRAME_AUTOADJUST, 0, frame, NULL, &frametimems, &newestframeix, NULL, cp.image)) 
			{
				if(nlockitems == 0) 
				{
				// Error occurred
				return TRUE;
				}
			}
			
	// Release the locked frame
	if(!cp.UnlockFrames(0, frame, frame)) 
			{
				// Error occurred
				return TRUE;
			}
		
	//save image		 //Utiliser SavetoMem vers ppm (pour stream),  vers jpg (pour snapshot)
	if(!cp.image.Save("readframes01.bmp", GX_BMP)) 
	{
		gx_geterror(&ecode, ebuf, sizeof(ebuf));
		return TRUE;
	}
	
	capture_ok=true;
	return FALSE;
}
 
bool CCam_confDlg::DisplayImage() 
{
	  capture_ok=false;	
 
	  if (eventsavedlgdc)
	  { 
	    	::DeleteObject(eventsavedlgdc);
	  }
 
	eventsavedlgdc = (HBITMAP)::LoadImage(NULL,"readframes01.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);//OK
 
	  CBitmap bmp;
	  bmp.Attach(eventsavedlgdc);
	  BITMAP bm;
	  bmp.GetBitmap(&bm);  
	  bmp.Detach(); 
 
	  HDC hdc=::GetDC(m_Picture.m_hWnd);
	  HDC memdc=::CreateCompatibleDC(hdc);
	  HBITMAP hBmp2=(HBITMAP)SelectObject(memdc,eventsavedlgdc);
  
 
	  SetStretchBltMode(hdc,COLORONCOLOR);  //For better quality
      StretchBlt(hdc,0,0,rectW,rectH,memdc,0,0,752,480,SRCCOPY); 
 
      DeleteDC(hdc);
      DeleteDC(memdc);
 
	  if(eventsavedlgdc)
	  {
			m_Picture.SetBitmap(hBmp2);
			DeleteDC(hMemDC);
			DeleteObject(eventsavedlgdc);
	  }
 
	  capture_ok=true;
	  return FALSE;
}

Answer : How Display bitmap data (a variable, not a file)  into a Picture Box with MFC or WIN32 ?

Hi Vimalalex,

Yes what you had give me works and display the window on my window.

Good question, in fact the Camdev class uses a SDL.dll so it is Windows API  I think?
But I don't know the DirectX technology ?

I'm using an ip camera, it is not really a webcam.

I'm happy to say you that it is ok I have the goal :) thanks to you because your help was important.

Now I can display, in the picture box, the bmp data coming from camera in fact It was not necessary to use the DDBtoDIB function because using the new code in DisplayImage function (please see code) and the LoadBitmapFromMemory function was sufficient.

Thank you

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
bool CFXCAMd_ConfiguratorDlg::DisplayImage() 
{
	//bmp_data_2_hdc
	
	HBITMAP hBitMap = ::LoadBitmapFromMemory( Gl_buffer );
	HDC hdc=::GetDC(m_Picture.m_hWnd);
	
	HDC hdcBits;
	BITMAP bm;
	long lBmpWidth, lBmpHeight;
	int iReturn;
	BOOLEAN bReturn;
 
	if (! (iReturn=GetObject(hBitMap,sizeof(BITMAP), &bm))) return 1;
 
	lBmpWidth =bm.bmWidth;
	lBmpHeight =bm.bmHeight;
 
	if (! (hdcBits=CreateCompatibleDC(hdc))) return 2;
 
	SelectObject (hdcBits,hBitMap);
 
	if(!(iReturn = SetStretchBltMode (hdc,COLORONCOLOR))) return 3;
 
	if(!(bReturn = StretchBlt (hdc,0,0,rectW, rectH, hdcBits,0,0,lBmpWidth, lBmpHeight, SRCCOPY))) return 4;
 
	if(hBitMap)    //090915 sinon memoire charge
	{
		DeleteDC(hMemDC);
		DeleteDC(hdc);
		DeleteObject(hBitMap);
	}
 
	DeleteDC (hdcBits);
	gx_globalfree(Gl_buffer);
 
	return FALSE;
}
Random Solutions  
 
programming4us programming4us