0

I am currently taking a screenshot of an external application with PrintWindow and i want to save it to my desktop.

This is my code :

// Get proc
            Process proc = Process.GetProcessesByName("procName").Single();
            IntPtr hWnd = proc.MainWindowHandle;

            // Restore proc if minimised
            int style = GetWindowLong(hWnd, GWL_STYLE);
            if ((style & WS_MINIMIZE) == WS_MINIMIZE)
                ShowWindow(hWnd, WindowShowStyle.Restore);

            // Get RECT
            RECT rect;
            GetWindowRect(new HandleRef(this, hWnd), out rect);

            // Get screenshot
            int width = rect.Right - rect.Left;
            int height = rect.Bottom - rect.Top;
            Bitmap bmp = new Bitmap(width, height);
            using (Graphics g = Graphics.FromImage(bmp))
            {
                IntPtr dc = g.GetHdc();

                if (!PrintWindow(hWnd, dc, 0))
                {
                    int error = Marshal.GetLastWin32Error();
                    var exception = new System.ComponentModel.Win32Exception(error);
                    Debug.WriteLine("ERROR: " + error + ": " + exception.Message);
                    return;
                }

                //Thread.Sleep(200);
                bmp.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\test.jpeg", ImageFormat.Jpeg);
                panel1.BackgroundImage = bmp;
                g.ReleaseHdc(dc);
            }

The panel1 shows me the good image, the actual screenshot of the application. When i go to my desktop, i find a test.jpeg but it's all black. Why?

Thanks !

Haytam
  • 4,643
  • 2
  • 20
  • 43
  • For him, only the background was black. For me, the whole image is black – Haytam Jul 25 '16 at 17:18
  • "_.. **JPEG** Image has a black background by default, so if your text color is also black you will get a black image. If your image has no background color, you must save it as **PNG** .._" From [**Saved Bitmap is black**](http://stackoverflow.com/questions/28019010/saved-bitmap-is-black) – Khalil Khalaf Jul 25 '16 at 17:19
  • My image is a screenshot of an application, it does contain black text but it's a lot more than just black text. – Haytam Jul 25 '16 at 17:20
  • We cannot see what's going on in the `PrintWindow()` method. – Mr Anderson Jul 25 '16 at 17:20
  • Its a user32 API method, basicly it just screens the application and save it in the graphics object, like i said, the image is well shown in the panel1 but after saving it, it's all black – Haytam Jul 25 '16 at 17:21
  • So why not saving it as **PNG**? – Khalil Khalaf Jul 25 '16 at 17:22
  • Crystal ball says that you have a problem with the alpha channel, it won't be set by PrintWindow(). First try g.Clear(Color.White), next try adding PixelFormat.Format24bppRgb to the bitmap constructor call. Also beware that not all program support PrintWindow(), a browser won't for example. Making a screenshot is best. – Hans Passant Jul 25 '16 at 17:23
  • i got it to work, thanks to everyone for their time ! – Haytam Jul 25 '16 at 17:26

1 Answers1

0

I have successed at saving it as .jpg by changing the code to become :

// Get screenshot
            int width = rect.Right - rect.Left;
            int height = rect.Bottom - rect.Top;
            Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.Clear(Color.White);
                IntPtr dc = g.GetHdc();

                if (!PrintWindow(hWnd, dc, 0))
                {
                    int error = Marshal.GetLastWin32Error();
                    var exception = new System.ComponentModel.Win32Exception(error);
                    Debug.WriteLine("ERROR: " + error + ": " + exception.Message);
                    return;
                }

                g.ReleaseHdc(dc);
            }

            // Save the screenshot
            bmp.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\test.jpg", ImageFormat.Jpeg);
            panel1.BackgroundImage = bmp;
Haytam
  • 4,643
  • 2
  • 20
  • 43
  • don't forget to dispose the old Bitmap object that `panel1.BackgroundImage` had assigned to it. Usually I would do somthing like `var temp = panel1.BackgroundImage; panel1.BackgroundImage = bmp; if(temp != null) temp.Dispose();` – Scott Chamberlain Jul 25 '16 at 18:17