Copyright (C) 2011 Me. You can use my code in accordance with the terms of the LGPL.
You can usually just use the code below:
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);
}
pbThisPictureBox.Image = bmpScreenshot;
}
But
System.Windows.Forms.Screen.GetBounds(),
it seems a buggy (the 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, which I found through Google. (The API is much more than just screen sizes, such as a mouse, etc.)
Using:
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
{
protected static System.Drawing.Rectangle rectScreenBounds = GetScrBounds();
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());
}
return new System.Drawing.Rectangle(0, 0, screen_width, screen_height);
}
protected static System.Drawing.Rectangle GetScrBounds()
{
if (Environment.OSVersion.Platform == PlatformID.Unix)
return GetXorgScreen();
return System.Windows.Forms.Screen.GetBounds(System.Drawing.Point.Empty);
}
public static System.Drawing.Bitmap GetScreenshot()
{
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);
}
return bmpScreenshot;
}
public static void GetScreenshot(System.Windows.Forms.PictureBox pbThisPictureBox)
{
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);
}
pbThisPictureBox.Image = bmpScreenshot;
}
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);
}
bmpScreenshotBitmap.Save(strFileNameAndPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
}
And the Xorg API I created:
using System;
using System.Runtime.InteropServices;
using Xorg.Structs;
namespace Xorg
{
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";
[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, int event_mask, ref XEvent 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);
}
}
, :
http://www.koders.com/csharp/fid285D7FCDE1D18AFAE02DF16B57B246C89F4C74C1.aspx?s=zoom#L1031
:
namespace Tao.Platform.X11
namespace Xorg.Structs