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 ...
随机推荐
- 洛谷P1521 求逆序对 题解
题意: 求1到n的全排列中有m对逆序对的方案数. 思路: 1.f[i][j]表示1到i的全排列中有j对逆序对的方案数. 2.显然,1到i的全排列最多有(i-1)*i/2对逆序对,而对于f[i][j]来 ...
- POJ3621 Sightseeing Cows【最短路】
题目大意:在一个无向图里找一个环,是的点权和除以边权和最大 思路:UVA11090姊妹题 事实上当这题点权和都为1时就是上一题TUT #include <stdio.h> #include ...
- [转]eclipse的android智能提示设置
以往 我们往往在输入 "." 然后 alt+/ 来进行智能提示,下面这个方法,可以帮你大幅度的提高智能打开 Eclipse -> Window -> Perferenc ...
- Iahub and Permutations(codeforces 314c)
题意:给出一组排列,某些位置不知道(-1),要求求出有多少种还原方式,使得所有a[i]!=i /* 这是一道关于排列的动态规划,这种体大都可以当作棋盘来做,如果把i这个数放到第j个位置,那么就将棋盘的 ...
- 洛谷P3094 [USACO13DEC]假期计划Vacation Planning
题目描述 有N(1 <= N <= 200)个农场,用1..N编号.航空公司计划在农场间建立航线.对于任意一条航线,选择农场1..K中的农场作为枢纽(1 <= K <= 100 ...
- 积累js中的一些问题及解决方案
一.取字符串的第i位不兼容的问题 1.问题:对于字符串str来说,要获取第i位,常见的是str[i],但是在低版本的浏览器中不兼容,例如ie7. 2.解决:使用str.charAt(i); 二.使用定 ...
- UVA 11806 组合数学+容斥
UVA: https://vjudge.net/problem/UVA-11806 AC代码 #include <bits/stdc++.h> #define pb push_back # ...
- 基于数据库的代码自动生成工具,生成JavaBean、生成数据库文档、生成前后端代码等(v6.0.0版)
TableGo v6.0.0 版震撼发布,此次版本更新如下: 1.UI界面大改版,组件大调整,提升界面功能的可扩展性. 2.新增BeautyEye主题,界面更加清新美观,也可以通过配置切换到原生Jav ...
- 解决idea中启动tomcat出现控制台乱码问题
尝试了很多方法,最后终于解决了,现在提供给大家一个我认为最简单也最有效的方案. 1.修改配置文件 找到idea的安装目录,在bin文件夹下找到以下两个文件,用记事本或者其他软件打开: 然后两个文件中都 ...
- Go --- 设计模式(模板模式)
模版模式真的是一个好东西.所谓模版模式,就是说,某几个类中相同的操作和代码提取到父类的一个函数中,并定义相同的操作为抽象函数.由子类来实现.估计我也没表达清楚,下面还是看代码来讲解吧. 例:我们有两个 ...