Take a screenshot of the active window in mono

>>>  小城故事吳儂軟語溫婉人心的力量  >>> 簡體     傳統

is there a way to take a screenshot of the active window using mono under Linux ?

Thanks in advance

Mike

Copyright (C) 2011 Me. You can use my code under the terms of the LGPL.

Normally, you could just use the below code:

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

But

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

seems to be buggy (width is wrong [too long, for example 5000px instead of 800], so CopyFromScreen throws an Exception). So I created the C# Xorg API, with a little help from the TAO Framework source that I found via Google. (The API is for far more than just screen dimensions, e.g. mouse, etc)

Usage:

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

Screenshot class:

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

And the Xorg API I created:

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

The structs you find here: http://www.koders.com/csharp/fid285D7FCDE1D18AFAE02DF16B57B246C89F4C74C1.aspx?s=zoom#L1031

You need to rename:

namespace Tao.Platform.X11

to

namespace Xorg.Structs

Graphics.CopyFromScreen(...) and clip only the active window using it's size and position?

http://stackoverflow.com/questions/3732486/take-a-screenshot-of-the-active-window-in-mono


stackoverflow 2013-08-31 23:15:38

[新一篇] 游戲燈塔:游戲文化視頻系列

[舊一篇] 一位陽朔畫家的鄉土情懷:將桂林山水“畫”出去
回頂部
寫評論


評論集


暫無評論。

稱謂:

内容:

驗證:


返回列表