WPF使用Win32进行GDI截屏笔记
2017-06-19 11:45
330 查看
1、新建项目,并为项目添加引用:
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 9.5px Helvetica }
3、添加一个类,命名空间自定:
4、获取截屏结果
完工!
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 9.5px Helvetica }
System.Drawing
3、添加一个类,命名空间自定:
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Text; namespace HJess { public class ScreenDevice { public const int SM_CXSCREEN = 0; public const int SM_CYSCREEN = 1; public const int SRCCOPY = 13369376; [DllImport("gdi32.dll", EntryPoint = "DeleteDC")] public static extern IntPtr DeleteDC(IntPtr hDc); [DllImport("gdi32.dll", EntryPoint = "DeleteObject")] public static extern IntPtr DeleteObject(IntPtr hDc); [DllImport("gdi32.dll", EntryPoint = "BitBlt")] public static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, int RasterOp); [DllImport("gdi32.dll", EntryPoint = "CreateCompatibleBitmap")] public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight); [DllImport("gdi32.dll", EntryPoint = "CreateCompatibleDC")] public static extern IntPtr CreateCompatibleDC(IntPtr hdc); [DllImport("gdi32.dll", EntryPoint = "SelectObject")] public static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp); [DllImport("user32.dll", EntryPoint = "GetDesktopWindow")] public static extern IntPtr GetDesktopWindow(); [DllImport("user32.dll", EntryPoint = "GetDC")] public static extern IntPtr GetDC(IntPtr ptr); [DllImport("user32.dll", EntryPoint = "GetSystemMetrics")] public static extern int GetSystemMetrics(int abc); [DllImport("user32.dll", EntryPoint = "GetWindowDC")] public static extern IntPtr GetWindowDC(Int32 ptr); [DllImport("user32.dll", EntryPoint = "ReleaseDC")] public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc); /** * 尺寸 * */ public struct SIZE { public int cx; public int cy; } /** * 获取屏幕解图 */ public static Bitmap GetDesktopImage() { //保存截屏的尺寸 SIZE size; //指向bitmap的句柄 IntPtr hBitmap; //通过GetDC函数获得指向桌面设备上下文的句柄 IntPtr hDC = GetDC(GetDesktopWindow()); //在内存中创建一个兼容的设备上下文 IntPtr hMemDC = CreateCompatibleDC(hDC); //传递一个常数到GetSystemMetrics,并返回屏幕的x坐标 size.cx = GetSystemMetrics(0); //传递一个常数到GetSystemMetrics,并返回屏幕的y坐标 size.cy = GetSystemMetrics(1); //创建与指定的设备环境相关的设备兼容的位图。 hBitmap = CreateCompatibleBitmap(hDC, size.cx, size.cy); //As hBitmap is IntPtr we can not check it against null. For this purspose IntPtr.Zero is used. if (hBitmap != IntPtr.Zero) { //Here we select the compatible bitmap in memeory device context and keeps the refrence to Old bitmap. IntPtr hOld = (IntPtr)SelectObject(hMemDC, hBitmap); //We copy the Bitmap to the memory device context. BitBlt(hMemDC, 0, 0, size.cx, size.cy, hDC, 0, 0, SRCCOPY); //We select the old bitmap back to the memory device context. SelectObject(hMemDC, hOld); //We delete the memory device context. DeleteDC(hMemDC); //We release the screen device context. ReleaseDC(GetDesktopWindow(), hDC); //Image is created by Image bitmap handle and stored in local variable. Bitmap bmp = System.Drawing.Image.FromHbitmap(hBitmap); //Release the memory for compatible bitmap. DeleteObject(hBitmap); //This statement runs the garbage collector manually. GC.Collect(); //Return the bitmap return bmp; } //If hBitmap is null retunrn null. return null; } } }
4、获取截屏结果
Bitmap bitmap = ScreenDevice.GetDesktopImage(); IntPtr ip = bitmap.GetHbitmap();//从GDI+ Bitmap创建GDI位图对象 //Imaging.CreateBitmapSourceFromHBitmap方法,基于所提供的非托管位图和调色板信息的指针,返回一个托管的BitmapSource BitmapSource bitmapSource = System.Windows.Interop.Imaging .CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); image1.Source = bitmapSource;
完工!
相关文章推荐
- C#(WPF)中使用WinAPI函数进行截屏
- C#(WPF)中使用WinAPI函数进行截屏
- 使用Grid来对WPF页面进行布局排版
- 使用Grid来对WPF页面进行布局排版
- 使用Lccwin32进行MySQL开发。
- WPF and Silverlight 学习笔记(二十八):基本图形的使用(3)图形的操作
- 『框架设计(第2版)CLR Via C#』学习笔记——使用is和as操作符来进行强制类型转换
- 使用Grid来对WPF页面进行布局排版
- Asp.net学习笔记----使用GridView+ObjectDataSource进行自定义分页排序
- 完整的使用GDI分割图片的win32程序
- DELPHI学习笔记--使用通配符进行模糊查询
- 使用WxPython进行Win32下Python编程
- WPF中,如何使用图像API进行绘制而不是XAML?
- 使用Grid来对WPF页面进行布局排版
- Win32 GDI基础(笔记)
- C++/GDI+ 学习笔记(一)——安装、配置&使用
- Win32学习笔记——菜单和加速键资源的使用
- WPF and Silverlight 学习笔记(二十五):使用CollectionView实现对绑定数据的排序、筛选、分组
- WPF and Silverlight 学习笔记(二十七):基本图形的使用(2)Path和位图操作
- Win32函数使用笔记