IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

MFC Discussion :

Affichage de bitmap chargé en mémoire sur Picture Box


Sujet :

MFC

  1. #1
    Membre confirmé
    Profil pro
    Developer
    Inscrit en
    Juin 2004
    Messages
    194
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations professionnelles :
    Activité : Developer

    Informations forums :
    Inscription : Juin 2004
    Messages : 194
    Par défaut Affichage de bitmap chargé en mémoire sur Picture Box
    Bonjour,

    Voici en gros j'ai fait une application utilisant HBITMAP et avec la methode LoadImage() sur un fichier bmp. Mais je voudrais cette fois charger une image non pas à partir d'un fichier (code ci-dessous) mais a partir de données en mémoire (voir commentaires).

    Mon application actuelle qui fonctionne wrappe une classe Camdevice, cette classe sauve un fichier bitmap sur disque qu'utilise mon HBITMAP pour afficher chaque images de la camera.

    voici le code:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    175
     
    //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;
    }



    Mais La classe Camdev possède également une methode qui sauve en memoire (SaveToMem()). Ne voulant pas sauver un fichier sur disque, j'ai donc sauver en buffeur le fichier bitmap puis j'ais utilisé une fonction que j'ai récuperé sur le net qui permet de passer directement un buffeur au HBITMAP en utilisant des DIBSections etc..

    Mais mon application compile bien avec les codes commentés maisje n'arrive pas afficher correctement l'image sur mon Picture Control, l'image semble figé.


    Comment dois je passer le buffeur à mon HBITMAP afin que mes fonction GetcaptureImage() et DisplayImage() utilise ce Handle correctement avec les methodes MFC ?

    Merci

  2. #2
    Membre confirmé
    Profil pro
    Developer
    Inscrit en
    Juin 2004
    Messages
    194
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations professionnelles :
    Activité : Developer

    Informations forums :
    Inscription : Juin 2004
    Messages : 194
    Par défaut
    Voici une façon que j'ai voulu faire en utilisant une fonction personnalisé (LoadBitmapFromMemory) qui renvoi un handle sur HBITMAP et qui utilise la fonction CreateDIBSection.

    Code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
     
     
    HWND hwnd   = NULL;
     
    //HBITMAP eventsavedlgdc = (HBITMAP)::LoadImage(NULL,"readframes01//.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
    HBITMAP eventsavedlgdc = (HBITMAP)::LoadImage(NULL,"readframes01.bmp",IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_LOADFROMFILE);
     
    HDC           hMemDC;
    RECT rectScaled;
    int rectW;
    int rectH;
     
    //BITMAP EN MEMOIRE
    void *Gl_buffer = 0;
    int Gl_buflen = 0;
     
     
    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() //
    {
    	TRACE("Dans Destructeur CFXAMD_PREVIEWDLG\n"); 
    	if (m_Picture)::DeleteObject(m_Picture);
    	if (Bmp4.m_hObject)::DeleteObject(Bmp4.m_hObject);
     
    	m_Font.DeleteObject();//
     
    	cp.StopCapture();//900901 utile ?
    	cp.SaveSettingsToFile();// 
    }
     
    //------------------------------------------------------------------
    HBITMAP LoadBitmapFromMemory( void * pBmpFileData )
    {
    // Check parameter
    ASSERT( pBmpFileData != NULL );
    if ( pBmpFileData == NULL )
    return NULL;
     
    // Pointer to file bytes
    BYTE * pFileBytes = (BYTE *)pBmpFileData;
     
    // DIB bits (will be set by CreateDIBSection)
    BYTE * pBits = NULL;
     
    // Access the BITMAPINFOHEADER section
    BITMAPINFOHEADER * pbmih = (BITMAPINFOHEADER *)(pFileBytes +
    sizeof(BITMAPFILEHEADER));
     
    // Create the DIB
    HBITMAP hBitmap = ::CreateDIBSection(
    NULL,                   // No HDC used
    (BITMAPINFO *)pbmih,    // Pointer to DIB information
    0,                      // Default
    (void **)&pBits,        // The function will return pointer to
    //   bitmap bits
    NULL,                   // No file mapping
    0 );                    // No file mapping
    if ( hBitmap == NULL )
    return NULL;
     
     
    // Get pointer to bitmap bits in original file
    // (we obtain the offset from BITMAPFILEHEADER.bfOffBits field)
    BITMAPFILEHEADER * pbmfh = (BITMAPFILEHEADER *)pFileBytes;
    BYTE * pSourceBitmapBits = pFileBytes + pbmfh->bfOffBits;
     
    // Copy bits
    memcpy(pBits, pSourceBitmapBits, pbmih->biSizeImage);
     
    // Return bitmap handle
    return hBitmap;
    }
     
    BOOL CCam_confDlg::OnInitDialog()
    {
    	CDialog::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;
     
    	bool store=FALSE;
     
    	store=cp.Init(Gl_ip);  //addresse IP de l'argument
     
    	if (store==false) 
         MessageBox("Problem of Connection on cam");
     
     
    	try{
    	//HBITMAP eventsavedlgdc = (HBITMAP)::LoadImage(NULL,"",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
     
    	cp.image.Load("readframes01.bmp"); //obligé
    	cp.image.SaveToMem(&Gl_buffer, &Gl_buflen, GX_BMP); //GX_JPEG
     
    	}
    	catch(...)
    	{
    	MessageBox("ERROR IN cp.image.SaveToMem","trycatch message");
     
    	}
     /*
    	HBITMAP hbmpMyImage = LoadBitmapFromMemory( Gl_buffer );
    	//eventsavedlgdc = (HBITMAP)::LoadImage(NULL,"readframes01.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);  //BON
    	eventsavedlgdc = hbmpMyImage;
    */
    	eventsavedlgdc = ::LoadBitmapFromMemory( Gl_buffer );
    	m_Picture.SetBitmap((HBITMAP) eventsavedlgdc);//?
     
    	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 
     
    	//if(!cp.image.Save("readframes01.bmp", GX_BMP)) 
         if(!cp.image.SaveToMem(&Gl_buffer, &Gl_buflen, GX_BMP)); //ICI 
    	{
    		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);  //BON
    	  eventsavedlgdc = ::LoadBitmapFromMemory( Gl_buffer );
     
    	  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); 
     
    	  //?
    	 // GetObject(eventsavedlgdc,sizeof(bm),&bm);
    	 // BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, memdc, 0, 0, SRCCOPY);
     
          DeleteDC(hdc);
          DeleteDC(memdc);
     
    	  if(eventsavedlgdc)
    	  {
    			m_Picture.SetBitmap(hBmp2);
    			m_Picture.SetBitmap((HBITMAP) eventsavedlgdc);//?
    			DeleteDC(hMemDC);
    			DeleteObject(eventsavedlgdc);
    	  }
     
    	  capture_ok=true;
    	  return FALSE;
    }

    Tel que fait, la première image qui apparait est le readframes01.bmp et ne change pas malheureusement sauf seulement apres l'envoi de quelques commandes sur la camera qu'apparait l'image rafraichit de la camera mais aussitot se fige et à nouveau l'image ne se rafraichit plus. L'application fonctionne bien au niveau des commandes la camera execute correctement mais le seul problème et l'affichage de l'image. J'ajoute pourtant le parametre LR_CREATEDIBSECTION dans la fonction LoadImage.

    Qu'y a-t-il de pas bon ? Quelles fonctions MFC pourrais je utiliser ?

    Merci

  3. #3
    Membre confirmé
    Profil pro
    Developer
    Inscrit en
    Juin 2004
    Messages
    194
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations professionnelles :
    Activité : Developer

    Informations forums :
    Inscription : Juin 2004
    Messages : 194
    Par défaut
    Bonjour,


    Désolé j'ai dans mon 2ème code oublier d'ajouter la ligne où l'image est sauvée en mémoire et

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    	//if(!cp.image.Save("readframes01.bmp", GX_BMP)) 
         if(!cp.image.SaveToMem(&Gl_buffer, &Gl_buflen, GX_BMP)); //ICI
    Mon problème est donc de recharger l'image à partir de la mémoire.
    J'ai une image noire dans mon picture Box ?

    Même quand je remplace la définition de eventsavedlgdc par une simple declaration tel que cidessous:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    //HBITMAP eventsavedlgdc = (HBITMAP)::LoadImage(NULL,"readframes01.bmp",IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_LOADFROMFILE);
     
    HBITMAP eventsavedlgdc ;
    Quelqu'un serait il m'aider ?

    Merci

Discussions similaires

  1. Réponses: 4
    Dernier message: 10/06/2009, 20h15
  2. [TP] Affichage d'image chargée sur le disque dur
    Par labgate dans le forum Turbo Pascal
    Réponses: 2
    Dernier message: 05/05/2008, 12h44
  3. [C#] : Augmentation mémoire sur affichage d'un formulaire AboutBox
    Par heavydrinker dans le forum Windows Forms
    Réponses: 1
    Dernier message: 04/04/2007, 19h25
  4. Réponses: 7
    Dernier message: 28/04/2004, 16h51
  5. Réponses: 25
    Dernier message: 16/07/2003, 20h41

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo