Example #1
0
        /// <summary>
        /// captures a bitmap of the currently visible desktop (all screens)
        /// </summary>
        /// <returns></returns>
        public static Bitmap CaptureDesktop()
        {
            Win32ErrorException error = null;
            var screenRectangle       = Rectangle.Empty;
            var screens = Screen.AllScreens;

            // Create a rectangle encompassing all screens...
            foreach (var screen in screens)
            {
                screenRectangle = Rectangle.Union(screenRectangle, screen.Bounds);
            }
            //Create target composite bitmap
            var bitmap = new Bitmap(screenRectangle.Width, screenRectangle.Height);
            // Get a graphics object for the composite bitmap and initialize it...
            var graphics = Graphics.FromImage(bitmap);

            graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
            graphics.Clear(SystemColors.Desktop);
            // Get an HDC for the composite area...
            var target = graphics.GetHdc();

            // Now, loop through screens, BitBlting each to the composite HDC created above...
            foreach (var screen in screens)
            {
                // Create DC for each source monitor...
                var source = GDI32.SafeNativeMethods.CreateDC(IntPtr.Zero, screen.DeviceName, IntPtr.Zero, IntPtr.Zero);
                // Blt the source directly to the composite destination...
                var x       = screen.Bounds.X - screenRectangle.X;
                var y       = screen.Bounds.Y - screenRectangle.Y;
                var thisOne = GDI32.SafeNativeMethods.StretchBlt(target, x, y, screen.Bounds.Width, screen.Bounds.Height, source, 0, 0, screen.Bounds.Width, screen.Bounds.Height, GDI32.TernaryRasterOperations.SRCCOPY);
                if (!thisOne)
                {
                    error = new Win32ErrorException();
                }
                // Cleanup source HDC...
                GDI32.SafeNativeMethods.DeleteDC(source);
                if (!thisOne)
                {
                    break;
                }
            }
            // Cleanup destination HDC and Graphics...
            graphics.ReleaseHdc(target);
            graphics.Dispose();
            // Return composite bitmap or throw exception
            if (error != null)
            {
                throw error;
            }
            return(bitmap);
        }
Example #2
0
        /// <summary>
        /// Obtains the executable name of the process
        /// </summary>
        /// <param name="processID"></param>
        /// <param name="throwExException"></param>
        /// <returns></returns>
        public static string GetExecutableName(int processID, bool throwExException)
        {
            Exception error  = null;
            var       result = new StringBuilder(65536);
            var       len    = result.Capacity;

            if ((Environment.OSVersion.Platform == PlatformID.Win32NT) && (Environment.OSVersion.Version >= new Version("6.0")))
            {
                var processHandle = KERNEL32.SafeNativeMethods.OpenProcess(ACCESS_PROCESS.QUERY_INFORMATION, false, processID);
                if (processHandle == IntPtr.Zero)
                {
                    //try limited
                    processHandle = KERNEL32.SafeNativeMethods.OpenProcess(ACCESS_PROCESS.QUERY_LIMITED_INFORMATION, false, processID);
                }
                if (processHandle == IntPtr.Zero)
                {
                    error = new Win32ErrorException();
                }
                else if (!KERNEL32.SafeNativeMethods.QueryFullProcessImageName(processHandle, 0, result, ref len))
                {
                    error = new Win32ErrorException();
                }
                KERNEL32.SafeNativeMethods.CloseHandle(processHandle);
            }
            else
            {
                var processHandle = KERNEL32.SafeNativeMethods.OpenProcess(ACCESS_PROCESS.QUERY_INFORMATION | ACCESS_PROCESS.VM_READ, false, processID);
                if (processHandle == IntPtr.Zero)
                {
                    error = new Win32ErrorException();
                }
                else
                {
                    len = PSAPI.GetModuleFileNameEx(processHandle, IntPtr.Zero, result, len);
                    if (len == 0)
                    {
                        error = new Win32ErrorException();
                    }
                }
                KERNEL32.SafeNativeMethods.CloseHandle(processHandle);
            }
            if (throwExException && (error != null))
            {
                throw error;
            }
            return(result.ToString());
        }