Public feed #890966

Something

public text v1 · immutable
Raw Edit
·published 2008-02-04 06:27 UTC
rendered paste body
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace Devil-666.Screen
{
    /// <summary>
    /// Capture class
    /// </summary>
    public sealed class Capture
    {
        #region gdi32
        private const int SRCCOPY = 13369376;

        [DllImport("gdi32.dll", EntryPoint = "DeleteDC")]
        private static extern IntPtr DeleteDC(IntPtr hDc);

        [DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
        private static extern IntPtr DeleteObject(IntPtr hDc);

        [DllImport("gdi32.dll", EntryPoint = "BitBlt")]
        private 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")]
        private static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);

        [DllImport("gdi32.dll", EntryPoint = "CreateCompatibleDC")]
        private static extern IntPtr CreateCompatibleDC(IntPtr hdc);

        [DllImport("gdi32.dll", EntryPoint = "SelectObject")]
        private static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp);
        #endregion
        #region User32
        private const int SM_CXSCREEN = 0;
        private const int SM_CYSCREEN = 1;

        [DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
        private static extern IntPtr GetDesktopWindow();

        [DllImport("user32.dll", EntryPoint = "GetDC")]
        private static extern IntPtr GetDC(IntPtr ptr);

        [DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
        private static extern int GetSystemMetrics(int abc);

        [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
        private static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
        #endregion
        private struct Size
        {
            public int X;
            public int Y;
        }
        private Capture()
        {
        }
        /// <summary>
        /// Get desktop image
        /// </summary>
        /// <returns></returns>
        public static Bitmap GetDesktopImage()
        {
            IntPtr m_HBitmap;//=null;
            System.Drawing.Bitmap bmp;
            Size size;
            IntPtr hDC = GetDC(GetDesktopWindow());
            IntPtr hMemDC = CreateCompatibleDC(hDC);
            size.X = GetSystemMetrics(SM_CXSCREEN);
            size.Y = GetSystemMetrics(SM_CYSCREEN);
            m_HBitmap = CreateCompatibleBitmap(hDC, size.X, size.Y);

            if (m_HBitmap != IntPtr.Zero)
            {
                IntPtr hOld = (IntPtr)SelectObject(hMemDC, m_HBitmap);
                BitBlt(hMemDC, 0, 0, size.X, size.Y, hDC, 0, 0, SRCCOPY);
                SelectObject(hMemDC, hOld);
                DeleteDC(hMemDC);
                ReleaseDC(GetDesktopWindow(), hDC);
                bmp = System.Drawing.Image.FromHbitmap(m_HBitmap);
                DeleteObject(m_HBitmap);
                return bmp;
            }
            return null;
        }
    }
}