c#截屏功能的实现
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LC
{
class ScreenCapture
{
#region 抓取屏幕
/// <summary>
/// 抓取屏幕(层叠的窗口)
/// </summary>
/// <param name="x">左上角的横坐标</param>
/// <param name="y">左上角的纵坐标</param>
/// <param name="width">抓取宽度</param>
/// <param name="height">抓取高度</param>
/// <returns></returns>
public static Bitmap captureScreen(int x, int y, int width, int height)
{
Bitmap bmp = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.CopyFromScreen(new Point(x, y), new Point(0, 0), bmp.Size);
g.Dispose();
}
//bit.Save(@"capture2.png");
return bmp;
}
/// <summary>
/// 抓取整个屏幕
/// </summary>
/// <returns></returns>
public static Bitmap captureScreen()
{
Size screenSize = Screen.PrimaryScreen.Bounds.Size;
return captureScreen(0,0,screenSize.Width,screenSize.Height);
}
#endregion
#region 使用BitBlt方法抓取控件,无论控件是否被遮挡
/// <summary>
/// 控件(窗口)的截图,控件被其他窗口(而非本窗口内控件)遮挡时也可以正确截图,使用BitBlt方法
/// </summary>
/// <param name="control">需要被截图的控件</param>
/// <returns>该控件的截图,控件被遮挡时也可以正确截图</returns>
public static Bitmap captureControl(Control control)
{
//调用API截屏
IntPtr hSrce = GetWindowDC(control.Handle);
IntPtr hDest = CreateCompatibleDC(hSrce);
IntPtr hBmp = CreateCompatibleBitmap(hSrce, control.Width, control.Height);
IntPtr hOldBmp = SelectObject(hDest, hBmp);
if (BitBlt(hDest, 0, 0, control.Width, control.Height, hSrce, 0, 0, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt))
{
Bitmap bmp = Image.FromHbitmap(hBmp);
SelectObject(hDest, hOldBmp);
DeleteObject(hBmp);
DeleteDC(hDest);
ReleaseDC(control.Handle, hSrce);
// bmp.Save(@"a.png");
// bmp.Dispose();
return bmp;
}
return null;
}
// /// <summary>
// /// 有问题!!!!!用户区域坐标不对啊
// /// 控件(窗口)的用户区域截图,控件被其他窗口(而非本窗口内控件)遮挡时也可以正确截图,使用BitBlt方法
// /// </summary>
// /// <param name="control">需要被截图的控件</param>
// /// <returns>控件(窗口)的用户区域截图</returns>
// public static Bitmap captureClientArea(Control control)
// {
//
// Size sz = control.Size;
// Rectangle rect = control.ClientRectangle;
//
//
// //调用API截屏
// IntPtr hSrce = GetWindowDC(control.Handle);
// IntPtr hDest = CreateCompatibleDC(hSrce);
// IntPtr hBmp = CreateCompatibleBitmap(hSrce, rect.Width, rect.Height);
// IntPtr hOldBmp = SelectObject(hDest, hBmp);
// if (BitBlt(hDest, 0, 0, rect.Width, rect.Height, hSrce, rect.X, rect.Y, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt))
// {
// Bitmap bmp = Image.FromHbitmap(hBmp);
// SelectObject(hDest, hOldBmp);
// DeleteObject(hBmp);
// DeleteDC(hDest);
// ReleaseDC(control.Handle, hSrce);
// // bmp.Save(@"a.png");
// // bmp.Dispose();
// return bmp;
// }
// return null;
//
// }
#endregion
#region 使用PrintWindow方法抓取窗口,无论控件是否被遮挡
/// <summary>
/// 窗口的截图,窗口被遮挡时也可以正确截图,使用PrintWindow方法
/// </summary>
/// <param name="control">需要被截图的窗口</param>
/// <returns>窗口的截图,控件被遮挡时也可以正确截图</returns>
public static Bitmap captureWindowUsingPrintWindow(Form form)
{
return GetWindow(form.Handle);
}
private static Bitmap GetWindow(IntPtr hWnd)
{
IntPtr hscrdc = GetWindowDC(hWnd);
Control control = Control.FromHandle(hWnd);
IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, control.Width, control.Height);
IntPtr hmemdc = CreateCompatibleDC(hscrdc);
SelectObject(hmemdc, hbitmap);
PrintWindow(hWnd, hmemdc, 0);
Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
DeleteDC(hscrdc);//删除用过的对象
DeleteDC(hmemdc);//删除用过的对象
return bmp;
}
#endregion
#region DLL calls
[DllImport("gdi32.dll")]
static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int
wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, CopyPixelOperation rop);
[DllImport("gdi32.dll")]
static extern IntPtr DeleteDC(IntPtr hDc);
[DllImport("gdi32.dll")]
static extern IntPtr DeleteObject(IntPtr hDc);
[DllImport("gdi32.dll")]
static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
[DllImport("gdi32.dll")]
static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("gdi32.dll")]
static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp);
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr ptr);
[DllImport("user32.dll")]
public static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, UInt32 nFlags);
[DllImport("user32.dll")]
static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDc);
#endregion
}
}
c#截屏功能的实现的更多相关文章
- PhantomJS linux系统下安装步骤及使用方法(网页截屏功能)
PhantomJS 是一个基于 WebKit 的服务器端 JavaScript API.它全面支持web而不需浏览器支持,其快速,原生支持各种Web标准: DOM 处理, CSS 选择器, JSON, ...
- Atitit截屏功能的设计解决方案
Atitit截屏功能的设计解决方案 自己实现.... 使用快捷键.. 弹出自己的win,,背景是屏幕快照 点击鼠标光标变成十字状态 出现截屏窗口调整截屏窗口位置与大小 释放鼠标,三个btn,, 复制 ...
- android4.3 截屏功能的尝试与失败分析
1.背景 上一篇讲了在源码中捕获到了android手机的截屏函数(同时按下电源键与音量减,详情http://blog.csdn.net/buptgshengod/article/details/199 ...
- iOS截屏功能
代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // ...
- 使用laravel框架与phantomjs实现截屏功能
在网上看到的关于phantomjs实现截屏功能很多都是与node结合在一起使用,并需要输入命令才能执行.因此我想要实现输入网址即可截屏并输出图片的功能.示例:http://120.77.171.182 ...
- Android自己定义截屏功能,相似QQ截屏
由于公司业务需求 须要对一个屏幕进行截屏.但自带的截屏功能是远远不够项目的功能需求 ,我们是做一个画板软件 .须要的像QQ那样截屏之后 ,能够看到我们自己定义的工具.有画笔,button等等 .and ...
- 【玩转cocos2d-x之三十九】Cocos2d-x 3.0截屏功能集成
3.0的截屏和2.x的截屏基本上同样.都是利用RenderTexture来处理,在渲染之前调用call函数,然后调用Cocos的场景visit函数对其进行渲染,渲染结束后调用end函数就可以.仅仅是3 ...
- [置顶] Android 应用内禁止截屏功能的实现
截图介绍 Android的调试工具DDMS提供有截屏功能,很多软件也会有截屏功能,在做支付等安全类应用的时候,为了保证用户的资产和系统安全,往往会禁止应用内截屏,禁止之后,在此应用处于前台的情况下 ...
- 小胖说事22-----iOS开发技巧之取消键盘响应和截屏功能
1.UILable内容模糊 在非Retina的iPad mini 的屏幕上,一个UILable的frame的origin值假设是有小数位(如0.5),就会造成显示模糊,所以不妨用整数值的origin. ...
- c# wpf 利用截屏键实现截屏功能
原文:c# wpf 利用截屏键实现截屏功能 最近做一个wpf程序需要截图功能,查找资料费了一些曲折,跟大家分享一下. 先是找到了这样一份代码: static class Scr ...
随机推荐
- iOS第三方地图-百度地图常用功能使用(POI搜索,地理正反编码,定位,添加标注)
百度地图官网: http://developer.baidu.com/map/index.php?title=iossdk 百度地图集成 1.引入相关包
- HDU 4821 字符串hash
题目大意: 希望找到连续的长为m*l的子串,使得m个l长的子串每一个都不一样,问能找到多少个这样的子串 简单的字符串hash,提前预处理出每一个长度为l的字符串的hash值 #include < ...
- POJ 3248 Catch That Cow
http://poj.org/problem?id=3278 二维BFS #include <iostream> #include <stdio.h> #include < ...
- CODEVS5565 二叉苹果树
n<=100个点的根为1的二叉树,树边上有苹果,求保留Q<=n条边的最多苹果数. 树形DP,f[i][j]--节点i为根的子树保留j条边最优方案,f[i][0]=0,f[i][j]=max ...
- iOS 用cornerstone 创建分支
第一步:在cornerstone中添加服务器上的代码路径,如下: 第二步:选中服务器路径下的代码,选择“分支”按钮 第三步:开始创建分支 第四步:效果图如下 第五步:选择分支下的路径下载代码并进行修改 ...
- 牛客网暑期ACM多校训练营(第九场) A题 FWT
链接:https://www.nowcoder.com/acm/contest/147/A来源:牛客网 Niuniu has recently learned how to use Gaussian ...
- Javascript标准事件模型
本文为原创,转载请注明出处: cnzt 文章:cnzt-p http://www.cnblogs.com/zt-blog/p/6676913.html 1. 分类 IE -- 冒泡型 现代 ...
- xml文件的schema也是经过jdk编译器编译的,如果xsd没引入完整,而xml中又用到了这些标签,就会编译不通过啊。
1.xml文件的schema也是经过jdk编译器编译的,如果xsd没引入完整,而xml中又用到了这些标签,就会编译不通过啊. 2.java编译器会下载xsd的指定链接文件,加在代码里,一起编译
- weblogic集群的资料
博客分类: weblogic 其实网上关于weblogic集群的资料非常多[大部分都是从创建新的domain开始,我这篇先介绍怎么样把原本普通的domain改造为集群环境],如果觉得不够,可以啃web ...
- Meteor集合
在本教程中,我们将学习如何使用 MongoDB集合. 创建集合 我们可以使用以下代码来创建一个新的集合- meteorApp/client/main.js MyCollection = new Mon ...