




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C#非顶端窗口截图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);/画圆trybmp.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 NULLIntPtr 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 DCint nWidth,/ width of bitmap, in pixelsint nHeight/ height of bitmap, in pixels);DllImport(gdi32.dll)public static extern IntPtr SelectObject(IntPtr hdc,/ handle to DCIntPtr 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的使用/ / 提供全屏和指定窗口的截图 以及保存为文件的类/ public class ScreenCapture/ / 全屏截图/ / public Image CaptureScreen()return CaptureWindow(User32.GetDesktopWindow();/ / 指定窗口截图/ / 窗口句柄. (在windows应用程序中, 从Handle属性获得)/ 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);User32.ReleaseDC(handle, hdcSrc);Image img = Image.FromHbitmap(hBitmap);GDI32.DeleteObject(hBitmap);return img;/ / 指定窗口截图 保存为图片文件/ / / / 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);/ / 辅助类 定义 Gdi32 API 函数/ private class GDI32public const int SRCCOPY = 0x00CC0020;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);/ / 辅助类 定义User32 API函数/ private class User32StructLayout(LayoutKind.Sequential)public struct RECTpublic 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);上面的类 使用了BitBlt这个API可以截取GDI或者非GDI图形 只不过,非前端窗体图形不能截获.下面说一下不使用API 最简单的全屏截图方案public static Bitmap CopyPrimaryScreen()Screen s = Screen.PrimaryScreen;Rectangle r = s.Bounds;int w = r.Width;int h = r.Height;Bitmap bmp = new Bitmap(w, h);Graphics g = Graphics.FromImage(bmp);g.CopyFromScreen(new Point(0, 0),new Point(0, 0),new Size(w, h);return bmp;最后,试试下面这个办法:只要窗体的visable为true,即使它在屏幕的外面也可以抓到图。如果为false,就是一张黑图了,赫赫。public 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;API声明DllImport(gdi32.dll)public static extern IntPtr CreateDC(string lpszDriver,/ driver name驱动名string lpszDevice,/ device name设备名string lpszOutput,/ not used; should be NULLIntPtr 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
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 医院人力资源工作总结15篇
- 名著读后感范文
- 2025年春季中国电子校园招聘考前自测高频考点模拟试题附答案详解(模拟题)
- 中国广电松原市2025秋招技术岗专业追问清单及参考回答
- 2025年西安明珠电力安装工程有限公司招聘(2人)模拟试卷参考答案详解
- 非婚生子女抚养协议样书5篇
- 2025年镇江丹阳市卫生健康委员会所属丹阳市妇幼保健院(第二人民医院)校园公开招聘工作人员14人考前自测高频考点模拟试题及答案详解(易错题)
- 2025年学前教育师资队伍能力提升与教师专业发展路径研究报告
- 2025年教育行业数字化教材开发与网络信息安全研究报告
- 资料制作合同6篇
- 2024年新人教版八年级上册物理全册教案
- 伤口造口专科护士进修汇报
- MOOC 实验室安全学-武汉理工大学 中国大学慕课答案
- 彩钢房建造合同
- 2型糖尿病低血糖护理查房课件
- 医院物业服务投标方案
- 高压燃气管道施工方案
- 国家免疫规划疫苗儿童免疫程序说明-培训课件
- GB/T 4802.3-2008纺织品织物起毛起球性能的测定第3部分:起球箱法
- GB/T 13298-1991金属显微组织检验方法
- 劳动人事争议仲裁案例分析与问题探讨课件
评论
0/150
提交评论