c# 圖片窗口區域截圖代碼

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace GetFormDemo.Comm
{
    public struct RECT
    {
        public int x1;
        public int y1;
        public int x2;
        public int y2;
    }




    class ScreenCapture
    {
        [DllImport("user32.dll")]
        private static extern IntPtr GetDC(IntPtr hwnd);
        [DllImport("user32.dll")]
        private static extern IntPtr ReleaseDC(IntPtr hc, IntPtr hDest);
        [DllImport("user32.dll")]
        private static extern IntPtr GetWindowDC(IntPtr hwnd);
        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();
        [DllImport("user32.dll")]
        private static extern IntPtr GetWindowRect(IntPtr hWnd, out RECT rect);
        [DllImport("user32.dll")]
        private static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, UInt32 nFlags);
        [DllImport("user32.dll")]
        private static extern IntPtr GetDesktopWindow();

        [DllImport("gdi32.dll")]
        private static extern IntPtr CreateDC(
             string lpszDriver,         // driver name驅動名
             string lpszDevice,         // device name設備名
             string lpszOutput,         // not used; should be NULL
             IntPtr lpInitData          // optional printer data
         );
        [DllImport("gdi32.dll")]
        private static extern int BitBlt(
             IntPtr hdcDest, // handle to destination DC目標設備的句柄
             int nXDest,   // x-coord of destination upper-left corner目標對象的左上角的X座標
             int nYDest,   // y-coord of destination upper-left corner目標對象的左上角的Y座標
             int nWidth,   // width of destination rectangle目標對象的矩形寬度
             int nHeight, // height of destination rectangle目標對象的矩形長度
             IntPtr hdcSrc,   // handle to source DC源設備的句柄
             int nXSrc,    // x-coordinate of source upper-left corner源對象的左上角的X座標
             int nYSrc,    // y-coordinate of source upper-left corner源對象的左上角的Y座標
             CopyPixelOperation dwRop   // raster operation code光柵的操做值
         );
        //static extern int BitBlt(IntPtr hdcDest, int xDest, int yDest, int
        //wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, CopyPixelOperation rop);

        [DllImport("gdi32.dll")]
        private static extern IntPtr CreateCompatibleDC(
         IntPtr hdc // handle to DC
         );
        [DllImport("gdi32.dll")]
        private static extern IntPtr CreateCompatibleBitmap(
             IntPtr hdc,         // handle to DC
             int nWidth,      // width of bitmap, in pixels
             int nHeight      // height of bitmap, in pixels
         );
        [DllImport("gdi32.dll")]
        private static extern IntPtr SelectObject(
             IntPtr hdc,           // handle to DC
             IntPtr hgdiobj    // handle to object
         );
        [DllImport("gdi32.dll")]
        private static extern int DeleteDC(
            IntPtr hdc           // handle to DC
         );





        /// <summary>
        /// 抓取屏幕(層疊的窗口)
        /// </summary>
        /// <param name="x">左上角的橫座標</param>
        /// <param name="y">左上角的縱座標</param>
        /// <param name="width">抓取寬度</param>
        /// <param name="height">抓取高度</param>
        /// <returns></returns>
        public 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 Bitmap CaptureScreen()
        {
            Size screenSize = Screen.PrimaryScreen.Bounds.Size;
            return CaptureScreen(0, 0, screenSize.Width, screenSize.Height);
        }

        /// <summary>
        /// 全屏截圖
        /// </summary>
        /// <returns></returns>
        public Image CaptureScreenI()
        {
            return CaptureWindow(GetDesktopWindow());
        }

        /// <summary>
        /// 全屏指定區域截圖
        /// </summary>
        /// <returns></returns>
        public Image CaptureScreenI(RECT rect)
        {
            return CaptureWindow(GetDesktopWindow(), rect);
        }

        /// <summary>
        /// 指定窗口截圖
        /// </summary>
        /// <param name="handle">窗口句柄. (在windows應用程序中, 從Handle屬性得到)</param>
        /// <returns></returns>
        public Bitmap CaptureWindow(IntPtr hWnd)
        {
            IntPtr hscrdc = GetWindowDC(hWnd);
            RECT rect = new RECT();
            return CaptureWindow(hWnd, rect);
        }

        /// <summary>
        /// 指定窗口區域截圖
        /// </summary>
        /// <param name="handle">窗口句柄. (在windows應用程序中, 從Handle屬性得到)</param>
        /// <param name="rect">窗口中的一個區域</param>
        /// <returns></returns>
        public Bitmap CaptureWindow(IntPtr hWnd, RECT rect)
        {
            // 獲取設備上下文環境句柄
            IntPtr hscrdc = GetWindowDC(hWnd);

            // 建立一個與指定設備兼容的內存設備上下文環境(DC)
            IntPtr hmemdc = CreateCompatibleDC(hscrdc);
            IntPtr myMemdc = CreateCompatibleDC(hscrdc);

            // 返回指定窗體的矩形尺寸
            RECT rect1;
            GetWindowRect(hWnd, out rect1);

            // 返回指定設備環境句柄對應的位圖區域句柄
            IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, rect1.x2 - rect1.x1, rect1.y2 - rect1.y1);
            IntPtr myBitmap = CreateCompatibleBitmap(hscrdc, rect.x2 - rect.x1, rect.y2 - rect.y1);

            //把位圖選進內存DC 
            // IntPtr OldBitmap = (IntPtr)SelectObject(hmemdc, hbitmap);
            SelectObject(hmemdc, hbitmap);
            SelectObject(myMemdc, myBitmap);

            /////////////////////////////////////////////////////////////////////////////
            //
            // 下面開始所謂的做畫過程,此過程能夠用的方法不少,看你怎麼調用 API 了
            //
            /////////////////////////////////////////////////////////////////////////////

            // 直接打印窗體到畫布
            PrintWindow(hWnd, hmemdc, 0);

            // IntPtr hw = GetDesktopWindow();
            // IntPtr hmemdcClone = GetWindowDC(myBitmap);

            BitBlt(myMemdc, 0, 0, rect.x2 - rect.x1, rect.y2 - rect.y1, hmemdc, rect.x1, rect.y1, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);
            //SelectObject(myMemdc, myBitmap);

            Bitmap bmp = Bitmap.FromHbitmap(myBitmap);
            DeleteDC(hscrdc);
            DeleteDC(hmemdc);
            DeleteDC(myMemdc);
            return bmp;
        }

        /// <summary>
        /// 指定窗口截圖 保存爲圖片文件
        /// </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>
        /// 全屏截圖 保存爲文件
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="format"></param>
        public void CaptureScreenToFile(string filename, ImageFormat format)
        {
            Image img = CaptureScreen();
            img.Save(filename, format);
        }

        /// <summary>
        /// 設置 RECT 的左下右上
        /// </summary>
        /// <param name="rect">給出要設定的 RECT</param>
        /// <param name="left">左邊</param>
        /// <param name="bottom">下邊</param>
        /// <param name="right">右邊</param>
        /// <param name="top">上邊</param>
        public void SetRECT(ref RECT rect, int x1, int y1, int x2, int y2)
        {
            rect.x1 = x1;
            rect.y1 = y1;
            rect.x2 = x2;
            rect.y2 = y2;            
           
        }

        /// <summary>
        /// 合併圖片
        /// </summary>
        /// <param name="bmp1">圖片1</param>
        /// <param name="bmp2">圖片2</param>
        public Bitmap HBpic(Bitmap bmp1, Bitmap bmp2)
        {
            Bitmap newBmp = new Bitmap(bmp1.Width, bmp1.Height + bmp2.Height);
            var g = Graphics.FromImage(newBmp);
            g.DrawImage(bmp1, 0, 0);
            g.DrawImage(bmp2, 0, bmp1.Height);

            return newBmp;
        }



    }

}

 

本人轉載至http://www.cnblogs.com/herbertchina/p/4492922.html  詳細請看這
相關文章
相關標籤/搜索