黑马程序员技术交流社区

标题: 如何实时截取桌面信息 [打印本页]

作者: 徐艳勇    时间: 2012-10-16 10:04
标题: 如何实时截取桌面信息
如何实时截取桌面信息,并生成数据流
作者: 李辰东    时间: 2012-10-16 13:45
截取桌面图像,调用Graphics的CopyFromScreen方法可以实现

操作代码:

             Graphics gSrc = this.CreateGraphics();     //创建窗体的Graphics对象
             HandleRef hDcSrc = new HandleRef(null, gSrc.GetHdc());

             int width = this.Width-SystemInformation.FrameBorderSize.Width;     //获取宽度
             int height = this.Height-SystemInformation.FrameBorderSize.Height;     //获取高度

             const int SRCCOPY = 0xcc0020;     //复制图块的光栅操作码

             Bitmap bmSave = new Bitmap(width, height);     //用于保存图片的位图对象
             Graphics gSave = Graphics.FromImage(bmSave);     //创建该位图的Graphics对象
             HandleRef hDcSave = new HandleRef(null, gSave.GetHdc());     //得到句柄

             BitBlt(hDcSave, 0, 0, width, height, hDcSrc, 0, 0, SRCCOPY);

     如此操作,当前窗体的图像即被保存之bmSave中。

详细的代码:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using System.Runtime.InteropServices;  
  9.    
  10. namespace ScreenShot  
  11. {  
  12.      public partial class Form1 : Form  
  13.      {  
  14.          public Form1()  
  15.          {  
  16.              InitializeComponent();  
  17.          }  
  18.    
  19.          private void btnScreen_Click(object sender, EventArgs e)  
  20.          {  
  21.              int screenWidth = System.Windows.Forms.SystemInformation.VirtualScreen.Width;     //屏幕宽度  
  22.              int screenHeight = System.Windows.Forms.SystemInformation.VirtualScreen.Height;     //屏幕高度  
  23.    
  24.              Bitmap bmSave = new Bitmap(screenWidth, screenHeight);  
  25.              Graphics g = Graphics.FromImage(bmSave);  
  26.    
  27.              g.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight), CopyPixelOperation.SourceCopy);  
  28.              g.Dispose();  
  29.    
  30.              SaveFile(bmSave);  
  31.          }  
  32.    
  33.          private void btnWindow_Click(object sender, EventArgs e)  
  34.          {  
  35.              Graphics gSrc = this.CreateGraphics();     //创建窗体的Graphics对象  
  36.              HandleRef hDcSrc = new HandleRef(null, gSrc.GetHdc());  
  37.    
  38.              int width = this.Width-SystemInformation.FrameBorderSize.Width;     //获取宽度  
  39.              int height = this.Height-SystemInformation.FrameBorderSize.Height;     //获取高度  
  40.    
  41.              const int SRCCOPY = 0xcc0020;     //复制图块的光栅操作码  
  42.    
  43.              Bitmap bmSave = new Bitmap(width, height);     //用于保存图片的位图对象  
  44.              Graphics gSave = Graphics.FromImage(bmSave);     //创建该位图的Graphics对象  
  45.              HandleRef hDcSave = new HandleRef(null, gSave.GetHdc());     //得到句柄  
  46.    
  47.              BitBlt(hDcSave, 0, 0, width, height, hDcSrc, 0, 0, SRCCOPY);  
  48.    
  49.              gSrc.ReleaseHdc();  
  50.              gSave.ReleaseHdc();  
  51.    
  52.              gSrc.Dispose();  
  53.              gSave.Dispose();  
  54.    
  55.              SaveFile(bmSave);  
  56.          }  
  57.          private void SaveFile(Bitmap bmSave)  
  58.          {  
  59.              if (DialogResult.OK == saveFileDialog1.ShowDialog())  
  60.              {  
  61.                  string fileName = saveFileDialog1.FileName;  
  62.                  if (1 == saveFileDialog1.FilterIndex)  
  63.                  {  
  64.                      if (!fileName.EndsWith(".bmp"))  
  65.                      {  
  66.                          fileName += ".bmp";  
  67.                      }  
  68.                  }  
  69.                  else if (2 == saveFileDialog1.FilterIndex)  
  70.                  {  
  71.                      if (!fileName.EndsWith(".jpg"))  
  72.                      {  
  73.                          fileName += ".jpg";  
  74.                      }  
  75.                  }  
  76.                  else if (3 == saveFileDialog1.FilterIndex)  
  77.                  {  
  78.                      if (!fileName.EndsWith(".png"))  
  79.                      {  
  80.                          fileName += ".png";  
  81.                      }  
  82.                  }  
  83.                  Save(bmSave, fileName);  
  84.              }  
  85.          }  
  86.          private void Save(Bitmap bm,string fileName)  
  87.          {  
  88.              if (fileName.EndsWith(".bmp"))  
  89.              {  
  90.                  bm.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp);  
  91.              }  
  92.              else if (fileName.EndsWith(".jpg"))  
  93.              {  
  94.                  bm.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);  
  95.              }  
  96.              else if (fileName.EndsWith(".png"))  
  97.              {  
  98.                  bm.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);  
  99.              }  
  100.              bm.Dispose();  
  101.          }  
  102.    
  103.          [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]  
  104.          public static extern int BitBlt(HandleRef hDC, int x, int y, int nWidth, int nHeight, HandleRef hSrcDC, int xSrc, int ySrc, int dwRop);  
  105.      }  
  106. }   
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2