I
I
Ivan Volodin2020-12-30 20:11:44
Windows
Ivan Volodin, 2020-12-30 20:11:44

How to extract the Bitmap of a minimized process in Windows?

I extract the Bitmap of the process with the following code:

*The code*

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace ConsoleApplication8
{
    public static class ProcessPictureGetter
    {
        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }

        public static void SaveProcessPicture(string processName, string savePath)
        {
            Bitmap savedProcessPicture = GetProcessPicture(processName);
            savedProcessPicture.Save(savePath, System.Drawing.Imaging.ImageFormat.Png);
            savedProcessPicture.Dispose();
        }

        public static Bitmap GetProcessPicture(string processName)
        {
            IntPtr hDesk = FindWindow(null, processName);
            RECT rct;
            GetWindowRect(hDesk, out rct);
            Size sz = new System.Drawing.Size(rct.Right - rct.Left + 1, rct.Bottom - rct.Top + 1);

            IntPtr hSrce = GetWindowDC(hDesk);
            IntPtr hDest = CreateCompatibleDC(hSrce);
            IntPtr hBmp = CreateCompatibleBitmap(hSrce, sz.Width, sz.Height);
            IntPtr hOldBmp = SelectObject(hDest, hBmp);
            bool b = BitBlt(hDest, 0, 0, sz.Width, sz.Height, hSrce, 0, 0, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);
            Bitmap bmp = Bitmap.FromHbitmap(hBmp);
            SelectObject(hDest, hOldBmp);
            DeleteObject(hBmp);
            DeleteDC(hDest);
            ReleaseDC(hDesk, hSrce);

            return bmp;

        }

        // P/Invoke declarations
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);

        [DllImport("gdi32.dll")]
        public static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int
        wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, CopyPixelOperation rop);
        [DllImport("user32.dll")]
        public static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDc);
        [DllImport("gdi32.dll")]
        public static extern IntPtr DeleteDC(IntPtr hDc);
        [DllImport("gdi32.dll")]
        public static extern IntPtr DeleteObject(IntPtr hDc);
        [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 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", SetLastError = true)]
        public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
    }
}



And it doesn't work for minimized windows and Chrome , just black rectangles instead. Is it possible to somehow get the Bitmap of a process, regardless of its curtailment?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
H
Hemul GM, 2020-12-30
@int_i

No. At what, the process has nothing to do with it. You get a screen of windows, not a process

V
Vasily Bannikov, 2020-12-30
@vabka

Since you screenshot Chrome, I can suggest using selenium / pupeter / playwright for this - they allow you to run Chrome without a window and take screenshots of pages

B
BasiC2k, 2020-12-31
@BasiC2k

Make the window visible, take a screenshot and hide it. For a fraction of a second, the user will not notice anything.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question