Codice PHP:
#include <string.h>
#include <sstream> //Per stampare le variabili sui box
#include <string> //Per stampare le variabili sui box
#include <stdlib.h> //Random
#include <time.h>
#include <conio.h>
#include <string.h>
#include <time.h>
#include <windows.h>
#include <stdio.h>
#include <stdexcept>
#include <cstdlib> //To enable writing on a file
#include <fstream> //To enable writing on a file
using namespace std;
// Rappresenta un pixel in una DIB a 24 bpp
struct DIB24BPPPixel
{
BYTE Blue;
BYTE Green;
BYTE Red;
};
void ControllaSchermo1(void);
POINT coord; /* struct contenente le coordinate xy del mouse */
string s;
string c;
stringstream out;
int x, y;
char * charRGBstring;
void Stampa (string);
int main()
{
ControllaSchermo1();
return 0;
}
void Stampa (string s)
{
FILE *file;
//MessageBox(NULL,"","",0);
charRGBstring = new char[9869757];
charRGBstring = (char*) s.c_str();
file = fopen("C:\\Users\\Niko\\Desktop\\RGB.txt","a");
fprintf(file,charRGBstring,0);
fclose(file);
}
void ControllaSchermo1()
{ // MessageBox(NULL, "Controlla schermo", "Cubo", 0);
// I device context
HDC dibDC=NULL, screenDC=NULL;
// Larghezza e altezza dello schermo (e di conseguenza della DIB)
LONG width, height;
// Bitmap selezionata all'inizio in dibDC, DIB Section
HBITMAP oldBmp=NULL, dibSection=NULL;
// Puntatore ai dati della DIB
LPBYTE dibBits=NULL;
// Struttura utilizzata per inizializzare la DIB
BITMAPINFO bi={0};
try
{
// I device context
HDC dibDC=NULL, screenDC=NULL;
// Larghezza e altezza dello schermo (e di conseguenza della DIB)
LONG width, height;
// Bitmap selezionata all'inizio in dibDC, DIB Section
HBITMAP oldBmp=NULL, dibSection=NULL;
// Puntatore ai dati della DIB
LPBYTE dibBits=NULL;
// Struttura utilizzata per inizializzare la DIB
BITMAPINFO bi={0};
// Lunghezza di una riga della DIB in byte; ci torna utile averlo pronto visto che le righe devono essere allineate a 32 bit
size_t bytesPerLine=0;
// Ottiene le dimensioni dello schermo
width = (LONG) GetSystemMetrics(SM_CXFULLSCREEN);
height = (LONG) GetSystemMetrics(SM_CYFULLSCREEN);
// Calcola la lunghezza di una riga della DIB
bytesPerLine = ((width * 24 + 31) & (~31)) / 8;
// Ottiene l'handle al DC dello schermo
screenDC = GetDC(0);
if(screenDC == NULL)
throw std::runtime_error("Impossibile ottenere il DC dello schermo; GetDC ha restituito NULL.");
// Inizializza i dati della DIB
bi.bmiHeader.biSize=sizeof(bi.bmiHeader);
bi.bmiHeader.biWidth=width;
bi.bmiHeader.biHeight=height;
bi.bmiHeader.biPlanes=1;
bi.bmiHeader.biBitCount=24; /* per semplicità usiamo una DIB a 24 bpp qualunque sia la profondità di colore dello schermo */
bi.bmiHeader.biCompression=BI_RGB;
// Crea la DIB section
dibSection=CreateDIBSection(NULL, &bi, DIB_RGB_COLORS, (LPVOID *)&dibBits, NULL, 0);
if(dibSection == NULL)
throw std::runtime_error("Impossibile creare la DibSection; CreateDIBSection ha restituito NULL.");
// Crea il DC
dibDC = CreateCompatibleDC(NULL);
if(dibDC == NULL)
throw std::runtime_error("Impossibile creare il device context; CreateCompatibleDC ha restituito NULL.");
// Seleziona la DIB section nel DC e salva la bitmap di default attualmente contenuta nel DC
oldBmp == SelectObject(dibDC, dibSection);
for(int d=0;d<1;d++)
{ //MessageBox(NULL, "CICLO PIXEL", "Pixel", MB_ICONINFORMATION);
// Ciclo da ripetere per ogni controllo dello schermo
// Copia il contenuto dello schermo nella DIB section
BitBlt(dibDC, 0,0, width, height, screenDC, 0, 0, SRCCOPY);
// Qui controlla i pixel della DIB
for(y = 0; y < 800; y++)
{
// Ottiene un puntatore al primo pixel della riga
DIB24BPPPixel * rowPtr = (DIB24BPPPixel *) (dibBits+(height-1-y)*bytesPerLine);
for(x = 0; x < 1280; x++)
{
// Verifica il colore del pixel
// Esempio:
s += rowPtr[x].Red;
s += rowPtr[x].Green;
s += rowPtr[x].Blue;
if(x == 0){Stampa(s);
s = "";}
out.str("");
out << y;
c = out.str();
//if(y>=3&&x==100)MessageBox(NULL,c.c_str(), "y", 0);
}
}//fine for
} //fine else
// Cleanup
ReleaseDC(NULL, screenDC);
SelectObject(dibDC, oldBmp);
DeleteDC(dibDC);
DeleteObject(dibSection);
}
catch(...)
{
// Cleanup in caso di errore
if( screenDC != NULL)
ReleaseDC(NULL, screenDC);
if(dibDC != NULL)
{
SelectObject(dibDC, oldBmp);
DeleteDC(dibDC);
}
if(dibSection != NULL)
DeleteObject(dibSection);
// Propaga l'eccezione
throw;
}
}
Il codice analizza i pixel dello schermo e dovrebbe stampare in totale esattamente 1280*800*3 byte. Gli errori sono 2.