zoukankan      html  css  js  c++  java
  • wpf窗口区域截图

    1. 直接将grid保存为图片,此方法无法保存嵌入的程序
    RenderTargetBitmap rtbmp = new RenderTargetBitmap((int)MainGrid.ActualWidth, (int)MainGrid.ActualHeight, 96, 96, PixelFormats.Default);
    rtbmp.Render(MainGrid);
    PngBitmapEncoder encode = new PngBitmapEncoder();
    encode.Frames.Add(BitmapFrame.Create(rtbmp));
    MemoryStream ms = new MemoryStream();
    encode.Save(ms);
    System.Drawing.Image MyImage = System.Drawing.Image.FromStream(ms);
    MyImage.Save(@"E:	mpa.PNG");
    
    1. 使用系统printscreen实现截图
      //推荐使用CutWindowScreen()
    using System;
    using System.Drawing;
    using System.Runtime.InteropServices;
    using System.Windows;
    using System.Windows.Forms;
    
    namespace Aixueshi.Util.ScreenHelper
    {
        public class WindowCapture
        {
            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(IntPtr hDC)
            {
    
    
                //保存截屏的尺寸
                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;
            }
    
    
            [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. 
          );
            //传入窗口句柄,获取该窗口的图像信息
    
            public static Image GetWindowImage(IntPtr windownHandle, int width, int height)
            {
                Bitmap image = new Bitmap(width, height);
                Graphics gp = Graphics.FromImage(image);
                IntPtr dc = gp.GetHdc();
                PrintWindow(windownHandle, dc, 0);
                gp.ReleaseHdc();
                gp.Dispose();
                return image;
            }
    
    
    
    
            [DllImport("user32.dll")]
    
            static extern void keybd_event
            (
                byte bVk,// 虚拟键值
    
                byte bScan,// 硬件扫描码    
    
                uint dwFlags,// 动作标识    
    
                IntPtr dwExtraInfo// 与键盘动作关联的辅加信息    
                  );
    
            const int VK_SNAPSHOT = 0x2C;
    
            //left alt
            const int VK_MENU = 0xA4 & 0x80;
    
            public static void key_down(Keys key)
            {
                keybd_event((byte)key, 0, 0x0, IntPtr.Zero);//down  
            }
    
            const int KEYEVENTF_KEYUP = 0x2;//若指定该值,该键将被释放;若未指定该值,该键将被按下   
            public static void key_up(Keys key)
            {
                keybd_event((byte)key, 0, KEYEVENTF_KEYUP, IntPtr.Zero);//up  
            }
    
            public static void PrintScreen()
            {
                key_down(Keys.PrintScreen);//down  
                System.Windows.Forms.Application.DoEvents();//强制窗口响应按钮事件
                key_up(Keys.PrintScreen);//up  
                System.Windows.Forms.Application.DoEvents();
            }
    
            public static void AltPrintScreen()
            {
                key_down(Keys.Menu);//down  
                key_down(Keys.PrintScreen);//down  
                System.Windows.Forms.Application.DoEvents();//强制窗口响应按钮事件
                key_up(Keys.PrintScreen);//up  
                key_up(Keys.Menu);//up  
                System.Windows.Forms.Application.DoEvents();
            }
    
            [DllImport("user32.dll")] private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
            //SendMessage参数
            private const int WM_KEYDOWN = 0X100;
            private const int WM_KEYUP = 0X101;
            private const int WM_SYSCHAR = 0X106;
            private const int WM_SYSKEYUP = 0X105;
            private const int WM_SYSKEYDOWN = 0X104;
            private const int WM_CHAR = 0X102;
    
            public static void AltPrintScreenForWindow(IntPtr windowPtr)
            {
                //Keys.Enter
                //Keys.Alt
                //SendMessage(windowPtr, WM_SYSKEYDOWN, 0X0D, 0); //输入ENTER(0x0d)
                SendMessage(windowPtr, WM_SYSKEYDOWN, (byte)Keys.PrintScreen, 0); //输入ENTER(0x0d)
                SendMessage(windowPtr, WM_SYSKEYDOWN, (byte)Keys.LMenu, 0); //输入ENTER(0x0d)
            }
    
            public static System.Drawing.Image cutPicture(System.Drawing.Image image, Int32Rect window)
            {
                //PrintScreen();
    
                if (window.X + window.Width > image.Width)
    
                    window.Width = image.Width - window.X;
    
                if (window.Y + window.Height > image.Height)
    
                    window.Height = image.Height - window.Y;
    
                if (image != null)
    
                {
                    //新建一个bmp图片
    
                    System.Drawing.Image bitmap = new System.Drawing.Bitmap(window.Width, window.Height);
    
                    //新建一个画板
    
                    System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(bitmap);
    
                    //设置高质量查值法
    
                    graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
    
                    //设置高质量,低速度呈现平滑程度
    
                    graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    
                    //清空画布并以透明背景色填充
    
                    graphic.Clear(System.Drawing.Color.Transparent);
    
                    //在指定位置并且按指定大小绘制原图片的指定部分
    
                    graphic.DrawImage(image, new System.Drawing.Rectangle(0, 0, window.Width, window.Height), new System.Drawing.Rectangle(window.X, window.Y, window.Width, window.Height), System.Drawing.GraphicsUnit.Pixel);
    
                    return bitmap;
                }
    
                else
                {
                    return null;
                }
            }
    
            /// <summary>
            /// 截取屏幕
            /// </summary>
            /// <param name="window"></param>
            /// <returns></returns>
            public static System.Drawing.Image CutScreen(Int32Rect window)
            {
                PrintScreen();
                if (!System.Windows.Forms.Clipboard.ContainsImage())
                {
                    return null;
                }
                //return System.Windows.Forms.Clipboard.GetImage();
                return cutPicture(System.Windows.Forms.Clipboard.GetImage(), window);
            }
    
            /// <summary>
            /// 截取窗口句柄(使用此方法需要保证窗口激活)
            /// </summary>
            /// <param name="window"></param>
            /// <returns></returns>
            public static System.Drawing.Image CutFocusScreen(Int32Rect window)
            {
                AltPrintScreen();
                if (!System.Windows.Forms.Clipboard.ContainsImage())
                {
                    return null;
                }
                return System.Windows.Forms.Clipboard.GetImage();
                //return cutPicture(System.Windows.Forms.Clipboard.GetImage(), window);
            }
    
            /// <summary>
            /// 截取窗口区域(基于窗口句柄)
            /// </summary>
            /// <param name="windowPtr"></param>
            /// <returns></returns>
            public static System.Drawing.Image CutWindowScreen(IntPtr windowPtr)
            {
                AltPrintScreenForWindow(windowPtr);
                if (!System.Windows.Forms.Clipboard.ContainsImage())
                {
                    return null;
                }
                return System.Windows.Forms.Clipboard.GetImage();
                //return cutPicture(System.Windows.Forms.Clipboard.GetImage(), window);
            }
        }
    }
    
    留待后查,同时方便他人
    联系我:renhanlinbsl@163.com
  • 相关阅读:
    数组 例题解析
    第六章 数组
    循环控制 例题解析
    第五章 循环控制
    第四章 选择结构程序设计
    第三章 数据的输入与输出
    第一章 程序设计及C语言概述
    C 字符串类型例题讲解与实现字符串库函数
    0XX_javascript核心
    012品优购03
  • 原文地址:https://www.cnblogs.com/ives/p/printscreen.html
Copyright © 2011-2022 走看看