snipix Combien dites-vous ? 30 THz ? | Salut,
Faut créer une bitmap, c'est le plus simple a faire, je dois avoir un exemple qaui traine.............
Code :
- Save()
- {
- int iColor = GetBitColor(); // Fonction qui donne le nombre de bit voulut -> met 24
- DWORD dwSize = m_ImageSizeX * m_ImageSizeY * iColor;
- DWORD dwTmp = 0;
- BITMAPFILEHEADER hdr;
- HANDLE hf = NULL;
- BITMAPINFO *pBitmap = NULL;
- if (iColor == DEF_NB_COLOR_8)
- {
- LOGPALETTE* plp = NULL;
- plp = (LOGPALETTE *)malloc (sizeof(LOGPALETTE) + 256 * sizeof(PALETTEENTRY));
- plp->palVersion = 0x0300;
- plp->palNumEntries = 256;
- for (int i = 0; i < 256; i++)
- {
- // Creation d'une palette 256 niveau de gris
- plp->palPalEntry[i].peRed = i;
- plp->palPalEntry[i].peGreen = i;
- plp->palPalEntry[i].peBlue = i;
- plp->palPalEntry[i].peFlags = 0;
- }
- pBitmap = (BITMAPINFO*) malloc(sizeof(BITMAPINFO)+256*sizeof(RGBQUAD));
- memset(pBitmap, 0, sizeof(BITMAPINFO));
- memcpy(pBitmap->bmiColors, plp->palPalEntry, 256*sizeof(RGBQUAD));
- free(plp);
- pBitmap->bmiHeader.biClrUsed = 256;
- pBitmap->bmiHeader.biClrImportant = 0;
- hdr.bfType = 0x4d42; // 0x42 = "B" 0x4d = "M"
- // Compute the size of the entire file.
- hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFO) + (256 * sizeof(RGBQUAD)) + dwSize);
- hdr.bfReserved1 = 0;
- hdr.bfReserved2 = 0;
- // Compute the offset to the array of color indices.
- hdr.bfOffBits = (DWORD)(sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFO) + (256 * sizeof(RGBQUAD)));
- }
- else
- {
- pBitmap = (BITMAPINFO*) malloc(sizeof(BITMAPINFO));
- memset(pBitmap,0,sizeof(BITMAPINFO));
- hdr.bfType = 0x4d42; // 0x42 = "B" 0x4d = "M"
- // Compute the size of the entire file.
- hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFO) + dwSize);
- hdr.bfReserved1 = 0;
- hdr.bfReserved2 = 0;
- // Compute the offset to the array of color indices.
- hdr.bfOffBits = (DWORD)(sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFO)) ;
- }
- pBitmap->bmiHeader.biBitCount = 8 * iColor;
- pBitmap->bmiHeader.biPlanes = 1;
- pBitmap->bmiHeader.biHeight = m_ImageSizeY;
- pBitmap->bmiHeader.biWidth = m_ImageSizeX;
- pBitmap->bmiHeader.biSizeImage = dwSize;
- pBitmap->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
- pBitmap->bmiHeader.biCompression = BI_RGB;
- // Create the .BMP file.
- hf = CreateFile(csFile, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hf != INVALID_HANDLE_VALUE)
- {
- BOOL bDetruire = TRUE;
- // Copy the BITMAPFILEHEADER into the .BMP file.
- WriteFile(hf, (LPVOID)&hdr, sizeof(BITMAPFILEHEADER), (LPDWORD)&dwTmp, NULL);
- if (dwTmp == sizeof(BITMAPFILEHEADER))
- {
- // Copy the BITMAPINFOHEADER and RGBQUAD array into the file.
- if (iColor == DEF_NB_COLOR_8)
- {
- WriteFile(hf, (LPVOID)pBitmap, sizeof(BITMAPINFO) + (256 * sizeof(RGBQUAD)), &dwTmp, NULL);
- }
- else
- {
- WriteFile(hf, (LPVOID)pBitmap, sizeof(BITMAPINFO), &dwTmp, NULL);
- }
- if (dwTmp == (iColor == DEF_NB_COLOR_8 ? sizeof(BITMAPINFO) + (256 * sizeof(RGBQUAD)) : sizeof(BITMAPINFO)))
- {
- // Copy the array of color indices into the .BMP file.
- WriteFile(hf, m_pImage->GetImagePtr(), dwSize, &dwTmp, NULL);
- if (dwTmp == dwSize)
- {
- bRet = TRUE;
- bDetruire = FALSE;
- }
- else if (!bQuiet)
- {
- AfxMessageBox("Impossible d'écrire les données de l'image.", MB_ICONSTOP);
- }
- }
- else if (!bQuiet)
- {
- AfxMessageBox("Impossible d'écrire les informations de la palette dans l'image.", MB_ICONSTOP);
- }
- }
- else if (!bQuiet)
- {
- AfxMessageBox("Impossible d'écrire l'entête de l'image.", MB_ICONSTOP);
- }
- // Close the .BMP file.
- CloseHandle(hf);
- if (bDetruire)
- {
- DeleteFile(csFile);
- }
- }
- free(pBitmap);
- }
- Load()
- {
- if (m_pPicture)
- {
- m_pPicture->Release();
- m_pPicture = NULL;
- }
- // handle sur le fichier selectionné
- HANDLE hFile = CreateFile(csFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
- if (INVALID_HANDLE_VALUE != hFile)
- {
- // Taille du fichier
- DWORD dwFileSize = GetFileSize(hFile, NULL);
- if (-1 != dwFileSize)
- {
- // allouer la memoire a partir de la taille du fichier
- HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
- if (hGlobal)
- {
- LPVOID pvData = GlobalLock(hGlobal);
- if (pvData)
- {
- DWORD dwBytesRead = 0;
- // lecture du fichier et stockage dans la memoire
- BOOL bRead = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL);
- GlobalUnlock(hGlobal);
- if (bRead)
- {
- LPSTREAM pstm = NULL;
- // creation IStream* a partir de la memoire
- HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm);
- if(SUCCEEDED(hr) && pstm)
- {
- // Creation IPicture a partit du du flux IStream*
- hr = ::OleLoadPicture(pstm, dwFileSize, FALSE, IID_IPicture, (LPVOID *)&m_pPicture);
- if (SUCCEEDED(hr) && m_pPicture)
- {
- short sType;
- hr = m_pPicture->get_Type(&sType);
- if (SUCCEEDED(hr))
- {
- if (sType == PICTYPE_BITMAP)
- {
- HBITMAP hBitmap;
- hr = m_pPicture->get_Handle((OLE_HANDLE *) &hBitmap);
- if (SUCCEEDED(hr) && hBitmap)
- {
- RedrawWindow();
- }
- }
- }
- }
- pstm->Release();
- }
- }
- else if (!bQuiet)
- {
- AfxMessageBox("Impossible de lire le fichier image.", MB_ICONSTOP);
- }
- }
- GlobalFree(hGlobal);
- }
- }
- CloseHandle(hFile);
- }
- }
|
---------------
La raison du plus fort est toujours la plus forte.... Parce que c'est comme ça NA !
|