Сделай скриншот активного окна в моно - PullRequest
3 голосов
/ 17 сентября 2010

Есть ли способ сделать снимок экрана активного окна, используя моно под Linux?

Ответы [ 2 ]

1 голос
/ 15 марта 2011

Copyright (C) 2011 Me. Вы можете использовать мой код в соответствии с условиями LGPL.

Обычно вы можете просто использовать следующий код:

 public static void GetScreenshot(System.Windows.Forms.PictureBox pbThisPictureBox)
        {
            System.Drawing.Rectangle rectScreenBounds = System.Windows.Forms.Screen.GetBounds();
            System.Drawing.Bitmap bmpScreenshot = new System.Drawing.Bitmap(rectScreenBounds.Width, rectScreenBounds.Height);

            using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmpScreenshot))
            {
                g.CopyFromScreen(System.Drawing.Point.Empty, System.Drawing.Point.Empty, rectScreenBounds.Size);
            } // End Using g

            pbThisPictureBox.Image = bmpScreenshot;

        } // End Sub GetScreenshot

Но

System.Windows.Forms.Screen.GetBounds(), 

, кажется, глючит (ширина неверна [слишком длинная, например, 5000 пикселей вместо 800], поэтому CopyFromScreen создает исключение) Поэтому я создал C # Xorg API с небольшой помощью из источника TAO Framework, который я нашел через Google. (API предназначен для гораздо большего, чем просто размеры экрана, например, мышь и т. Д.)

Использование:

Tools.Graphics.ScreenShot.GetScreenshot(this.pictureBox1);

Класс снимка экрана:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Tools.Graphics
{


    class ScreenShot
    {
        // http://www.eggheadcafe.com/tutorials/aspnet/064b41e4-60bc-4d35-9136-368603bcc27a/7zip-lzma-inmemory-com.aspx

        protected static System.Drawing.Rectangle rectScreenBounds = GetScrBounds();
        //protected System.Drawing.Rectangle rectScreenBounds = System.Windows.Forms.Screen.GetBounds(System.Drawing.Point.Empty);
        protected static System.Drawing.Bitmap bmpScreenshot = new System.Drawing.Bitmap(1, 1);


        protected static System.Drawing.Rectangle GetXorgScreen()
        {
            int screen_width = 0;
            int screen_height = 0;

            IntPtr display = Xorg.API.XOpenDisplay(System.IntPtr.Zero);

            if (display == IntPtr.Zero)
            {
                Console.WriteLine("Error: Failed on XOpenDisplay.\n");
            }
            else
            {
                screen_width = Xorg.API.DisplayWidth(display, Xorg.API.XDefaultScreen(display));
                screen_height = Xorg.API.DisplayHeight(display, Xorg.API.XDefaultScreen(display));

                Xorg.API.XCloseDisplay(display);
                Console.WriteLine("Width: " + screen_width.ToString() + " Height: " + screen_height.ToString());
            } // End Else (display == IntPtr.Zero)

            return new System.Drawing.Rectangle(0, 0, screen_width, screen_height);
        } // End Function GetXorgScreen


        protected static System.Drawing.Rectangle GetScrBounds()
        {
            // Wouldn't be necessary if GetBounds on mono wasn't buggy.
            if (Environment.OSVersion.Platform == PlatformID.Unix)
                return GetXorgScreen();

            return System.Windows.Forms.Screen.GetBounds(System.Drawing.Point.Empty);
        } // End Function GetScrBounds


        // http://jalpesh.blogspot.com/2007/06/how-to-take-screenshot-in-c.html
        // Tools.Graphics.ScreenShot.GetScreenshot();
        public static System.Drawing.Bitmap GetScreenshot()
        {
            /*
            if (this.pictureBox1.Image != null)
                this.pictureBox1.Image.Dispose();
            */
            bmpScreenshot.Dispose();
            bmpScreenshot = new System.Drawing.Bitmap(rectScreenBounds.Width, rectScreenBounds.Height);

            using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmpScreenshot))
            {
                g.CopyFromScreen(System.Drawing.Point.Empty, System.Drawing.Point.Empty, rectScreenBounds.Size);
            } // End Using g

            return bmpScreenshot;
        } // End Function GetScreenshotImage


        // http://jalpesh.blogspot.com/2007/06/how-to-take-screenshot-in-c.html
        // Tools.Graphics.ScreenShot.GetScreenshot(this.PictureBox1);
        public static void GetScreenshot(System.Windows.Forms.PictureBox pbThisPictureBox)
        {
            /*
            if (this.pictureBox1.Image != null)
                this.pictureBox1.Image.Dispose();
            */
            bmpScreenshot.Dispose();
            bmpScreenshot = new System.Drawing.Bitmap(rectScreenBounds.Width, rectScreenBounds.Height);

            using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmpScreenshot))
            {
                g.CopyFromScreen(System.Drawing.Point.Empty, System.Drawing.Point.Empty, rectScreenBounds.Size);
            } // End Using g

            pbThisPictureBox.Image = bmpScreenshot;

        } // End Sub GetScreenshot


        // http://jalpesh.blogspot.com/2007/06/how-to-take-screenshot-in-c.html
        // Tools.Graphics.ScreenShot.SaveScreenshot(@"C:\Users\Stefan.Steiger.COR\Desktop\test.jpg");
        public static void SaveScreenshot(string strFileNameAndPath)
        {
            System.Drawing.Rectangle rectBounds = System.Windows.Forms.Screen.GetBounds(System.Drawing.Point.Empty);
            using (System.Drawing.Bitmap bmpScreenshotBitmap = new System.Drawing.Bitmap(rectBounds.Width, rectBounds.Height))
            {

                using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmpScreenshotBitmap))
                {
                    g.CopyFromScreen(System.Drawing.Point.Empty, System.Drawing.Point.Empty, rectBounds.Size);
                } // End Using g

                bmpScreenshotBitmap.Save(strFileNameAndPath, System.Drawing.Imaging.ImageFormat.Jpeg);
            } // End Using

        } // End Sub SaveScreenshot


    } // End  Class ScreenShot


} // End Namespace Tools.Graphics

И API Xorg, который я создал:

using System;
using System.Runtime.InteropServices;

using Xorg.Structs;


// http://www.koders.com/csharp/fid5A7CBAABE4E399E1BED8C2C2FB6E1B36C289628D.aspx?s=zoom#L293
namespace Xorg
{


    // http://www.koders.com/csharp/fidFC528FE04222FE631D31990CC4B30889DB6ACCA8.aspx?s=socket
    public class API
    {


        [Flags]
        internal enum EventMask
        {
            NoEventMask             = 0,
            KeyPressMask            = 1<<0,
            KeyReleaseMask          = 1<<1,
            ButtonPressMask         = 1<<2,
            ButtonReleaseMask       = 1<<3,
            EnterWindowMask         = 1<<4,
            LeaveWindowMask         = 1<<5,
            PointerMotionMask       = 1<<6,
            PointerMotionHintMask   = 1<<7,
            Button1MotionMask       = 1<<8,
            Button2MotionMask       = 1<<9,
            Button3MotionMask       = 1<<10,
            Button4MotionMask       = 1<<11,
            Button5MotionMask       = 1<<12,
            ButtonMotionMask        = 1<<13,
            KeymapStateMask         = 1<<14,
            ExposureMask            = 1<<15,
            VisibilityChangeMask    = 1<<16,
            StructureNotifyMask     = 1<<17,
            ResizeRedirectMask      = 1<<18,
            SubstructureNotifyMask  = 1<<19,
            SubstructureRedirectMask= 1<<20,
            FocusChangeMask         = 1<<21,
            PropertyChangeMask      = 1<<22,
            ColormapChangeMask      = 1<<23,
            OwnerGrabButtonMask     = 1<<24
        }


        protected const string m_strSharedObjectName = "libX11";
        protected const string m_strSharedObjectName_Video = "libXxf86vm";

        // For AIX shared object, use "dump -Tv /path/to/foo.o"
        // For an ELF shared library, use "readelf -s /path/to/libfoo.so"
        // or (if you have GNU nm) "nm -D /path/to/libfoo.so"
        // For a Windows DLL, use "dumpbin /EXPORTS foo.dll".


        // nm -D $(locate libX11 | sed '/\/usr\/lib/!d;' | grep ".so$")
        // nm -D $(locate "libX11.so" | grep ".so$")
        // nm -D $(locate "libX11.so" | grep ".so$") | grep "DisplayHeight"


        [DllImport(m_strSharedObjectName, EntryPoint = "XOpenDisplay")]
        internal extern static IntPtr XOpenDisplay(IntPtr display);

        [DllImport(m_strSharedObjectName, EntryPoint = "XDefaultScreen")]
        internal extern static int XDefaultScreen(IntPtr display);

        [DllImport(m_strSharedObjectName, EntryPoint = "XDisplayHeight")]
        internal extern static int DisplayHeight(IntPtr display, int screen_number);

        [DllImport(m_strSharedObjectName, EntryPoint = "XDisplayWidth")]
        internal extern static int DisplayWidth(IntPtr display, int screen_number);

        [DllImport(m_strSharedObjectName, EntryPoint = "XRootWindow")]
        internal extern static IntPtr XRootWindow(IntPtr display, int screen_number);

        [DllImport(m_strSharedObjectName, EntryPoint = "XCloseDisplay")]
        internal extern static int XCloseDisplay(IntPtr display);

        [DllImport(m_strSharedObjectName, EntryPoint = "XSynchronize")]
        internal extern static IntPtr XSynchronize(IntPtr display, bool onoff);

        [DllImport(m_strSharedObjectName, EntryPoint = "XGrabServer")]
        internal extern static void XGrabServer(IntPtr display);

        [DllImport(m_strSharedObjectName, EntryPoint = "XUngrabServer")]
        internal extern static void XUngrabServer(IntPtr display);

        [DllImport(m_strSharedObjectName)]
        internal extern static int XFlush(IntPtr display);

        [DllImport(m_strSharedObjectName, EntryPoint = "XFree")]
        internal extern static int XFree(IntPtr data);

        //[DllImport(m_strSharedObjectName, EntryPoint = "XSendEvent")]
        //internal extern static int XSendEvent(IntPtr display, IntPtr window, bool propagate, IntPtr event_mask, ref XEvent send_event);

        [DllImport(m_strSharedObjectName, EntryPoint = "XSendEvent")]
        internal extern static int XSendEvent(IntPtr display, IntPtr window, bool propagate, int event_mask, ref XEvent send_event);

        //[DllImport (m_strSharedObjectName, EntryPoint="XSendEvent")]
        //internal extern static int XSendEvent(IntPtr display, IntPtr window, bool propagate, EventMask event_mask, ref XEvent send_event);
        //internal extern static int XSendEvent(IntPtr display, IntPtr window, bool propagate, EventMask event_mask, ref XClientMessageEvent send_event);

        [DllImport(m_strSharedObjectName, EntryPoint = "XQueryPointer")]
        internal extern static bool XQueryPointer(IntPtr display, IntPtr window, out IntPtr root, out IntPtr child, out int root_x, out int root_y, out int win_x, out int win_y, out int keys_buttons);

        [DllImport(m_strSharedObjectName, EntryPoint = "XWarpPointer")]
        internal extern static uint XWarpPointer(IntPtr display, IntPtr src_w, IntPtr dest_w, int src_x, int src_y, uint src_width, uint src_height, int dest_x, int dest_y);

        [DllImport(m_strSharedObjectName, EntryPoint = "XGetWindowProperty")]
        internal extern static int XGetWindowProperty(IntPtr display, IntPtr window, IntPtr atom, IntPtr long_offset, IntPtr long_length, bool delete, IntPtr req_type, out IntPtr actual_type, out int actual_format, out IntPtr nitems, out IntPtr bytes_after, ref IntPtr prop);


    } // End Class Mouse


} // End Namespace Xorg

Структуры, которые вы найдете здесь: http://www.koders.com/csharp/fid285D7FCDE1D18AFAE02DF16B57B246C89F4C74C1.aspx?s=zoom#L1031

Вам необходимо переименовать:

namespace Tao.Platform.X11

до

namespace Xorg.Structs
0 голосов
/ 20 сентября 2010

Graphics.CopyFromScreen(...) и обрезать только активное окно, используя его размер и положение?

...