原文地址:http://blog.csdn.net/u013096568/article/details/53400389

panel上可以通过DrawToBitmap截图,不管是否在屏幕外是否有遮挡

 Bitmap sourceBitmap = new Bitmap(400, 300);
//Control ct = frmMain.mianForm.panel1 as Control; //ct.DrawToBitmap(sourceBitmap, new System.Drawing.Rectangle(0, 0, 400, 300));
panel1.DrawToBitmap(sourceBitmap, new System.Drawing.Rectangle(0, 0, 400, 300));
sourceBitmap.Save(@"e:\123form2.bmp");

在图片上打水印

string strpath = @"e:\1.bmp";
Bitmap bmp = new Bitmap(strpath);
Graphics graphics = Graphics.FromImage(bmp); System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
graphics.FillEllipse(myBrush, new System.Drawing.Rectangle(150, 200, 10, 10));//画圆 try
{
bmp.Save(@"e:\new.bmp");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

  

抓屏截图

   Image objImage = new Bitmap(400, 300);
Graphics g = Graphics.FromImage(objImage);
g.CopyFromScreen(new System.Drawing.Point(Cursor.Position.X - 150, Cursor.Position.Y - 25), new System.Drawing.Point(0, 0), new Size(400, 300));
IntPtr dc1 = g.GetHdc();
g.ReleaseHdc(dc1); objImage.Save("e:\\test.jpg");

  

非顶端窗口截图

用Windows热键截图然后保存的我就不说了,地球人都知道.

如何截取非前端窗体的截图,要先获取所要截图的窗口的句柄IntPtr PicWindow = this.Handle

首先说一下PrintWindow这个API的使用

  public static Bitmap GetWindowCapture(IntPtr hWnd)
{
IntPtr hscrdc = GetWindowDC(hWnd);
RECT windowRect = new RECT();
GetWindowRect(hWnd, ref windowRect);
int width = windowRect.right - windowRect.left;
int height = windowRect.bottom - windowRect.top; IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, width, height);
IntPtr hmemdc = CreateCompatibleDC(hscrdc);
SelectObject(hmemdc, hbitmap);
PrintWindow(hWnd, hmemdc, 0);
Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
DeleteDC(hscrdc);//删除用过的对象
DeleteDC(hmemdc);//删除用过的对象
return bmp;
} [DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect); [DllImport("gdi32.dll")]
public static extern IntPtr CreateDC(
string lpszDriver, // driver name驱动名
string lpszDevice, // device name设备名
string lpszOutput, // not used; should be NULL
IntPtr lpInitData // optional printer data
);
[DllImport("gdi32.dll")]
public static extern int BitBlt(
IntPtr hdcDest, // handle to destination DC目标设备的句柄
int nXDest, // x-coord of destination upper-left corner目标对象的左上角的X坐标
int nYDest, // y-coord of destination upper-left corner目标对象的左上角的Y坐标
int nWidth, // width of destination rectangle目标对象的矩形宽度
int nHeight, // height of destination rectangle目标对象的矩形长度
IntPtr hdcSrc, // handle to source DC源设备的句柄
int nXSrc, // x-coordinate of source upper-left corner源对象的左上角的X坐标
int nYSrc, // y-coordinate of source upper-left corner源对象的左上角的Y坐标
UInt32 dwRop // raster operation code光栅的操作值
); [DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(
IntPtr hdc // handle to DC
); [DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleBitmap(
IntPtr hdc, // handle to DC
int nWidth, // width of bitmap, in pixels
int nHeight // height of bitmap, in pixels
); [DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(
IntPtr hdc, // handle to DC
IntPtr hgdiobj // handle to object
); [DllImport("gdi32.dll")]
public static extern int DeleteDC(
IntPtr hdc // handle to DC
); [DllImport("user32.dll")]
public static extern bool PrintWindow(
IntPtr hwnd, // Window to copy,Handle to the window that will be copied.
IntPtr hdcBlt, // HDC to print into,Handle to the device context.
UInt32 nFlags // Optional flags,Specifies the drawing options. It can be one of the following values.
); [DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(
IntPtr hwnd
);

  

很遗憾,上面的确可以截取非前端窗体的截图,但是非GDI的程序是无法截图的比如DirectX

下面说一下BitBlt这个API的使用

  /// <summary>
/// 提供全屏和指定窗口的截图 以及保存为文件的类
/// </summary>
public class ScreenCapture
{
/// <summary>
/// 全屏截图
/// </summary>
/// <returns></returns>
public Image CaptureScreen()
{
return CaptureWindow(User32.GetDesktopWindow());
}
/// <summary>
/// 指定窗口截图
/// </summary>
/// <param name="handle">窗口句柄. (在windows应用程序中, 从Handle属性获得)</param>
/// <returns></returns>
public Image CaptureWindow(IntPtr handle)
{
IntPtr hdcSrc = User32.GetWindowDC(handle);
User32.RECT windowRect = new User32.RECT();
User32.GetWindowRect(handle, ref windowRect);
int width = windowRect.right - windowRect.left;
int height = windowRect.bottom - windowRect.top;
IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);
GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);
GDI32.SelectObject(hdcDest, hOld);
GDI32.DeleteDC(hdcDest);

  

C# 非顶端窗口截图 - 用于查找指定窗口并截图的更多相关文章

  1. [bash]用于查找指定格式目录的程序

    功能: 在指定目录下查找符合yyyy-MM-dd(-d)nnn模式的目录名,例如2020-03-22-b888 目标目录情况: [root@localhost testfolder]# ll tota ...

  2. 背水一战 Windows 10 (122) - 其它: 通过 Windows.System.Profile 命名空间下的类获取信息, 查找指定类或接口的所在程序集的所有子类和子接口

    [源码下载] 背水一战 Windows 10 (122) - 其它: 通过 Windows.System.Profile 命名空间下的类获取信息, 查找指定类或接口的所在程序集的所有子类和子接口 作者 ...

  3. 如何在pyqt中在实现无边框窗口的同时保留Windows窗口动画效果(一)

    无边框窗体的实现思路 在pyqt中只要 self.setWindowFlags(Qt.FramelessWindowHint) 就可以实现边框的去除,但是没了标题栏也意味着窗口大小无法改变.窗口无法拖 ...

  4. Windows 系统上用 .NET/C# 查找所有窗口,并获得窗口的标题、位置、尺寸、最小化、可见性等各种状态

    原文:Windows 系统上用 .NET/C# 查找所有窗口,并获得窗口的标题.位置.尺寸.最小化.可见性等各种状态 在 Windows 应用开发中,如果需要操作其他的窗口,那么可以使用 EnumWi ...

  5. 初识TypeScript:查找指定路径下的文件按类型生成json

    如果开发过node.js的话应该对js(javascript)非常熟悉,TypeScript(以下简称ts)是js的超集. 下面是ts的官网: https://www.tslang.cn/ 1.环境配 ...

  6. 菜鸟学Linux命令:lsof命令 查找指定用户、进程、端口打开的文件

    lsof,list open files, 是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件. 命令格式:ls ...

  7. 项目总结03:window.open()方法用于子窗口数据回调至父窗口,即子窗口操作父窗口

    window.open()方法用于子窗口数据回调至父窗口,即子窗口操作父窗口 项目中经常遇到一个业务逻辑:在A窗口中打开B窗口,在B窗口中操作完以后关闭B窗口,同时自动刷新A窗口(或局部更新A窗口)( ...

  8. 在eclipse中查找指定文件 [多种方法]

    在eclipse中查找指定文件   1.ctrl+h打开搜索界面 File Search: containing text填*,File name patterns填写hello.*,可以找到hell ...

  9. Windows实现桌面录屏、指定窗口录制直播,低延时,H5页面播放

    接着前面记录的3种方式实现桌面推流直播: 1.Windows 11实现录屏直播,搭建Nginx的rtmp服务 的方式需要依赖与Flash插件,使用场景有限 2.Windows 11实现直播,VLC超简 ...

随机推荐

  1. Create a DAC from a microcontroller's ADC

    Few microcontrollers include a DAC. Although you can easily find an inexpensive DAC to control from ...

  2. [转].net reactor 学习系列(三)---.net reactor代码自动操作相关保护功能

    接上篇,上篇已经学习了界面的各种功能以及各种配置,这篇准备学习下代码控制许可证. 代码控制许可证的意思就是软件经过.net reactor保护后,到期时客户端就需要购买许可证,这时软件开发商就需要生成 ...

  3. perf 函数调用性能(函数流程图)

    perf record -g -p pid perf record -g -t tid perf report -g fractal,0.2,caller perf report -g fractal ...

  4. 【mybatis】mybatis动态order by 的问题, 注意 只需要把#{} 改成 ${} 即可

    先说解决方案: 注意  只需要把#{} 改成 ${}  即可 再看 使用过程: Mapper.java List<IntegralGoods> findInUid(@Param(" ...

  5. ListVIew点击事件失效

    转自:http://blog.csdn.net/zhufuing/article/details/8677407 记录下自己所犯的错误,在写ListView的点击事件时OnItemClickListe ...

  6. php 验证身份证号码

    身份证号码的结构 身份证号码是特征组合码,由17位数字本体码和一位校验码组成. 排列顺序从左至右依此为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码. 地址码(前六位数) 表示编 ...

  7. Spring持久化

    1. Spring的DAO理念 Spring提供了一套抽象的DAO类,供开发者扩展,这有利于以统一的方式操作各种DAO技术,如JDO.JDBC等,这些抽象DAO类提供了设置数据源及相关辅助信息的方法, ...

  8. @MySQL中length字符长度函数使用方法

    MySQL里面的length函数是一个用来获取字符串长度的内置函数,一个汉字是算三个字符,中文的标点符号也是算三个字符,一个数字或字母算一个字符.具体用法示例如下: 1.查看某字符串的长度 SELEC ...

  9. go语言基础之工程管理和工作区介绍

    1.工程管理 在实际的开发工作中,直接调用编译器进行编译和链接的场景是少而又少,因为在工程中不会简单到只有一个源代码文件,且源文件之间会有相互的依赖关系.如果这样一个文件一个文件逐步编译,那不亚于一场 ...

  10. jquery判断滚动条是否到底部

    clientHeight:这个元素的高度,占用整个空间的高度,所以,如果一个div有滚动条,那个这个高度则是不包括滚动条没显示出来的下面部分的内容.而只是单纯的DIV的高度. offsetHeight ...