最簡單的螢幕複製程式(象素複製) (轉)

gugu99發表於2007-08-15
最簡單的螢幕複製程式(象素複製) (轉)[@more@]

////////////////////////////////////////////////

//小弟工作之餘寫了一個,

//注意,該程式非常佔用時間,它的唯好處就是簡單,可以一個象素一個象素的操作

//您還可以根據需要改變象素的值達到特殊的效果。

//本程式在下測試透過,可以獲取256色以上個種解析度的螢幕

//自定義RGB象素Structure
typedef struct tagRBBREQUIRED
{
 BYTE rgbBlue;
 BYTE rgbGREEN;
 BYTE rgbRed;
} RGBREQUIRED, *PRGBREQUIRED;


int ENTRY WinMain ( HINSTANCE hInstance,
  HINSTance hPrevInstance,
  LPSTR  lpCmdLine,
  int  nCmdShow)
{
 HDC  hdcScr;
 int  cx, cy;
  BITMAPFILEHEADER  BFH;
  BITMAPINFOHEADER  BIH;
  wType;
 LONG lSize;
 LONG lPixNum;
 BYTE byte;

 PRGBREQUIRED pPixel = NULL;
 fstream  *out;


 cx = GetSystemMetrics ( SM_CXSCREEN );
 cy = GetSystemMetrics ( SM_CYSCREEN );

 lSize = sizeof ( RGBREQUIRED ) * cy * cx;
 lPixNum = cy * cx ;

 byte  = 'M';
 wType = (byte << 8);
 byte  = 'B';
 wType =  wType + byte;
 

//填寫點陣圖資訊頭
 BIH.biSize = sizeof (BIH);
 BIH.biHeight = cy;
 BIH.biWidth  = cx;
 BIH.biClrImportant = 0;
 BIH.biClrUsed  = 0;
 BIH.biCompresion  = BI_RGB;
 BIH.biPlaned  = 1;
 BIH.biSizeImage  = lSize + 44;
 BIH.biXPelsPerMeter = 3780;
 BIH.biYPelsPerMeter = 3780;
 BIH.biBitCount  = 24;

//填寫點陣圖頭

 BFH.bfType = wType;
 BFH.bfSize = lSize;
 BFH.bfOffBits = sizeof ( BFH ) + sizeof ( BIH );
 BFH.bfReserved1 = 0;
 BFH.bfReserved2 = 0;

 hdcScr = GetDC ( NULL );
 if ( !hdcScr )
 {
 MessageBox ( NULL, "Can't get the screen handle", "Error", MB_OK );
 return 0;
 }

 pPixel = new RGBREQUIRED[lPixNum];

 if ( !pPixel )

{

  MessageBox ( NULL, "Can't alloc memory", "Error", MB_OK);

  return 0;

 }
 memset ( ( void * ) pPixel, 0, lSize );
 PRGBREQUIRED pTemp = pPixel;
 RGBREQUIRED  TmpPix;
 DWORD  colorRef;
 int  i, j;

 for ( i=cy-1; i>=0; i-- )
 for ( j=0  ; j {
 colorRef = GetPixel ( hdcScr, j, i ); //獲取象素的色彩值00BBGGRR
 TmpPix.rgbBlue  = (BYTE) ( ( colorRef & 0x00ff0000 ) >> 16 );
 TmpPix.rgbGreen = (BYTE) ( ( colorRef & 0x0000ff00 ) >> 16 );
 TmpPix.rgbRed  = (BYTE)  ( colorRef & 0x000000ff ); 
 *pTemp = TmpPix;
 }

//儲存檔案

 out = new fstream ( "screen.bmp", ios::out|ios::binary, filebuf::sh_none );
 
 if ( !out )
 {

 delete pPixel;

  pPixel = NULL;

  pTemp  = NULL;
 MessageBox ( NULL, "Can't open file", "Error", MB_OK );
 return 0;
 }

 out->write ( ( char* )&BFH, sizeof ( BITMAPFILEHEADER ) ); //寫入位件頭
 out->write ( ( char* )&BIH, sizeof ( BITMAPINFOHEADER ) ); //寫入點陣圖資訊頭
 out->write ( ( char* )&pPixel, lSize );

 out->close();

 delete pPixel;
 pPixel = NULL;
 pTemp  = NULL;

 MessageBox ( NULL, "Copy screen succesully!", "Congratulations", MB_OK);
 return 0;
}

 

 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10748419/viewspace-958854/,如需轉載,請註明出處,否則將追究法律責任。

相關文章