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 :

enregistrer une image


Sujet :

MFC

  1. #1
    Membre habitué Avatar de meli0207
    Inscrit en
    Novembre 2004
    Messages
    208
    Détails du profil
    Informations personnelles :
    Âge : 40

    Informations forums :
    Inscription : Novembre 2004
    Messages : 208
    Points : 133
    Points
    133
    Par défaut enregistrer une image
    bonjour,

    est-il possible de générer un fichier image (.bmp, .jpg, ...) à partir d'une CDialog, c'est à dire de faire une "capture d'écran" du contenu de la CDialog?

    merci

  2. #2
    Membre éclairé
    Avatar de matazz
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    471
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 471
    Points : 668
    Points
    668
    Par défaut
    tu fais un CDC en mémoire, tu y attache un CBitmap et tu fais un BitBlt avec le DC de ta CDialog
    Regarde ce post, c'est le même principe mais dnas lautre sens :
    http://www.developpez.net/forums/vie...=memdc+cbitmap
    Qui va piano va sano...

  3. #3
    Membre habitué Avatar de meli0207
    Inscrit en
    Novembre 2004
    Messages
    208
    Détails du profil
    Informations personnelles :
    Âge : 40

    Informations forums :
    Inscription : Novembre 2004
    Messages : 208
    Points : 133
    Points
    133
    Par défaut
    à quel moment je précise le nom du fichier dans lequel je veux enregistrer?

  4. #4
    Membre éclairé
    Avatar de matazz
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    471
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 471
    Points : 668
    Points
    668
    Par défaut
    tu travaille avec .NET ou VC++ 6.0 ?
    Qui va piano va sano...

  5. #5
    Membre habitué Avatar de meli0207
    Inscrit en
    Novembre 2004
    Messages
    208
    Détails du profil
    Informations personnelles :
    Âge : 40

    Informations forums :
    Inscription : Novembre 2004
    Messages : 208
    Points : 133
    Points
    133
    Par défaut
    VC++ 6.0

  6. #6
    Membre éclairé
    Avatar de matazz
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    471
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 471
    Points : 668
    Points
    668
    Par défaut
    si t'est sous VC 6, ben il faut le coder :
    regarde :
    http://www.codeguru.com/Cpp/G-M/bitm...cle.php/c1697/

    Sinon, sous .NET u peut utiliser un objet CPicture je crois....
    Qui va piano va sano...

  7. #7
    Membre habitué Avatar de meli0207
    Inscrit en
    Novembre 2004
    Messages
    208
    Détails du profil
    Informations personnelles :
    Âge : 40

    Informations forums :
    Inscription : Novembre 2004
    Messages : 208
    Points : 133
    Points
    133
    Par défaut
    merci

    malheureusement, je n'arrive pas à faire marcher ce code, et je ne le comprend pas tres bien...

  8. #8
    Membre éclairé
    Avatar de matazz
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    471
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 471
    Points : 668
    Points
    668
    Par défaut
    c'est quoi que tu comprends pas ?
    Qui va piano va sano...

  9. #9
    Membre habitué Avatar de meli0207
    Inscrit en
    Novembre 2004
    Messages
    208
    Détails du profil
    Informations personnelles :
    Âge : 40

    Informations forums :
    Inscription : Novembre 2004
    Messages : 208
    Points : 133
    Points
    133
    Par défaut
    cette fonction prend un argument de type HANDLE, qu'est ce que je dois lui passer? moi j'ai un CDC* ...

  10. #10
    Membre habitué Avatar de meli0207
    Inscrit en
    Novembre 2004
    Messages
    208
    Détails du profil
    Informations personnelles :
    Âge : 40

    Informations forums :
    Inscription : Novembre 2004
    Messages : 208
    Points : 133
    Points
    133
    Par défaut
    je rappelle l'entete de la fonction:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    BOOL WriteDIB( LPTSTR szFile, HANDLE hDIB)
    pour le 2eme paramètre (hDIB) je passe un CDC*

    mais ca plante sur , alors j'ai remplacé ceci par Dans ce cas, l'image crée ne fait que 18o et je ne peut pas l'ouvrir...

  11. #11
    Membre éclairé
    Avatar de matazz
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    471
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 471
    Points : 668
    Points
    668
    Par défaut
    hDib n'est pas un pointeur sur un CDC mais sur HBITMAP

    Tu peut l'obtenir à partir d'un CBitmap associé à un MemDC comme expliqué dans le lien que je t'ai donné.

    GlobalSize ne doit pas être remplacé par sizeof car il te renvoi la taille de ton handle + les bits de l'image, alors que sizeof ne te renvoi que la taille du handle.
    Qui va piano va sano...

  12. #12
    Membre habitué Avatar de meli0207
    Inscrit en
    Novembre 2004
    Messages
    208
    Détails du profil
    Informations personnelles :
    Âge : 40

    Informations forums :
    Inscription : Novembre 2004
    Messages : 208
    Points : 133
    Points
    133
    Par défaut
    HBITMAP ou pointeur sur HBITMAP?

    avec un pointeur sur HBITMAP ca ne fait pas d'erreur mais l'image créée ne peut pas etre ouverte.

    avec un HBITMAP ca bloque sur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    lpbi = (LPBITMAPINFOHEADER)hDIB;
    int nColors = 1 << lpbi->biBitCount;
    je suppose que le cast ne se fait pas bien

  13. #13
    Membre éclairé
    Avatar de matazz
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    471
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 471
    Points : 668
    Points
    668
    Par défaut
    Citation Envoyé par meli0207
    HBITMAP ou pointeur sur HBITMAP?
    ...
    HBITMAP, autant pour moi...
    Qui va piano va sano...

  14. #14
    Membre éclairé
    Avatar de matazz
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    471
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 471
    Points : 668
    Points
    668
    Par défaut
    Citation Envoyé par meli0207
    ...
    avec un HBITMAP ca bloque sur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    lpbi = (LPBITMAPINFOHEADER)hDIB;
    int nColors = 1 << lpbi->biBitCount;
    je suppose que le cast ne se fait pas bien
    je vais essayer
    Qui va piano va sano...

  15. #15
    Membre éclairé
    Avatar de matazz
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    471
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 471
    Points : 668
    Points
    668
    Par défaut
    Citation Envoyé par meli0207
    ...
    avec un pointeur sur HBITMAP ca ne fait pas d'erreur mais l'image créée ne peut pas etre ouverte.
    ...
    Tu as une messagebox qui te dis que tu n'as pas le droit d'aceder au fichier ?

    car apparement il manque la fermeture du fichier dans le code du lien
    rajoute :
    avant
    Qui va piano va sano...

  16. #16
    Membre habitué Avatar de meli0207
    Inscrit en
    Novembre 2004
    Messages
    208
    Détails du profil
    Informations personnelles :
    Âge : 40

    Informations forums :
    Inscription : Novembre 2004
    Messages : 208
    Points : 133
    Points
    133
    Par défaut
    ca ne marche toujours pas.

    je n'ai pas la boite d'erreur que tu dis, mais un message "aucun apercu n'est disponible"

  17. #17
    Membre éclairé
    Avatar de matazz
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    471
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 471
    Points : 668
    Points
    668
    Par défaut
    Bon ben en fait c'est pas clair mais il faut utiliser plusieurs méthode de différents articles de codeguru.
    En fait il existe une foncion qui écrit directement une CDialog dans un fichier, pour ça utilise la Fonction WriteWindowToDIB passe this dans le param CWnd*
    Moi je les ai mis dnas ma classe outils et ça marche :
    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
     
    /*******************************************************************************/
    //LPTSTR szFile : DestFile
    //HANDLE hDIB  : Handle sur un DIB
    /*******************************************************************************/
    BOOL COutils::WriteDIB( LPTSTR szFile, HANDLE hDIB)
    	{
    	BITMAPFILEHEADER	hdr;
    	LPBITMAPINFOHEADER	lpbi;
     
    	if (!hDIB)
    		return FALSE;
     
    	CFile file;
    	if( !file.Open( szFile, CFile::modeWrite|CFile::modeCreate) )
    		return FALSE;
     
    	lpbi = (LPBITMAPINFOHEADER)hDIB;
     
    	int nColors = 1 << lpbi->biBitCount;
     
    	// Fill in the fields of the file header 
    	hdr.bfType		= ((WORD) ('M' << 8) | 'B');	// is always "BM"
    	hdr.bfSize		= GlobalSize (hDIB) + sizeof( hdr );
    	hdr.bfReserved1 	= 0;
    	hdr.bfReserved2 	= 0;
    	hdr.bfOffBits		= (DWORD) (sizeof( hdr ) + lpbi->biSize +
    						nColors * sizeof(RGBQUAD));
     
    	// Write the file header 
    	file.Write( &hdr, sizeof(hdr) );
     
    	// Write the DIB header and the bits 
    	file.Write( lpbi, GlobalSize(hDIB) );
     
    	file.Close();
     
    	return TRUE;
    	}
    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
     
    /*******************************************************************************/
    //WriteWindowToDIB
    //LPTSTR DestFile
    //Pointeur sur un CWnd
    /*******************************************************************************/
    BOOL COutils::WriteWindowToDIB( LPTSTR szFile, CWnd *pWnd )
    	{
    	CBitmap 	bitmap;
    	CWindowDC	dc(pWnd);
    	CDC 		memDC;
    	CRect		rect;
     
    	memDC.CreateCompatibleDC(&dc);
     
    	pWnd->GetWindowRect(rect);
     
    	bitmap.CreateCompatibleBitmap(&dc, rect.Width(),rect.Height() );
     
    	CBitmap* pOldBitmap = memDC.SelectObject(&bitmap);
    	memDC.BitBlt(0, 0, rect.Width(),rect.Height(), &dc, 0, 0, SRCCOPY);
     
    	// Create logical palette if device support a palette
    	CPalette pal;
    	if( dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE )
    	{
    		UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * 256);
    		LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
    		pLP->palVersion = 0x300;
     
    		pLP->palNumEntries =
    			GetSystemPaletteEntries( dc, 0, 255, pLP->palPalEntry );
     
    		// Create the palette
    		pal.CreatePalette( pLP );
     
    		delete[] pLP;
    	}
     
    	memDC.SelectObject(pOldBitmap);
     
    	// Convert the bitmap to a DIB
    	HANDLE hDIB = DDBToDIB( bitmap, BI_RGB, &pal );
     
    	if( hDIB == NULL )
    		return FALSE;
     
    	// Write it to file
    	WriteDIB( szFile, hDIB );
     
    	// Free the memory allocated by DDBToDIB for the DIB
    	GlobalFree( hDIB );
    	return TRUE;
        }
    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
     
    /*******************************************************************************/
    // DDBToDIB		- Creates a DIB from a DDB
    // bitmap		- Device dependent bitmap
    // dwCompression	- Type of compression - see BITMAPINFOHEADER
    // pPal			- Logical palette
    /*******************************************************************************/
    HANDLE COutils::DDBToDIB( CBitmap& bitmap, DWORD dwCompression, CPalette* pPal )
    	{
    	BITMAP			bm;
    	BITMAPINFOHEADER	bi;
    	LPBITMAPINFOHEADER 	lpbi;
    	DWORD			dwLen;
    	HANDLE			hDIB;
    	HANDLE			handle;
    	HDC 			hDC;
    	HPALETTE		hPal;
     
     
    	ASSERT( bitmap.GetSafeHandle() );
     
    	// The function has no arg for bitfields
    	if( dwCompression == BI_BITFIELDS )
    		return NULL;
     
    	// If a palette has not been supplied use defaul palette
    	hPal = (HPALETTE) pPal->GetSafeHandle();
    	if (hPal==NULL)
    		hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);
     
    	// Get bitmap information
    	bitmap.GetObject(sizeof(bm),(LPSTR)&bm);
     
    	// Initialize the bitmapinfoheader
    	bi.biSize		= sizeof(BITMAPINFOHEADER);
    	bi.biWidth		= bm.bmWidth;
    	bi.biHeight 		= bm.bmHeight;
    	bi.biPlanes 		= 1;
    	bi.biBitCount		= bm.bmPlanes * bm.bmBitsPixel;
    	bi.biCompression	= dwCompression;
    	bi.biSizeImage		= 0;
    	bi.biXPelsPerMeter	= 0;
    	bi.biYPelsPerMeter	= 0;
    	bi.biClrUsed		= 0;
    	bi.biClrImportant	= 0;
     
    	// Compute the size of the  infoheader and the color table
    	int nColors = (1 << bi.biBitCount);
    	if( nColors > 256 )
    		nColors = 0;
    	dwLen  = bi.biSize + nColors * sizeof(RGBQUAD);
     
    	// We need a device context to get the DIB from
    	hDC = GetDC(NULL);
    	hPal = SelectPalette(hDC,hPal,FALSE);
    	RealizePalette(hDC);
     
    	// Allocate enough memory to hold bitmapinfoheader and color table
    	hDIB = GlobalAlloc(GMEM_FIXED,dwLen);
     
    	if (!hDIB){
    		SelectPalette(hDC,hPal,FALSE);
    		ReleaseDC(NULL,hDC);
    		return NULL;
    	}
     
    	lpbi = (LPBITMAPINFOHEADER)hDIB;
     
    	*lpbi = bi;
     
    	// Call GetDIBits with a NULL lpBits param, so the device driver 
    	// will calculate the biSizeImage field 
    	GetDIBits(hDC, (HBITMAP)bitmap.GetSafeHandle(), 0L, (DWORD)bi.biHeight,
    			(LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);
     
    	bi = *lpbi;
     
    	// If the driver did not fill in the biSizeImage field, then compute it
    	// Each scan line of the image is aligned on a DWORD (32bit) boundary
    	if (bi.biSizeImage == 0){
    		bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)
    						* bi.biHeight;
     
    		// If a compression scheme is used the result may infact be larger
    		// Increase the size to account for this.
    		if (dwCompression != BI_RGB)
    			bi.biSizeImage = (bi.biSizeImage * 3) / 2;
    	}
     
    	// Realloc the buffer so that it can hold all the bits
    	dwLen += bi.biSizeImage;
    	if (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
    		hDIB = handle;
    	else{
    		GlobalFree(hDIB);
     
    		// Reselect the original palette
    		SelectPalette(hDC,hPal,FALSE);
    		ReleaseDC(NULL,hDC);
    		return NULL;
    	}
     
    	// Get the bitmap bits
    	lpbi = (LPBITMAPINFOHEADER)hDIB;
     
    	// FINALLY get the DIB
    	BOOL bGotBits = GetDIBits( hDC, (HBITMAP)bitmap.GetSafeHandle(),
    				0L,				// Start scan line
    				(DWORD)bi.biHeight,		// # of scan lines
    				(LPBYTE)lpbi 			// address for bitmap bits
    				+ (bi.biSize + nColors * sizeof(RGBQUAD)),
    				(LPBITMAPINFO)lpbi,		// address of bitmapinfo
    				(DWORD)DIB_RGB_COLORS);		// Use RGB for color table
     
    	if( !bGotBits )
    	{
    		GlobalFree(hDIB);
     
    		SelectPalette(hDC,hPal,FALSE);
    		ReleaseDC(NULL,hDC);
    		return NULL;
    	}
     
    	SelectPalette(hDC,hPal,FALSE);
    	ReleaseDC(NULL,hDC);
    	return hDIB;
    	}
    Qui va piano va sano...

  18. #18
    Membre habitué Avatar de meli0207
    Inscrit en
    Novembre 2004
    Messages
    208
    Détails du profil
    Informations personnelles :
    Âge : 40

    Informations forums :
    Inscription : Novembre 2004
    Messages : 208
    Points : 133
    Points
    133
    Par défaut
    avec cette méthode, l'image créée a une taille plus raisonnable (650Ko) mais lorsque j'essaie de l'ouvrir, j'ai un message "le dessin a échoué"

  19. #19
    Membre éclairé
    Avatar de matazz
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    471
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 471
    Points : 668
    Points
    668
    Par défaut
    chez moi ça marche !!!
    tu utilise WriteWindowToDIB ?
    Qui va piano va sano...

  20. #20
    Membre habitué Avatar de meli0207
    Inscrit en
    Novembre 2004
    Messages
    208
    Détails du profil
    Informations personnelles :
    Âge : 40

    Informations forums :
    Inscription : Novembre 2004
    Messages : 208
    Points : 133
    Points
    133
    Par défaut
    oui j'utilise WriteWindowToDIB

    voici mon 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
     
    void CInterface3Dlg::OnButton3() 
    {
    	MemDC   = new CDC();
       MemDC->CreateCompatibleDC(panel->dc); 
     
    	CBitmap *Bmp   = new CBitmap();
    	Bmp->CreateCompatibleBitmap(panel->dc,200,200);
     
      //Attacher le BMP au CDC;
    	HBITMAP OldBMP = (HBITMAP) MemDC->SelectObject((HBITMAP)Bmp->GetSafeHandle());
     
    	MemDC->BitBlt(0,0, 500, 500, panel->dc, 0, 0, SRCCOPY); 
    	WriteWindowToDIB("test.bmp", panel);
    	MessageBox("fini");
    }
    ce que j'appelle panel, c'est la CDialog en question

    J'ai repris tes 3 fonctions, j'ai juste enlevé COutils::

+ Répondre à la discussion
Cette discussion est résolue.
Page 1 sur 2 12 DernièreDernière

Discussions similaires

  1. [Image] Enregistrer une image jpg
    Par younes_ dans le forum Entrée/Sortie
    Réponses: 6
    Dernier message: 14/04/2011, 19h17
  2. ENREGISTRER UNE IMAGE OU UN FICHIER .DOC
    Par manudja dans le forum MS SQL Server
    Réponses: 1
    Dernier message: 03/08/2005, 13h32
  3. [GLScene] Comment enregistrer une image au format TGA
    Par Invité dans le forum API, COM et SDKs
    Réponses: 1
    Dernier message: 27/05/2005, 15h12
  4. Enregistrer une image
    Par N@sH dans le forum Entrée/Sortie
    Réponses: 4
    Dernier message: 04/05/2005, 10h07
  5. enregistrer une image et un identifiant
    Par dosach dans le forum MS SQL Server
    Réponses: 6
    Dernier message: 18/03/2004, 17h18

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