民初思韻網

加入收藏   設為首頁
選擇語言   簡體中文
你好,請 登陸 或 注冊
首頁 人文思韻 傳奇人物 歷史思潮 時代作品 話題討論 國民思韻 民初捐助 賬戶管理
  搜索  
    話題討論 >>> 新編網絡文學
字體    

Take a screenshot of the active window in mono
Take a screenshot of the active window in mono
stackoverflow     阅读简体中文版

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

2013-08-31 23:15

歡迎訂閱我們的微信公眾賬號!
春秋茶館訂閱號
微信號 season-tea(春秋茶館)
每天分享一篇科技/遊戲/人文類的資訊,點綴生活,啟迪思想,探討古典韻味。
  清末民初歷史人物  民初人物
從國務總理到修道士
陸徵祥(1871-1949年),字子欣,一作子興,上海人。中國近代著名的天主教人士,也是著名的外交官。他出生于一個基督教家庭,父親是一位基督教新教徒,曾經在倫敦傳教會工作....
為元首清廉不阿至情至性
林森(1868年—1943年8月1日)字子超,號長仁。福建閩侯人。1868年出生于福建省閩侯縣尚干鄉,1884年于臺北電信局工作。1902年到上海海關任職,其間參加反清活....
資助民初精神網
        回頂部     寫評論

 
評論集
暫無評論!
發表評論歡迎你的評論
昵稱:     登陸  註冊
主頁:  
郵箱:  (僅管理員可見)

驗證:   验证码(不區分大小寫)  
© 2011   民初思韻網-清末民初傳奇時代的發現與復興   版權所有   加入收藏    設為首頁    聯繫我們    1616導航