最简单的屏幕拷贝程序(象素拷贝) (转)

最简单的屏幕拷贝程序(象素拷贝) (转)[@more@]

//小弟工作之余写了一个程序

//注意,该程序非常占用cpu时间,它的唯好处就是简单,可以一个象素一个象素的操作

//您还可以根据需要改变象素的值达到特殊的效果。

//本程序在win2000下测试通过,可以获取256色以上个种分辨率的屏幕

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


int apiENTRY WinMain ( HINSTANCE hInstance,
  HINSTance hPrevInstance,
  LPSTR  lpCmdLine,
  int  nCmdShow)
{
 HDC  hdcScr;
 int  cx, cy;
  BITMAPFILEHEADER  BFH;
  BITMAPINFOHEADER  BIH;
 word 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 successfully!", "Congratulations", MB_OK);
 return 0;
}

 

 


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10748419/viewspace-958854/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10748419/viewspace-958854/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值