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
| // header
#include "screen_shot.h"
// Initialisation des données
Snapshot::Snapshot(void)
{
m_pBitmapInfoHeader = NULL;
m_pBitmapInfo = NULL;
m_pBitmapData = NULL;
m_hDcBitmap = NULL;
m_hBitmap = NULL;
}
// Destructeur de class
Snapshot::~Snapshot(void)
{
// Effacer les variables dynamiques
delete[] m_pBitmapInfo;
delete[] m_pBitmapData;
// Free memory.
DeleteDC(m_hDcBitmap);
DeleteObject(m_hBitmap);
}
// Prise Capture du bureau
bool Snapshot::TakeDesktopSnapshot(void)
{
// Récupération du HWND et du HDC du bureau
HWND hWndWindow = GetDesktopWindow();
HDC hDcWindow = GetWindowDC(hWndWindow);
// On récupére les dimensions du bureau
int ScreenX = GetDeviceCaps(hDcWindow, HORZRES);
int ScreenY = GetDeviceCaps(hDcWindow, VERTRES);
// Création d'un contexte mémoire
m_hDcBitmap = CreateCompatibleDC(hDcWindow);
if(!m_hDcBitmap) return 0;
// On crée un bitmap en mémoire ayant les dimensions du bureau
HBITMAP hBitmap = CreateCompatibleBitmap(hDcWindow, ScreenX, ScreenY);
if(!hBitmap) return 0;
m_hBitmap = hBitmap;
// On sélectionne ce bitmap dans le contexte mémoire
if(!SelectObject(m_hDcBitmap, hBitmap)) return 0;
// On effectue une copie du contexte écran vers le contexte mémoire
if(!BitBlt(m_hDcBitmap, 0, 0, ScreenX, ScreenY, hDcWindow, 0, 0, SRCCOPY)) return 0;
// Maintenant on crée la structure BITMAPINFO
BITMAP Bitmap;
WORD Couleur;
// Récupération de la structure BITMAP
if (!GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap)) return 0;
// Convertie le format de couleur
Couleur = (WORD)(Bitmap.bmPlanes * Bitmap.bmBitsPixel);
if (Couleur == 1)
Couleur = 1;
else if (Couleur <= 4)
Couleur = 4;
else if (Couleur <= 8)
Couleur = 8;
else if (Couleur <= 16)
Couleur = 16;
else if (Couleur <= 24)
Couleur = 24;
else Couleur = 32;
// Alloue de la memoire pour la structure BITMAPINFO (Cette structure
// contient une structure BITMAPINFOHEADER et un tableau de RGBQUAD
if (Couleur != 24)
m_pBitmapInfo = (PBITMAPINFO) new BYTE[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * (1<< Couleur)];
// Il n'y a pas de tableau de RGBQUAD pour le format 24 bit
else
m_pBitmapInfo = (PBITMAPINFO) new BYTE[sizeof(BITMAPINFOHEADER)];
if(!m_pBitmapInfo) return 0;
// Initialise la structure BITMAPINFO
m_pBitmapInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
m_pBitmapInfo->bmiHeader.biWidth = Bitmap.bmWidth;
m_pBitmapInfo->bmiHeader.biHeight = Bitmap.bmHeight;
m_pBitmapInfo->bmiHeader.biPlanes = Bitmap.bmPlanes;
m_pBitmapInfo->bmiHeader.biBitCount = Bitmap.bmBitsPixel;
if (Couleur < 24)
m_pBitmapInfo->bmiHeader.biClrUsed = (1<<Couleur);
// If the bitmap is not compressed, set the BI_RGB flag.
m_pBitmapInfo->bmiHeader.biCompression = BI_RGB;
m_pBitmapInfo->bmiHeader.biSizeImage = ((m_pBitmapInfo->bmiHeader.biWidth * Couleur +31) & ~31) /8 * m_pBitmapInfo->bmiHeader.biHeight;
// Set biClrImportant to 0, indicating that all of the
// device colors are important.
m_pBitmapInfo->bmiHeader.biClrImportant = 0;
//initialise le reste des structures...
m_pBitmapInfoHeader = (PBITMAPINFOHEADER) m_pBitmapInfo;
m_pBitmapData = new BYTE[m_pBitmapInfoHeader->biSizeImage];
if (!m_pBitmapData) return 0;
// Retrieve the color table (RGBQUAD array) and the bits
// (array of palette indices) from the DIB.
if (!GetDIBits(m_hDcBitmap, hBitmap, 0, (WORD) m_pBitmapInfoHeader->biHeight, m_pBitmapData, m_pBitmapInfo, DIB_RGB_COLORS)) return 0;
m_BitmapFileHeader.bfType = 0x4d42; // 0x42 = "B" 0x4d = "M"
// Compute the size of the entire file.
m_BitmapFileHeader.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) + m_pBitmapInfoHeader->biSize + m_pBitmapInfoHeader->biClrUsed * sizeof(RGBQUAD) + m_pBitmapInfoHeader->biSizeImage);
m_BitmapFileHeader.bfReserved1 = 0;
m_BitmapFileHeader.bfReserved2 = 0;
// Compute the offset to the array of color indices.
m_BitmapFileHeader.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + m_pBitmapInfoHeader->biSize + m_pBitmapInfoHeader->biClrUsed * sizeof (RGBQUAD);
return 1;
}
// Sauvegarde de la capture dans un fichier
bool Snapshot::SaveToFile(const char* Path)
{
HANDLE hFichier;
DWORD dwTmp;
// Create the .BMP file.
hFichier = CreateFile(Path, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFichier == INVALID_HANDLE_VALUE) return 0;
// Copy the BITMAPFILEHEADER into the .BMP file.
if (!WriteFile(hFichier, &m_BitmapFileHeader, sizeof(BITMAPFILEHEADER), &dwTmp, NULL)) return 0;
// Copy the BITMAPINFOHEADER and RGBQUAD array into the file.
if (!WriteFile(hFichier, m_pBitmapInfoHeader, sizeof(BITMAPINFOHEADER) + m_pBitmapInfoHeader->biClrUsed * sizeof (RGBQUAD), &dwTmp, NULL)) return 0;
// Copy the array of color indices into the .BMP file.
if (!WriteFile(hFichier, (LPSTR) m_pBitmapData, (int) m_pBitmapInfoHeader->biSizeImage, &dwTmp, NULL)) return 0;
// Close the .BMP file.
if (!CloseHandle(hFichier)) return 0;
return 1;
}
bool Snapshot::SaveToHDC(HDC &hDc)
{
// Création d'un contexte mémoire
hDc = CreateCompatibleDC(m_hDcBitmap);
if(!hDc) return 0;
// On crée un bitmap en mémoire ayant les dimensions du bureau
HBITMAP hBitmap = CreateCompatibleBitmap(m_hDcBitmap, GetWidth(), GetHeight());
if(!hBitmap) return 0;
// On sélectionne ce bitmap dans le contexte mémoire
if(!SelectObject((HDC)hDc, hBitmap)) return 0;
if(!BitBlt((HDC)hDc, 0, 0, GetWidth(), GetHeight(), m_hDcBitmap, 0, 0, SRCCOPY)) return 0;
return 1;
}
// longueur capture
int Snapshot::GetWidth()
{
return m_pBitmapInfo->bmiHeader.biWidth;
}
// largeur capture
int Snapshot::GetHeight()
{
return m_pBitmapInfo->bmiHeader.biHeight;
} |
Partager