博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c# 图片窗口区域截图代码
阅读量:6076 次
发布时间:2019-06-20

本文共 7411 字,大约阅读时间需要 24 分钟。

using System;using System.Collections.Generic;using System.Drawing;using System.Drawing.Imaging;using System.Runtime.InteropServices;using System.Text;using System.Windows.Forms;namespace GetFormDemo.Comm{    public struct RECT    {        public int x1;        public int y1;        public int x2;        public int y2;    }    class ScreenCapture    {        [DllImport("user32.dll")]        private static extern IntPtr GetDC(IntPtr hwnd);        [DllImport("user32.dll")]        private static extern IntPtr ReleaseDC(IntPtr hc, IntPtr hDest);        [DllImport("user32.dll")]        private static extern IntPtr GetWindowDC(IntPtr hwnd);        [DllImport("user32.dll")]        private static extern IntPtr GetForegroundWindow();        [DllImport("user32.dll")]        private static extern IntPtr GetWindowRect(IntPtr hWnd, out RECT rect);        [DllImport("user32.dll")]        private static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, UInt32 nFlags);        [DllImport("user32.dll")]        private static extern IntPtr GetDesktopWindow();        [DllImport("gdi32.dll")]        private 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")]        private 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坐标             CopyPixelOperation dwRop   // raster operation code光栅的操作值         );        //static extern int BitBlt(IntPtr hdcDest, int xDest, int yDest, int        //wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, CopyPixelOperation rop);        [DllImport("gdi32.dll")]        private static extern IntPtr CreateCompatibleDC(         IntPtr hdc // handle to DC         );        [DllImport("gdi32.dll")]        private 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")]        private static extern IntPtr SelectObject(             IntPtr hdc,           // handle to DC             IntPtr hgdiobj    // handle to object         );        [DllImport("gdi32.dll")]        private static extern int DeleteDC(            IntPtr hdc           // handle to DC         );        ///         /// 抓取屏幕(层叠的窗口)        ///         /// 左上角的横坐标        /// 左上角的纵坐标        /// 抓取宽度        /// 抓取高度        /// 
public 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; } /// /// 抓取整个屏幕 /// ///
public Bitmap CaptureScreen() { Size screenSize = Screen.PrimaryScreen.Bounds.Size; return CaptureScreen(0, 0, screenSize.Width, screenSize.Height); } /// /// 全屏截图 /// ///
public Image CaptureScreenI() { return CaptureWindow(GetDesktopWindow()); } /// /// 全屏指定区域截图 /// ///
public Image CaptureScreenI(RECT rect) { return CaptureWindow(GetDesktopWindow(), rect); } /// /// 指定窗口截图 /// /// 窗口句柄. (在windows应用程序中, 从Handle属性获得) ///
public Bitmap CaptureWindow(IntPtr hWnd) { IntPtr hscrdc = GetWindowDC(hWnd); RECT rect = new RECT(); return CaptureWindow(hWnd, rect); } /// /// 指定窗口区域截图 /// /// 窗口句柄. (在windows应用程序中, 从Handle属性获得) /// 窗口中的一个区域 ///
public Bitmap CaptureWindow(IntPtr hWnd, RECT rect) { // 获取设备上下文环境句柄 IntPtr hscrdc = GetWindowDC(hWnd); // 创建一个与指定设备兼容的内存设备上下文环境(DC) IntPtr hmemdc = CreateCompatibleDC(hscrdc); IntPtr myMemdc = CreateCompatibleDC(hscrdc); // 返回指定窗体的矩形尺寸 RECT rect1; GetWindowRect(hWnd, out rect1); // 返回指定设备环境句柄对应的位图区域句柄 IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, rect1.x2 - rect1.x1, rect1.y2 - rect1.y1); IntPtr myBitmap = CreateCompatibleBitmap(hscrdc, rect.x2 - rect.x1, rect.y2 - rect.y1); //把位图选进内存DC // IntPtr OldBitmap = (IntPtr)SelectObject(hmemdc, hbitmap); SelectObject(hmemdc, hbitmap); SelectObject(myMemdc, myBitmap); ///// // // 下面开始所谓的作画过程,此过程可以用的方法很多,看你怎么调用 API 了 // ///// // 直接打印窗体到画布 PrintWindow(hWnd, hmemdc, 0); // IntPtr hw = GetDesktopWindow(); // IntPtr hmemdcClone = GetWindowDC(myBitmap); BitBlt(myMemdc, 0, 0, rect.x2 - rect.x1, rect.y2 - rect.y1, hmemdc, rect.x1, rect.y1, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt); //SelectObject(myMemdc, myBitmap); Bitmap bmp = Bitmap.FromHbitmap(myBitmap); DeleteDC(hscrdc); DeleteDC(hmemdc); DeleteDC(myMemdc); return bmp; } /// /// 指定窗口截图 保存为图片文件 /// /// /// /// public void CaptureWindowToFile(IntPtr handle, string filename, ImageFormat format) { Image img = CaptureWindow(handle); img.Save(filename, format); } /// /// 全屏截图 保存为文件 /// /// /// public void CaptureScreenToFile(string filename, ImageFormat format) { Image img = CaptureScreen(); img.Save(filename, format); } /// /// 设置 RECT 的左下右上 /// /// 给出要设定的 RECT /// 左边 /// 下边 /// 右边 /// 上边 public void SetRECT(ref RECT rect, int x1, int y1, int x2, int y2) { rect.x1 = x1; rect.y1 = y1; rect.x2 = x2; rect.y2 = y2; } /// /// 合并图片 /// /// 图片1 /// 图片2 public Bitmap HBpic(Bitmap bmp1, Bitmap bmp2) { Bitmap newBmp = new Bitmap(bmp1.Width, bmp1.Height + bmp2.Height); var g = Graphics.FromImage(newBmp); g.DrawImage(bmp1, 0, 0); g.DrawImage(bmp2, 0, bmp1.Height); return newBmp; } }}

 

本人转载至http://www.cnblogs.com/herbertchina/p/4492922.html  详细请看这

转载于:https://www.cnblogs.com/daming1233/p/6389021.html

你可能感兴趣的文章
CodeForces 580B Kefa and Company
查看>>
开发规范浅谈
查看>>
Spark Streaming揭秘 Day29 深入理解Spark2.x中的Structured Streaming
查看>>
鼠标增强软件StrokeIt使用方法
查看>>
本地连接linux虚拟机的方法
查看>>
某公司面试java试题之【二】,看看吧,说不定就是你将要做的题
查看>>
BABOK - 企业分析(Enterprise Analysis)概要
查看>>
Linux 配置vnc,开启linux远程桌面
查看>>
NLog文章系列——如何优化日志性能
查看>>
Hadoop安装测试简单记录
查看>>
CentOS6.4关闭触控板
查看>>
ThreadPoolExecutor线程池运行机制分析-线程复用原理
查看>>
React Native 极光推送填坑(ios)
查看>>
Terratest:一个用于自动化基础设施测试的开源Go库
查看>>
修改Windows远程终端默认端口,让服务器更安全
查看>>
扩展器必须,SAS 2.0未必(SAS挺进中端存储系统之三)
查看>>
Eclipse遇到Initializing Java Tooling解决办法
查看>>
while((ch = getchar()) != '\n')
查看>>
好程序员web前端分享JS检查浏览器类型和版本
查看>>
Oracle DG 逻辑Standby数据同步性能优化
查看>>