屏幕及窗口拷贝

以下来自于:http://www.codeguru.com/forum/showthread.php?t=244907

 

#define PW_WINDOW          9001
#define PW_CLIENT             9002

HBITMAP CMyProgramDlg::CopyScreenToBitmap(LPRECT lpRect)
{
 HDC         hScrDC, hMemDC;         // screen DC and memory DC    
 int         nX, nY, nX2, nY2;       // coordinates of rectangle to grab    
 int         nWidth, nHeight;        // DIB width and height    
 int         xScrn, yScrn;           // screen resolution     

 HGDIOBJ     hOldBitmap , hBitmap;
  
  // check for an empty rectangle
    if (IsRectEmpty(lpRect))      
    return NULL;     
    // create a DC for the screen and create    
    // a memory DC compatible to screen DC         

   hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);    
   hMemDC = CreateCompatibleDC(hScrDC);      // get points of rectangle to grab 
  
   nX = lpRect->left;    
   nY = lpRect->top;    
   nX2 = lpRect->right;    
   nY2 = lpRect->bottom;      // get screen resolution     
  
   xScrn = GetDeviceCaps(hScrDC, HORZRES);    
   yScrn = GetDeviceCaps(hScrDC, VERTRES);     
  
   //make sure bitmap rectangle is visible     
  
   if (nX < 0)        
   nX = 0;    
  
   if (nY < 0)        
      nY = 0;    
  
   if (nX2 > xScrn)        
      nX2 = xScrn;    
  
   if (nY2 > yScrn)        
      nY2 = yScrn;     

   nWidth = nX2 - nX;    
   nHeight = nY2 - nY;     
  
   // create a bitmap compatible with the screen DC    
  
   hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);     
  
   // select new bitmap into memory DC    
  
   hOldBitmap =   SelectObject (hMemDC, hBitmap);     
  
   // bitblt screen DC to memory DC    
  
   BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nX, nY, SRCCOPY);    
  
   // select old bitmap back into memory DC and get handle to    
   // bitmap of the screen         
  
   hBitmap = SelectObject(hMemDC, hOldBitmap);     

   // clean up     
  
   DeleteDC(hScrDC);    
   DeleteDC(hMemDC);     
 
   // return handle to the bitmap     
  
   return (HBITMAP)hBitmap;
}

HBITMAP CYourProgramDlg::CopyWindowToBitmap(CWnd* wnd , HWND hWnd, WORD fPrintArea)
{
 HBITMAP     hBitmap = NULL;  // handle to device-dependent bitmap     
 // check for a valid window handle     
 if (!hWnd)        
     return NULL;

    RECT    rectWnd;
    ::GetWindowRect(hWnd, &rectWnd); 

 switch (fPrintArea)    
 {        
  case PW_WINDOW: // copy entire window        
  {            
          // get the window rectangle             
          // get the bitmap of that window by calling            
    // CopyScreenToBitmap and passing it the window rect                    
//    GetWindowRect(&rectWnd);
    hBitmap = CopyScreenToBitmap(&rectWnd);            
    break;        
  }  
       case PW_CLIENT: // copy client area        
    {            
      RECT rectClient;            
      POINT pt1, pt2;              // get client dimensions             
      wnd->GetClientRect(&rectClient);              // convert client coords to screen coords             
      pt1.x = rectClient.left;            
      pt1.y = rectClient.top;            
      pt2.x = rectClient.right;            
      pt2.y = rectClient.bottom;            
      wnd->ClientToScreen(&pt1);            
      wnd->ClientToScreen(&pt2);            
      rectClient.left = pt1.x;            
      rectClient.top = pt1.y;            
      rectClient.right = pt2.x;            
      rectClient.bottom = pt2.y; 

         // get the bitmap of the client area by calling            
      // CopyScreenToBitmap and passing it the client rect                         
      hBitmap = CopyScreenToBitmap(&rectClient);            
      break;        
    }             
    default:    // invalid print area            
         return NULL;    
 }      // return handle to the bitmap    
 
 return hBitmap;
}A good usage is :

Code:
HBITMAP hBitmap = CopyWindowToBitmap(this,GetDesktopWindow()->m_hWnd,PW_

要在 C# WinForm 窗口中实现屏幕录制,可以使用 .NET Framework 中提供的 `System.Drawing` 命名空间下的 `Screen` 和 `Graphics` 类,以及 `System.Windows.Forms` 命名空间下的 `Timer` 控件。 具体步骤如下: 1. 在窗口中添加 `PictureBox` 控件,用于显示录制的屏幕内容。 2. 添加一个 `Timer` 控件,设置其 `Interval` 属性为需要录制的帧率(例如 30 毫秒)。 3. 在 `Timer.Tick` 事件中,使用 `Graphics.CopyFromScreen` 方法将屏幕内容拷贝到 `PictureBox` 控件中。 4. 启动 `Timer` 控件,开始录制屏幕。 以下是一个简单的示例代码: ```csharp public partial class Form1 : Form { private Timer timer; private Bitmap bitmap; public Form1() { InitializeComponent(); timer = new Timer(); timer.Interval = 30; // 30毫秒一帧 timer.Tick += Timer_Tick; } private void Timer_Tick(object sender, EventArgs e) { if (bitmap == null) { bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); } using (var g = Graphics.FromImage(bitmap)) { g.CopyFromScreen(0, 0, 0, 0, bitmap.Size); pictureBox1.Image = bitmap; } } private void button1_Click(object sender, EventArgs e) { timer.Start(); // 开始录制 } private void button2_Click(object sender, EventArgs e) { timer.Stop(); // 停止录制 } } ``` 在这个示例中,通过每隔 30 毫秒执行一次 `Timer_Tick` 事件,并在其中将屏幕内容拷贝到 `PictureBox` 控件中,实现了屏幕录制的功能。通过点击 `button1` 和 `button2` 按钮来开始和停止录制。注意,由于屏幕录制可能会消耗大量的系统资源,建议在录制时关闭其他不必要的应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值