C# 仅捕获表单的一部分的屏幕截图?

C# 仅捕获表单的一部分的屏幕截图?,c#,winforms,screenshot,C#,Winforms,Screenshot,我目前有一个winform,在运行时,我需要截取它的一部分并将其保存为图像。特别是,我想要一个名为“panel1”的面板的屏幕截图。我已经能够找到关于如何获取整个网络表单或整个屏幕截图的信息。。。但是我很难找到关于只获取屏幕一部分的信息 有没有人能告诉我正确的方向?即使是MSDN网站上我应该查找的文档的名称也会有很大的帮助!谢谢 从面板派生的控件类有一个方法DrawToBitmap()。你可以用这个 查看文档以了解更多信息。您将向其传递两组X/Y坐标-屏幕上的坐标和位图中的坐标。!非常感谢您的快

我目前有一个winform,在运行时,我需要截取它的一部分并将其保存为图像。特别是,我想要一个名为“panel1”的面板的屏幕截图。我已经能够找到关于如何获取整个网络表单或整个屏幕截图的信息。。。但是我很难找到关于只获取屏幕一部分的信息


有没有人能告诉我正确的方向?即使是MSDN网站上我应该查找的文档的名称也会有很大的帮助!谢谢

面板
派生的
控件
类有一个方法
DrawToBitmap()
。你可以用这个


查看文档以了解更多信息。您将向其传递两组X/Y坐标-屏幕上的坐标和位图中的坐标。

!非常感谢您的快速回复=这应该可以完美地解决我的问题。太棒了!这对我刚刚遇到的另一个问题也是一个巨大的帮助!哈哈,非常感谢!
     #region - Capture & Graphic API  -

        #region 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 bool StretchBlt(
              IntPtr hdcDest,      // handle to destination DC
              int nXOriginDest, // x-coord of destination upper-left corner
              int nYOriginDest, // y-coord of destination upper-left corner
              int nWidthDest,   // width of destination rectangle
              int nHeightDest,  // height of destination rectangle
              IntPtr hdcSrc,       // handle to source DC
              int nXOriginSrc,  // x-coord of source upper-left corner
              int nYOriginSrc,  // y-coord of source upper-left corner
              int nWidthSrc,    // width of source rectangle
              int nHeightSrc,   // height of source rectangle
              int dwRop       // raster operation code
            );


        [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);
        #endregion

        #region - 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);

        #endregion

    #endregion


   private void run_test()
   {

        Rectangle rc = new Rectangle();
        Image img = ScreenToImage(ref rc, false);

        img.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);
        img.Dispose();

   }


    private Image ScreenToImage(ref Rectangle rcDest, bool IsPapaer)
    {
        IntPtr handle = this.Handle;

        //this.Handle
        // get te hDC of the target window
        IntPtr hdcSrc = GetWindowDC(handle);
        // get the size
        RECT windowRect = new RECT();
        GetWindowRect(handle, ref windowRect);
        int nWidth = windowRect.right - windowRect.left;
        int nHeight = windowRect.bottom - windowRect.top;

        if (IsPapaer)
        {
            float fRate = (float)rcDest.Width/nWidth;
            //float fHeight = nHeight * fRate;
            //rcDest.Height = (int)(nHeight * fRate);
            //rcDest.Width = (int)(rcDest.Width);// * fRate);
            rcDest.X = 0;
            rcDest.Y = 0;
            rcDest.Height = (int)(nHeight * fRate);
            //rcDest.Width = (int)(nWidth * fRate);
        }
        else
        {
            rcDest.X = 0;
            rcDest.Y = 0;
            rcDest.Height = nHeight;
            rcDest.Width = nWidth;
        }

        // create a device context we can copy to
        IntPtr hdcDest = CreateCompatibleDC(hdcSrc);
        // create a bitmap we can copy it to,
        // using GetDeviceCaps to get the width/height
        IntPtr hBitmap = CreateCompatibleBitmap(hdcSrc, rcDest.Width, rcDest.Height);
        // select the bitmap object
        IntPtr hOld = SelectObject(hdcDest, hBitmap);
        // bitblt over
        StretchBlt(hdcDest, rcDest.X, rcDest.Y, rcDest.Width, rcDest.Height, hdcSrc, 0, 0, nWidth, nHeight, SRCCOPY);
        // restore selection
        SelectObject(hdcDest, hOld);
        // clean up 
        DeleteDC(hdcDest);
        ReleaseDC(handle, hdcSrc);

        // get a .NET image object for it
        Image img = Image.FromHbitmap(hBitmap);
        // free up the Bitmap object
        DeleteObject(hBitmap);
        return img;
    }