C 中三种截屏方式总结

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

 

分类 WinForm2009-2-6 12:53:19 
转自 YuFun's 自动化测试随笔

.

昨天写自动化测试的CASE的时候,碰到一个疑难杂症,调用截图的函数去截取一个Popup窗口,但是总是把背景程序给截下来,Popup窗口就跟看不到一样。本来以为是同步的问题,也就是以为先截图再点击弹出Popup窗口了。后来加了N个Thread.Sleep来测试,发现根本不是因为这个原因,而是截图的函数截不下来这个窗口。

这个为啥呢,只好把截图的函数代码翻出来看,以前是用这种方式的:
http://www.cnblogs.com/yufun/archive/2009/01/12/1374335.html

看代码:

需要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: }

BitBlt(dcImage, 0, 0, (int) (rect.Width), (int) (rect.Height), dcScreen, (int) (rect.Left), (int) (rect.Top), TernaryRasterOperations.SRCCOPY);

凭直觉感觉应该是因为这种通过DC的方式对WPF程序支持有问题,但是又觉得奇怪就是截取其它的WPF组件和窗口都没有问题,偏偏Popup窗口不行。

前些天听说另外一种截屏的方法,这种方法连被遮挡的窗口都可以截,于是就Google一大把,找打了PrintWindow函数,于是就有了第二种解决方案,代码如下:
IntPtr hdc = Native.GetWindowDC(this.Handle);if (hdc != IntPtr.Zero){    IntPtr hdcMem = Native.CreateCompatibleDC(hdc);    if (hdcMem != IntPtr.Zero)    {        IntPtr hbitmap = Native.CreateCompatibleBitmap(hdc, (int) (Rect.Width), (int) (Rect.Height));        if (hbitmap != IntPtr.Zero)        {            Native.SelectObject(hdcMem, hbitmap);            Native.PrintWindow(this.Handle, hdcMem, 0);            Native.DeleteObject(hbitmap);            Bitmap bmp = Bitmap.FromHbitmap(hbitmap);            bmp.Save(sPath);        }        Native.DeleteObject(hdcMem);    }    Native.ReleaseDC(this.Handle, hdc);}

就是拿到窗口的句柄,通过PrintWindow API来截取窗口。

但是更让人气愤的事情出现了,截出来的窗口中,只要是用到WPF组件的地方,全部是黑块儿,只有MFC的窗口框架和按钮可以正常被截取。

于是乎,就无奈的继续分析这个问题,我记得WPF是没有走GDI,而是通过Directx渲染的,那就是说DC的方式和PrintWindow的方式都不靠谱,但是截Directx的貌似还比较复杂。

突然想起来,平常报bug的时候都是按PrintScreen,然后再处理一下的,那应该PrintScreen按键是管用的,看来只能曲线救国了。但是那样就得走剪切板了,貌似会破坏剪切板的数据,不过如果我在截取前保存一下数据,在截取后再恢复一下剪切板数据,那就没有问题了。

于是就有了第三种解决方案(暂时还没有加恢复剪切板数据的代码):
const uint KEYEVENTF_EXTENDEDKEY = 0x1;const uint KEYEVENTF_KEYUP = 0x2;const byte VK_SNAPSHOT = 0x2C;Native.keybd_event(VK_SNAPSHOT, 0x45, KEYEVENTF_EXTENDEDKEY, UIntPtr.Zero);Native.keybd_event(VK_SNAPSHOT, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, UIntPtr.Zero);IDataObject iObj = Clipboard.GetDataObject();if (iObj.GetDataPresent(DataFormats.Bitmap, true)){    Bitmap bmpScreen = iObj.GetData(DataFormats.Bitmap, true) as Bitmap;    Bitmap bmpOutput = new Bitmap((int) this.Rect.Width, (int) this.Rect.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);    Graphics g = Graphics.FromImage(bmpOutput);    Rectangle destRectangle = new Rectangle(0, 0, (int) this.Rect.Width, (int) this.Rect.Height);    g.DrawImage(bmpScreen, destRectangle, (int) this.Rect.X, (int) this.Rect.Y, (int) this.Rect.Width, (int) this.Rect.Height, GraphicsUnit.Pixel);    bmpOutput.Save(sPath, System.Drawing.Imaging.ImageFormat.Bmp);}

测试可用,只好先用着了

不过还有几个问题,先写下来,留待以后解决:

1. 针对第三种方案,既然可以按PrintScreen键截图,那对应的API是什么,总觉得发键盘消息没有直接调API稳定

2. 针对WPF截图有没有更好的解决方案

回复补充
public static Bitmap CopyPrimaryScreen(){    Screen s = Screen.PrimaryScreen;    Rectangle r = s.Bounds;    int w = r.Width;    int h = r.Height;    Bitmap bmp = new Bitmap(w, h);    Graphics g = Graphics.FromImage(bmp);    g.CopyFromScreen    (    new Point(0, 0),    new Point(0, 0),    new Size(w, h)    );    return bmp;}
 
///  <summary>  ///  ///  </summary>  ///  <param  name="sFromFilePath">原始图片</param>  ///  <param  name="saveFilePath">生成新图</param>  ///  <param  name="width">截取图片宽度</param>  ///  <param  name="height">截取图片高度</param>  ///  <param  name="spaceX">截图图片X坐标</param>  ///  <param  name="spaceY">截取图片Y坐标</param>  public static void CaptureImage(string sFromFilePath, string saveFilePath, int width, int height, int spaceX, int spaceY)  {  //载入底图  Image fromImage = Image.FromFile(sFromFilePath);  int x = 0//截取X坐标  int y = 0;  //截取Y坐标  //原图宽与生成图片宽  之差  //当小于0(即原图宽小于要生成的图)时,新图宽度为较小者  即原图宽度  X坐标则为0  //当大于0(即原图宽大于要生成的图)时,新图宽度为设置值  即width  X坐标则为  sX与spaceX之间较小者  //Y方向同理 
           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值