Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于捕获完整屏幕截图的c#代码 使用系统; 使用System.Runtime.InteropServices; 使用系统图; 使用系统、绘图、成像; 名称空间截图演示 { /// ///提供捕获整个屏幕或特定窗口并将其保存到文件的功能。 /// 公共类截图 { 静态void Main() { ScreenCapture sc=新屏幕捕获(); //捕获整个屏幕,并将其保存到文件中 图像img=sc.CaptureScreen(); //在名为imageDisplay的图片控件中显示图像 //this.imageDisplay.Image=img; img.Save(“c:\\myBitmap.bmp”); //捕获此窗口并保存它 sc.CaptureWindowToFile(句柄,“C:\\temp2.gif”,ImageFormat.gif); //截屏运行=新截屏(); //run.CaptureScreen(); } /// ///创建包含整个桌面的屏幕快照的图像对象 /// /// 公共图像捕获屏幕() { 返回CaptureWindow(User32.GetDesktopWindow()); } /// ///创建包含特定窗口的屏幕快照的图像对象 /// ///窗口的句柄。(在windows窗体中,这是通过句柄属性获得的) /// 公共图像捕获窗口(IntPtr句柄) { //获取目标窗口的te hDC IntPtr hdcSrc=User32.GetWindowDC(句柄); //知道尺寸了吗 User32.RECT windowRect=新的User32.RECT(); User32.GetWindowRect(句柄,ref windowRect); int width=windowRect.right-windowRect.left; int height=windowRect.bottom-windowRect.top; //创建可以复制到的设备上下文 IntPtr hdcDest=GDI32.CreateCompatibleDC(hdcSrc); //创建一个我们可以复制到的位图, //使用GetDeviceCaps获取宽度/高度 IntPtr hBitmap=GDI32.CreateCompatibleBitmap(hdcSrc,宽度,高度); //选择位图对象 IntPtr hOld=GDI32。选择对象(hdcDest、hBitmap); //结束 GDI32.BitBlt(hdcDest,0,0,宽度,高度,hdcSrc,0,0,GDI32.srcopy); //恢复选择 GDI32.选择对象(hdcDest,保持); //清理 GDI32.DeleteDC(hdcDest); User32.ReleaseDC(句柄,hdcSrc); //为它获取一个.NET图像对象 图像img=图像。来自hBitmap(hBitmap); //释放位图对象 GDI32.DeleteObject(hBitmap); 返回img; } /// ///捕获特定窗口的屏幕快照,并将其保存到文件中 /// /// /// /// public void CaptureWindowToFile(IntPtr句柄、字符串文件名、ImageFormat格式) { 图像img=捕获窗口(手柄); 保存(文件名、格式); } /// ///捕获整个桌面的屏幕快照,并将其保存到文件中 /// /// /// public void CaptureScreenToFile(字符串文件名、图像格式) { 图像img=CaptureScreen(); 保存(文件名、格式); } /// ///包含Gdi32 API函数的帮助器类 /// 私有类GDI32 { public const int SRCCOPY=0x00CC0020;//BitBlt dwRop参数 [DllImport(“gdi32.dll”)] 公共静态外部bool BitBlt(IntPtr hObject、intnxtest、intnydest、, int nWidth、int nHeight、IntPtr hObjectSource、, int nXSrc、int nYSrc、int dwRop); [DllImport(“gdi32.dll”)] 公共静态外部IntPtr CreateCompatibleBitmap(IntPtr hDC,int nWidth, 内特(右); [DllImport(“gdi32.dll”)] 公共静态外部IntPtr CreateCompatibleDC(IntPtr hDC); [DllImport(“gdi32.dll”)] 公共静态外部布尔删除DC(IntPtr hDC); [DllImport(“gdi32.dll”)] 公共静态外部bool DeleteObject(IntPtr-hObject); [DllImport(“gdi32.dll”)] 公共静态外部IntPtr SelectObject(IntPtr hDC、IntPtr hObject); } /// ///包含user32api函数的Helper类 /// 私有类User32 { [StructLayout(LayoutKind.Sequential)] 公共结构矩形 { 公共int左; 公共int top; 公共权利; 公共int底部; } [DllImport(“user32.dll”)] 公共静态外部IntPtr GetDesktopWindow(); [DllImport(“user32.dll”)] 公共静态外部IntPtr GetWindowDC(IntPtr hWnd); [DllImport(“user32.dll”)] 公共静态外部IntPtr ReleaseDC(IntPtr hWnd、IntPtr hDC); [DllImport(“user32.dll”)] 公共静态外部IntPtr GetWindowRect(IntPtr hWnd,ref RECT RECT); } } }_C#_C# 4.0 - Fatal编程技术网

用于捕获完整屏幕截图的c#代码 使用系统; 使用System.Runtime.InteropServices; 使用系统图; 使用系统、绘图、成像; 名称空间截图演示 { /// ///提供捕获整个屏幕或特定窗口并将其保存到文件的功能。 /// 公共类截图 { 静态void Main() { ScreenCapture sc=新屏幕捕获(); //捕获整个屏幕,并将其保存到文件中 图像img=sc.CaptureScreen(); //在名为imageDisplay的图片控件中显示图像 //this.imageDisplay.Image=img; img.Save(“c:\\myBitmap.bmp”); //捕获此窗口并保存它 sc.CaptureWindowToFile(句柄,“C:\\temp2.gif”,ImageFormat.gif); //截屏运行=新截屏(); //run.CaptureScreen(); } /// ///创建包含整个桌面的屏幕快照的图像对象 /// /// 公共图像捕获屏幕() { 返回CaptureWindow(User32.GetDesktopWindow()); } /// ///创建包含特定窗口的屏幕快照的图像对象 /// ///窗口的句柄。(在windows窗体中,这是通过句柄属性获得的) /// 公共图像捕获窗口(IntPtr句柄) { //获取目标窗口的te hDC IntPtr hdcSrc=User32.GetWindowDC(句柄); //知道尺寸了吗 User32.RECT windowRect=新的User32.RECT(); User32.GetWindowRect(句柄,ref windowRect); int width=windowRect.right-windowRect.left; int height=windowRect.bottom-windowRect.top; //创建可以复制到的设备上下文 IntPtr hdcDest=GDI32.CreateCompatibleDC(hdcSrc); //创建一个我们可以复制到的位图, //使用GetDeviceCaps获取宽度/高度 IntPtr hBitmap=GDI32.CreateCompatibleBitmap(hdcSrc,宽度,高度); //选择位图对象 IntPtr hOld=GDI32。选择对象(hdcDest、hBitmap); //结束 GDI32.BitBlt(hdcDest,0,0,宽度,高度,hdcSrc,0,0,GDI32.srcopy); //恢复选择 GDI32.选择对象(hdcDest,保持); //清理 GDI32.DeleteDC(hdcDest); User32.ReleaseDC(句柄,hdcSrc); //为它获取一个.NET图像对象 图像img=图像。来自hBitmap(hBitmap); //释放位图对象 GDI32.DeleteObject(hBitmap); 返回img; } /// ///捕获特定窗口的屏幕快照,并将其保存到文件中 /// /// /// /// public void CaptureWindowToFile(IntPtr句柄、字符串文件名、ImageFormat格式) { 图像img=捕获窗口(手柄); 保存(文件名、格式); } /// ///捕获整个桌面的屏幕快照,并将其保存到文件中 /// /// /// public void CaptureScreenToFile(字符串文件名、图像格式) { 图像img=CaptureScreen(); 保存(文件名、格式); } /// ///包含Gdi32 API函数的帮助器类 /// 私有类GDI32 { public const int SRCCOPY=0x00CC0020;//BitBlt dwRop参数 [DllImport(“gdi32.dll”)] 公共静态外部bool BitBlt(IntPtr hObject、intnxtest、intnydest、, int nWidth、int nHeight、IntPtr hObjectSource、, int nXSrc、int nYSrc、int dwRop); [DllImport(“gdi32.dll”)] 公共静态外部IntPtr CreateCompatibleBitmap(IntPtr hDC,int nWidth, 内特(右); [DllImport(“gdi32.dll”)] 公共静态外部IntPtr CreateCompatibleDC(IntPtr hDC); [DllImport(“gdi32.dll”)] 公共静态外部布尔删除DC(IntPtr hDC); [DllImport(“gdi32.dll”)] 公共静态外部bool DeleteObject(IntPtr-hObject); [DllImport(“gdi32.dll”)] 公共静态外部IntPtr SelectObject(IntPtr hDC、IntPtr hObject); } /// ///包含user32api函数的Helper类 /// 私有类User32 { [StructLayout(LayoutKind.Sequential)] 公共结构矩形 { 公共int左; 公共int top; 公共权利; 公共int底部; } [DllImport(“user32.dll”)] 公共静态外部IntPtr GetDesktopWindow(); [DllImport(“user32.dll”)] 公共静态外部IntPtr GetWindowDC(IntPtr hWnd); [DllImport(“user32.dll”)] 公共静态外部IntPtr ReleaseDC(IntPtr hWnd、IntPtr hDC); [DllImport(“user32.dll”)] 公共静态外部IntPtr GetWindowRect(IntPtr hWnd,ref RECT RECT); } } }

用于捕获完整屏幕截图的c#代码 使用系统; 使用System.Runtime.InteropServices; 使用系统图; 使用系统、绘图、成像; 名称空间截图演示 { /// ///提供捕获整个屏幕或特定窗口并将其保存到文件的功能。 /// 公共类截图 { 静态void Main() { ScreenCapture sc=新屏幕捕获(); //捕获整个屏幕,并将其保存到文件中 图像img=sc.CaptureScreen(); //在名为imageDisplay的图片控件中显示图像 //this.imageDisplay.Image=img; img.Save(“c:\\myBitmap.bmp”); //捕获此窗口并保存它 sc.CaptureWindowToFile(句柄,“C:\\temp2.gif”,ImageFormat.gif); //截屏运行=新截屏(); //run.CaptureScreen(); } /// ///创建包含整个桌面的屏幕快照的图像对象 /// /// 公共图像捕获屏幕() { 返回CaptureWindow(User32.GetDesktopWindow()); } /// ///创建包含特定窗口的屏幕快照的图像对象 /// ///窗口的句柄。(在windows窗体中,这是通过句柄属性获得的) /// 公共图像捕获窗口(IntPtr句柄) { //获取目标窗口的te hDC IntPtr hdcSrc=User32.GetWindowDC(句柄); //知道尺寸了吗 User32.RECT windowRect=新的User32.RECT(); User32.GetWindowRect(句柄,ref windowRect); int width=windowRect.right-windowRect.left; int height=windowRect.bottom-windowRect.top; //创建可以复制到的设备上下文 IntPtr hdcDest=GDI32.CreateCompatibleDC(hdcSrc); //创建一个我们可以复制到的位图, //使用GetDeviceCaps获取宽度/高度 IntPtr hBitmap=GDI32.CreateCompatibleBitmap(hdcSrc,宽度,高度); //选择位图对象 IntPtr hOld=GDI32。选择对象(hdcDest、hBitmap); //结束 GDI32.BitBlt(hdcDest,0,0,宽度,高度,hdcSrc,0,0,GDI32.srcopy); //恢复选择 GDI32.选择对象(hdcDest,保持); //清理 GDI32.DeleteDC(hdcDest); User32.ReleaseDC(句柄,hdcSrc); //为它获取一个.NET图像对象 图像img=图像。来自hBitmap(hBitmap); //释放位图对象 GDI32.DeleteObject(hBitmap); 返回img; } /// ///捕获特定窗口的屏幕快照,并将其保存到文件中 /// /// /// /// public void CaptureWindowToFile(IntPtr句柄、字符串文件名、ImageFormat格式) { 图像img=捕获窗口(手柄); 保存(文件名、格式); } /// ///捕获整个桌面的屏幕快照,并将其保存到文件中 /// /// /// public void CaptureScreenToFile(字符串文件名、图像格式) { 图像img=CaptureScreen(); 保存(文件名、格式); } /// ///包含Gdi32 API函数的帮助器类 /// 私有类GDI32 { public const int SRCCOPY=0x00CC0020;//BitBlt dwRop参数 [DllImport(“gdi32.dll”)] 公共静态外部bool BitBlt(IntPtr hObject、intnxtest、intnydest、, int nWidth、int nHeight、IntPtr hObjectSource、, int nXSrc、int nYSrc、int dwRop); [DllImport(“gdi32.dll”)] 公共静态外部IntPtr CreateCompatibleBitmap(IntPtr hDC,int nWidth, 内特(右); [DllImport(“gdi32.dll”)] 公共静态外部IntPtr CreateCompatibleDC(IntPtr hDC); [DllImport(“gdi32.dll”)] 公共静态外部布尔删除DC(IntPtr hDC); [DllImport(“gdi32.dll”)] 公共静态外部bool DeleteObject(IntPtr-hObject); [DllImport(“gdi32.dll”)] 公共静态外部IntPtr SelectObject(IntPtr hDC、IntPtr hObject); } /// ///包含user32api函数的Helper类 /// 私有类User32 { [StructLayout(LayoutKind.Sequential)] 公共结构矩形 { 公共int左; 公共int top; 公共权利; 公共int底部; } [DllImport(“user32.dll”)] 公共静态外部IntPtr GetDesktopWindow(); [DllImport(“user32.dll”)] 公共静态外部IntPtr GetWindowDC(IntPtr hWnd); [DllImport(“user32.dll”)] 公共静态外部IntPtr ReleaseDC(IntPtr hWnd、IntPtr hDC); [DllImport(“user32.dll”)] 公共静态外部IntPtr GetWindowRect(IntPtr hWnd,ref RECT RECT); } } },c#,c#-4.0,C#,C# 4.0,它给出错误“名称句柄在当前上下文中不存在”,请帮助我 谢谢, Nishant将静态void Main()的主体更换为 using System; using System.Runtime.InteropServices; using System.Drawing; using System.Drawing.Imaging; namespace ScreenShotDemo { /// <summary> /// Provides functions to capt

它给出错误“名称句柄在当前上下文中不存在”,请帮助我

谢谢,
Nishant

静态void Main()
的主体更换为

using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;

namespace ScreenShotDemo
{

    /// <summary>
    /// Provides functions to capture the entire screen, or a particular window, and save it to a file.
    /// </summary>
    public class ScreenCapture
    {

        static void Main()
      {

         ScreenCapture sc = new ScreenCapture();
       // capture entire screen, and save it to a file
       Image img = sc.CaptureScreen();
       // display image in a Picture control named imageDisplay
       //this.imageDisplay.Image = img;
       img.Save("c:\\myBitmap.bmp");
       // capture this window, and save it
       sc.CaptureWindowToFile(Handle,"C:\\temp2.gif",ImageFormat.Gif);
       // ScreenCapture run = new ScreenCapture();  
       // run.CaptureScreen(); 
      }

        /// <summary>
        /// Creates an Image object containing a screen shot of the entire desktop
        /// </summary>
        /// <returns></returns>
        public Image CaptureScreen() 
        {
            return CaptureWindow( User32.GetDesktopWindow() );
        }

        /// <summary>
        /// Creates an Image object containing a screen shot of a specific window
        /// </summary>
        /// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</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>
        /// Captures a screen shot of a specific window, and saves it to a file
        /// </summary>
        /// <param name="handle"></param>
        /// <param name="filename"></param>
        /// <param name="format"></param>
        public void CaptureWindowToFile(IntPtr handle, string filename, ImageFormat format) 
        {
            Image img = CaptureWindow(handle);
            img.Save(filename,format);
        }

        /// <summary>
        /// Captures a screen shot of the entire desktop, and saves it to a file
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="format"></param>
        public void CaptureScreenToFile(string filename, ImageFormat format) 
        {
            Image img = CaptureScreen();
            img.Save(filename,format);
        }

        /// <summary>
        /// Helper class containing Gdi32 API functions
        /// </summary>
        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);
        }

        /// <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);

        }


    }
}

sc.CaptureWindowToFile(句柄,“C:\\temp2.gif”,ImageFormat.gif);句柄变量没有声明在此语句可以访问它的任何位置。2注释,它是什么,错误发生在哪里?也许你指的是屏幕截图,不是短截图。这里有人在使用WPF应用程序时遇到任何问题吗?
CaptureScreenToFile("C:\\temp2.gif",ImageFormat.Gif)