惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
博客园 - 司徒正美
博客园 - 【当耐特】
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
人人都是产品经理
人人都是产品经理
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
大猫的无限游戏
大猫的无限游戏
月光博客
月光博客
宝玉的分享
宝玉的分享
V
V2EX
S
SegmentFault 最新的问题
V
Visual Studio Blog
阮一峰的网络日志
阮一峰的网络日志
Martin Fowler
Martin Fowler
Jina AI
Jina AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园_首页
L
LangChain Blog
D
Docker
腾讯CDC

博客园 - yufun

Windows GUI自动化测试技术的比较和展望 双屏(Daul Monitor)很爽 ChromePlus很好用,已经替代我的Firefox了 很久没来了,更新一下状态 小议云计算和Live Mesh、网络存储 [转载] 我的测试观点与经验 获取当前操作系统的版本 - yufun - 博客园 获取当前执行的函数(Testcase)名称 C#中三种截屏方式总结 搬家完成 你是否知道-你可把代码段拖拽到Toolbox里边 跟UI自动化测试有关的技术 转载:关于开发和测试 转载:再谈UI自动化测试 转载:一个UI自动化的小例子 转载:用一个小例子来说明手工测试,自动化测试,系统命令,编程语言,API的关系 各类搜索网站 在自动化测试中,如果控件不能识别,你会怎么做? 在C#中如何模拟鼠标键盘操作 - yufun - 博客园
在C#中调用API进行截屏
yufun · 2009-01-12 · via 博客园 - yufun

看代码:

需要Reference下面的DLL:
  System.Drawing
  System.Windows.Forms
  WindowBase

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Text;
   4: using System.Windows;
   5: using System.Windows.Forms;
   6: using System.Drawing;
   7: using System.Runtime.InteropServices;
   8:  
   9: namespace ConsoleApplication1
  10: {
  11:     class Program
  12:     {
  13:         [System.Runtime.InteropServices.DllImport("gdi32.dll")]
  14:         public static extern IntPtr CreateDC(string driver, string device, IntPtr res1, IntPtr res2);
  15:  
  16:         public enum TernaryRasterOperations
  17:         {
  18:             SRCCOPY = 0x00CC0020, /* dest = source*/
  19:             SRCPAINT = 0x00EE0086, /* dest = source OR dest*/
  20:             SRCAND = 0x008800C6, /* dest = source AND dest*/
  21:             SRCINVERT = 0x00660046, /* dest = source XOR dest*/
  22:             SRCERASE = 0x00440328, /* dest = source AND (NOT dest )*/
  23:             NOTSRCCOPY = 0x00330008, /* dest = (NOT source)*/
  24:             NOTSRCERASE = 0x001100A6, /* dest = (NOT src) AND (NOT dest) */
  25:             MERGECOPY = 0x00C000CA, /* dest = (source AND pattern)*/
  26:             MERGEPAINT = 0x00BB0226, /* dest = (NOT source) OR dest*/
  27:             PATCOPY = 0x00F00021, /* dest = pattern*/
  28:             PATPAINT = 0x00FB0A09, /* dest = DPSnoo*/
  29:             PATINVERT = 0x005A0049, /* dest = pattern XOR dest*/
  30:             DSTINVERT = 0x00550009, /* dest = (NOT dest)*/
  31:             BLACKNESS = 0x00000042, /* dest = BLACK*/
  32:             WHITENESS = 0x00FF0062, /* dest = WHITE*/
  33:         };
  34:  
  35:         [DllImport("gdi32.dll")]
  36:         public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest, int nWidth,
  37:             int nHeight, IntPtr hObjSource, int nXSrc, int nYSrc, TernaryRasterOperations dwRop);
  38:  
  39:         public static void CaptureDesktop(string sPath)
  40:         {
  41:             Rect rect = new Rect();
  42:             rect.Width = Screen.PrimaryScreen.Bounds.Width;
  43:             rect.Height = Screen.PrimaryScreen.Bounds.Height;
  44:  
  45:             IntPtr dcTmp = CreateDC("DISPLAY", "DISPLAY", (IntPtr)null, (IntPtr)null);
  46:             Graphics gScreen = Graphics.FromHdc(dcTmp);
  47:             Bitmap image = new Bitmap((int)(rect.Width), (int)(rect.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
  48:             Graphics gImage = Graphics.FromImage(image);
  49:             IntPtr dcImage = gImage.GetHdc();
  50:             IntPtr dcScreen = gScreen.GetHdc();
  51:             BitBlt(dcImage, 0, 0, (int)(rect.Width), (int)(rect.Height), dcScreen, (int)(rect.Left), (int)(rect.Top), TernaryRasterOperations.SRCCOPY);
  52:             gScreen.ReleaseHdc(dcScreen);
  53:             gImage.ReleaseHdc(dcImage);
  54:  
  55:             image.Save(sPath);
  56:         }
  57:  
  58:         static void Main(string[] args)
  59:         {
  60:             CaptureDesktop("c:\\1.bmp");
  61:         }
  62:     }
  63: }