정리된 C# 화면 캡처, 컨트롤 캡처 프로그램
                                            
 7180 단어  C#
                    
스크린 캡처 포함(스크린에서 본 것과 일치);컨트롤 캡처 (이 컨트롤이 이 창에 완전히 표시되고 다른 컨트롤에 가려지지 않으면 캡처할 수 있습니다)
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LC
{
    class ScreenCapture
    {
        #region     
        /// <summary>
        ///     (     )
        /// </summary>
        /// <param name="x">       </param>
        /// <param name="y">       </param>
        /// <param name="width">    </param>
        /// <param name="height">    </param>
        /// <returns></returns>
        public static Bitmap captureScreen(int x, int y, int width, int height)
        {
            Bitmap bmp = new Bitmap(width, height);
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.CopyFromScreen(new Point(x, y), new Point(0, 0), bmp.Size);
                g.Dispose();
            }
            //bit.Save(@"capture2.png");
            return bmp;
        }
        /// <summary>
        ///        
        /// </summary>
        /// <returns></returns>
        public static Bitmap captureScreen()
        {
            Size screenSize = Screen.PrimaryScreen.Bounds.Size;
            return captureScreen(0,0,screenSize.Width,screenSize.Height);
        }
        #endregion
        #region   BitBlt      ,         
        /// <summary>
        ///   (  )   ,       (        )          ,  BitBlt  
        /// </summary>
        /// <param name="control">        </param>
        /// <returns>      ,             </returns>
        public static Bitmap captureControl(Control control)
        {
            //  API  
            IntPtr hSrce = GetWindowDC(control.Handle);
            IntPtr hDest = CreateCompatibleDC(hSrce);
            IntPtr hBmp = CreateCompatibleBitmap(hSrce, control.Width, control.Height);
            IntPtr hOldBmp = SelectObject(hDest, hBmp);
            if (BitBlt(hDest, 0, 0, control.Width, control.Height, hSrce, 0, 0, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt))
            {
                Bitmap bmp = Image.FromHbitmap(hBmp);
                SelectObject(hDest, hOldBmp);
                DeleteObject(hBmp);
                DeleteDC(hDest);
                ReleaseDC(control.Handle, hSrce);
                // bmp.Save(@"a.png");
                // bmp.Dispose();
                return bmp;
            }
            return null;
        }
//         /// <summary>
//         ///    !!!!!         
//         ///   (  )       ,       (        )          ,  BitBlt  
//         /// </summary>
//         /// <param name="control">        </param>
//         /// <returns>  (  )       </returns>
//         public static Bitmap captureClientArea(Control control)
//         {
// 
//             Size sz = control.Size;
//             Rectangle rect = control.ClientRectangle;
//             
// 
//             //  API  
//             IntPtr hSrce = GetWindowDC(control.Handle);
//             IntPtr hDest = CreateCompatibleDC(hSrce);
//             IntPtr hBmp = CreateCompatibleBitmap(hSrce, rect.Width, rect.Height);
//             IntPtr hOldBmp = SelectObject(hDest, hBmp);
//             if (BitBlt(hDest, 0, 0, rect.Width, rect.Height, hSrce, rect.X, rect.Y, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt))
//             {
//                 Bitmap bmp = Image.FromHbitmap(hBmp);
//                 SelectObject(hDest, hOldBmp);
//                 DeleteObject(hBmp);
//                 DeleteDC(hDest);
//                 ReleaseDC(control.Handle, hSrce);
//                 // bmp.Save(@"a.png");
//                 // bmp.Dispose();
//                 return bmp;
//             }
//             return null;
// 
//         }
        #endregion
        #region   PrintWindow      ,         
        /// <summary>
        ///      ,             ,  PrintWindow  
        /// </summary>
        /// <param name="control">        </param>
        /// <returns>     ,             </returns>
        public static Bitmap captureWindowUsingPrintWindow(Form form)
        {
            return GetWindow(form.Handle);
        }
        private 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;
        }
        #endregion
        #region  DLL calls
        [DllImport("gdi32.dll")]
        static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int
        wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, CopyPixelOperation rop);
        [DllImport("gdi32.dll")]
        static extern IntPtr DeleteDC(IntPtr hDc);
        [DllImport("gdi32.dll")]
        static extern IntPtr DeleteObject(IntPtr hDc);
        [DllImport("gdi32.dll")]
        static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
        [DllImport("gdi32.dll")]
        static extern IntPtr CreateCompatibleDC(IntPtr hdc);
        [DllImport("gdi32.dll")]
        static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp);
        [DllImport("user32.dll")]
        public static extern IntPtr GetDesktopWindow();
        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowDC(IntPtr ptr);
        [DllImport("user32.dll")]
        public static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, UInt32 nFlags);
        [DllImport("user32.dll")]
        static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDc);
        #endregion
    }
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
WebView2를 Visual Studio 2017 Express에서 사용할 수 있을 때까지Evergreen .Net Framework SDK 4.8 VisualStudio2017에서 NuGet을 사용하기 때문에 패키지 관리 방법을 packages.config 대신 PackageReference를 사용해야...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.