c# 根据窗口截图,合并图片
c# 根据窗口截图,合并图片
public class CaptureWindows
{
#region 类
/// <summary>
/// Helper class containing User32 API functions
/// </summary>
private class User32
{
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
} private class GDI32
{ public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter
[DllImport("gdi32.dll")]
public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,
int nWidth, int nHeight, IntPtr hObjectSource,
int nXSrc, int nYSrc, int dwRop);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth,
int nHeight);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
}
#endregion [DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); /// <summary>
/// 根据句柄名截图
/// </summary>
/// <param name="handle">句柄名</param>
/// <returns></returns>
public Image CaptureWindow(IntPtr handle)
{
// get te hDC of the target window
IntPtr hdcSrc = User32.GetWindowDC(handle);
// get the size
User32.RECT windowRect = new User32.RECT();
User32.GetWindowRect(handle, ref windowRect);
int width = windowRect.right - windowRect.left;
int height = windowRect.bottom - windowRect.top;
// create a device context we can copy to
IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
// create a bitmap we can copy it to,
// using GetDeviceCaps to get the width/height
IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
// select the bitmap object
IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);
// bitblt over
GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);
// restore selection
GDI32.SelectObject(hdcDest, hOld);
// clean up
GDI32.DeleteDC(hdcDest);
User32.ReleaseDC(handle, hdcSrc);
// get a .NET image object for it
Image img = Image.FromHbitmap(hBitmap);
// free up the Bitmap object
GDI32.DeleteObject(hBitmap);
return img;
} /// <summary>
/// 根据窗口名称截图
/// </summary>
/// <param name="windowName">窗口名称</param>
/// <returns></returns>
public Image CaptureWindow(string windowName)
{
IntPtr handle = FindWindow(null, windowName);
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);
User32.ReleaseDC(handle, hdcSrc);
Image img = Image.FromHbitmap(hBitmap);
GDI32.DeleteObject(hBitmap);
return img;
}
/// <summary>
/// 合并图片
/// </summary>
/// <param name="firstImage">第一张图片</param>
/// <param name="lastImage">第二张图片</param>
/// <returns></returns>
public Image MergerImage(Image firstImage, Image lastImage)
{
int width = firstImage.Width + lastImage.Width ;
int height = firstImage.Height > lastImage.Height ? firstImage.Height : lastImage.Height;
////创建要显示的图片对象,根据参数的个数设置宽度
Bitmap backgroudImg = new Bitmap(width, height);
Graphics g = Graphics.FromImage(backgroudImg);
////清除画布,背景设置为白色
g.Clear(System.Drawing.Color.White);
g.DrawImageUnscaled(firstImage, 0, 0);
g.DrawImageUnscaled(lastImage, firstImage.Width, 0);
g.Dispose();
return backgroudImg;
} }
c# 根据窗口截图,合并图片的更多相关文章
- Ocr答题辅助神器 OcrAnswerer4.x,通过百度OCR识别手机文字,支持屏幕窗口截图和ADB安卓截图,支持四十个直播App,可保存题库
http://www.cnblogs.com/Charltsing/p/OcrAnswerer.html 联系qq:564955427 最新版为v4.1版,开放一定概率的八窗口体验功能,请截图体验(多 ...
- C# 非顶端窗口截图 - 用于查找指定窗口并截图
原文地址:http://blog.csdn.net/u013096568/article/details/53400389 panel上可以通过DrawToBitmap截图,不管是否在屏幕外是否有遮挡 ...
- Python+selenium之窗口截图
自动化用例是由程序去执行,因此有时候打印的错误信息并不明确,如果在脚本执行错误的时候能对当前窗口截图保存,那么通过图片就可以非常直观的看出出错的原因.webdriver提供了截图函数get_scree ...
- Selenium常用API的使用java语言之20-获取窗口截图
自动化用例是由程序去执行,因此有时候打印的错误信息并不十分明确.如果在脚本执行出错的时候能对当前窗口截图保存,那么通过图片就可以非常直观地看出出错的原因. WebDriver提供了截图函数getScr ...
- Selenium 2自动化测试实战23(窗口截图)
一.窗口截图 WebDriver提供了截图函数get_screenshot_as_file()来截取当前窗口. # -*- coding: utf-8 -*- from selenium import ...
- Selenium3 + Python3自动化测试系列十二——窗口截图与关闭浏览器
窗口截图 自动化用例是由程序去执行的,因此有时候打印的错误信息并不十分明确.如果在脚本执行出错的时候能对当前窗口截图保存,那么通过图片就可以非常直观地看出出错的原因.WebDriver提供了截图函数g ...
- selenium窗口截图操作
selenium窗口截图操作 使用背景:在自动化测试过程中,可能遇到执行错误情况,那么怎么样及时捕获出错信息/界面? 可以使用 get_screenshot_as_file(self,filenam ...
- Selenium实战(三)——滑动解锁+窗口截图
一.简单滑块的实例 当单机滑块时,改变的只是CSS样式,HTML代码如下: <div class="slide-to-unlock-progress" style=" ...
- 【Selenium04篇】python+selenium实现Web自动化:文件上传,Cookie操作,调用 JavaScript,窗口截图
一.前言 最近问我自动化的人确实有点多,个人突发奇想:想从0开始讲解python+selenium实现Web自动化测试,请关注博客持续更新! 这是python+selenium实现Web自动化第四篇博 ...
随机推荐
- XAML 名称范围 (x:) 语言特性
本节介绍为 Windows 运行时实现的 XAML 语言特性的参考信息. 本部分内容 主题 描述 x:Class 属性 配置 XAML 编译,在标记和代码隐藏之间连接分部类.代码分部类在一个独立的代码 ...
- (转)jQuery插件开发全解析
jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...
- pch文件出现no such file or directory错误
一般出现这种情况是由于项目直接拷贝到其他电脑上运行... clang: error: no such file or directory: '/demo2/控件代码/13/Recorder/Recor ...
- C# 前台线程与后台线程区别
using System; using System.Drawing; using System.Windows.Forms; using System.Threading; namespace Wi ...
- OSG调试信息显示
调试信息显示 OSG 可以将各式各样的调试信息输出到std:cout.这在开发OSG 程序时十分有用,你可以借此观察OSG 的执行的各种操作.环境变量OSG_NOTIFY_LEVEL用于控制OSG调试 ...
- DataTable 对象 转换为Json 字符串
/// <summary> /// DataTable 对象 转换为Json 字符串 /// </summary> /// <param name="dt&qu ...
- Clover周报模块 -- 开发总结
2014年7月8日 00:16:05 一.切图 这次开发,切图花了不少时间,样式是用scss写的,第一次用,不过用着用着就发现它的强大,层级.作用域.重用等都非常的方便,还有考拉神器,用着真是爽!不过 ...
- 给大家推荐几款OSX上非常好工具(针对程序员)
前两天,在App Store无意中发现几款免费工具,感觉非常好用,推荐给大家,希望大家喜欢. 一个是帮助文档管理软件,Dash.以前在Windows上开发,经常要自己搜集一些手册,文档,什么html手 ...
- Git学习笔记--Git常用命令
参考资料: http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 mkdir myfir ...
- delegate,notifucation,KVO三种模式实现通信的优缺点
在开发ios应用的时候,我们会经常遇到一个常见的问题:在不过分耦合的前提下,controllers间怎么进行通信.在IOS应用不断的出现三种模式来实现这种通信: 1.委托delega ...