Скриншот гаджетов рабочего стола windows 7 - C#
Формулировка задачи:
Доброй ночи.
Спешу сообщить - да, гуглил.
Естественно - уйма всего нагуглилось, но:
К этому коду есть 2 основных нарекания:
1) Течет память. Я новичок совсем, нагуглил, что нужно "обернуть" вот это и это:
в using(), пока разбираюсь "как это правильно делается";
2) На получаемом скрине нет тех самых гаджетов, потому что оверлей, судя по всему.
Вот тут не понял: куда оно "уже включено"? На всякий случай скачал архив, в нем 2 папки, подозреваю, нужно NetFX4.0 подложить к проекту: как/куда/как "прописать" ?
Далее идет совершенно непонятный код, в котором по названиям вызываемых методов даже примерно не понять - в какой строке делается этот самый скриншот, куда и в каком виде он "кладется", и соответственно - не ясно как вызывать/как сохранять...
Вот еще статья с того же источника: Еще одна статья на ту же тему, тут не многим легче, если "слепо" скопировать код в студию:
то сразу же получаю ошибку:
Как так? Ведь парой шагов ранее я уже установил SlimDX Runtime .NET 4.0 x64 (January 2012).msi, или этого не достаточно??
Sorry за много текста, повторю задачу: необходимо получать скриншоты рабочего стола, на котором крутятся некоторые оконные программы и "для контроля" окрыты гаджеты рабочего стола Win7 часы с календарем. Как "проще всего" это можно реализовать??
Во первых
, вот "почти" рабочий код:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ScreenShot
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(printscreen as Image);
graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);
printscreen.Save(@"screenshot.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); Graphics graphics = Graphics.FromImage(printscreen as Image);
Во вторых
, мне бы подошло и "костыльное" решение, через принудительное отключение ускорения DirectDraw в системе, но тут "засада" - почему-то при открытии “dxdiag” as Administrator на вкладке Display у меня нет кнопок для включения/отключения режимов! (( Как это выглядит у других: ссылка на сторонний сайт Как это выглядит у меня: (приложил скриншот dxdiag.png)И наконец, в третьих
: абсолютно во всех ветках (включая стековерфлоу) как только речь заходит об скриншоте оверлея ссылаются на одну и ту же страницу: Чудо а не рецепт, панацея просто, но к моему глубочайшему сожалению я мало того, что слаб в шарпе - я еще и с английским "на Вы". Поясните, пожалуйста, "на пальцах", пошагово, как с этим работать, так например, с самого начала: 1) говорится, что будет использоваться SlimDX и дается ссылка на скачивание - скачал SlimDX Runtime .NET 4.0 x64 (January 2012).msi, установил; 2)
"The download already includes the EasyHook binaries, but if you want to download them yourself you can find them at CodePlex here."
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Runtime.InteropServices;
namespace Spazzarama.ScreenCapture
{
public static class Direct3DCapture
{
private static SlimDX.Direct3D9.Direct3D _direct3D9 = new SlimDX.Direct3D9.Direct3D();
private static Dictionary<IntPtr, SlimDX.Direct3D9.Device> _direct3DDeviceCache = new Dictionary<IntPtr, SlimDX.Direct3D9.Device>();
/// <summary>
/// Capture the entire client area of a window
/// </summary>
/// <param name="hWnd"></param>
/// <returns></returns>
public static Bitmap CaptureWindow(IntPtr hWnd)
{
return CaptureRegionDirect3D(hWnd, NativeMethods.GetAbsoluteClientRect(hWnd));
}
/// <summary>
/// Capture a region of the screen using Direct3D
/// </summary>
/// <param name="handle">The handle of a window</param>
/// <param name="region">The region to capture (in screen coordinates)</param>
/// <returns>A bitmap containing the captured region, this should be disposed of appropriately when finished with it</returns>
public static Bitmap CaptureRegionDirect3D(IntPtr handle, Rectangle region)
{
IntPtr hWnd = handle;
Bitmap bitmap = null;
// We are only supporting the primary display adapter for Direct3D mode
SlimDX.Direct3D9.AdapterInformation adapterInfo = _direct3D9.Adapters.DefaultAdapter;
SlimDX.Direct3D9.Device device;
#region Get Direct3D Device
// Retrieve the existing Direct3D device if we already created one for the given handle
if (_direct3DDeviceCache.ContainsKey(hWnd))
{
device = _direct3DDeviceCache[hWnd];
}
// We need to create a new device
else
{
// Setup the device creation parameters
SlimDX.Direct3D9.PresentParameters parameters = new SlimDX.Direct3D9.PresentParameters();
parameters.BackBufferFormat = adapterInfo.CurrentDisplayMode.Format;
Rectangle clientRect = NativeMethods.GetAbsoluteClientRect(hWnd);
parameters.BackBufferHeight = clientRect.Height;
parameters.BackBufferWidth = clientRect.Width;
parameters.Multisample = SlimDX.Direct3D9.MultisampleType.None;
parameters.SwapEffect = SlimDX.Direct3D9.SwapEffect.Discard;
parameters.DeviceWindowHandle = hWnd;
parameters.PresentationInterval = SlimDX.Direct3D9.PresentInterval.Default;
parameters.FullScreenRefreshRateInHertz = 0;
// Create the Direct3D device
device = new SlimDX.Direct3D9.Device(_direct3D9, adapterInfo.Adapter, SlimDX.Direct3D9.DeviceType.Hardware, hWnd, SlimDX.Direct3D9.CreateFlags.SoftwareVertexProcessing, parameters);
_direct3DDeviceCache.Add(hWnd, device);
}
#endregion
// Capture the screen and copy the region into a Bitmap
using (SlimDX.Direct3D9.Surface surface = SlimDX.Direct3D9.Surface.CreateOffscreenPlain(device, adapterInfo.CurrentDisplayMode.Width, adapterInfo.CurrentDisplayMode.Height, SlimDX.Direct3D9.Format.A8R8G8B8, SlimDX.Direct3D9.Pool.SystemMemory))
{
device.GetFrontBufferData(0, surface);
// Update: thanks digitalutopia1 for pointing out that SlimDX have fixed a bug
// where they previously expected a RECT type structure for their Rectangle
bitmap = new Bitmap(SlimDX.Direct3D9.Surface.ToStream(surface, SlimDX.Direct3D9.ImageFileFormat.Bmp, new Rectangle(region.Left, region.Top, region.Width, region.Height)));
// Previous SlimDX bug workaround: new Rectangle(region.Left, region.Top, region.Right, region.Bottom)));
}
return bitmap;
}
}
#region Native Win32 Interop
/// <summary>
/// The RECT structure defines the coordinates of the upper-left and lower-right corners of a rectangle.
/// </summary>
[Serializable, StructLayout(LayoutKind.Sequential)]
internal struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
public RECT(int left, int top, int right, int bottom)
{
this.Left = left;
this.Top = top;
this.Right = right;
this.Bottom = bottom;
}
public Rectangle AsRectangle
{
get
{
return new Rectangle(this.Left, this.Top, this.Right - this.Left, this.Bottom - this.Top);
}
}
public static RECT FromXYWH(int x, int y, int width, int height)
{
return new RECT(x, y, x + width, y + height);
}
public static RECT FromRectangle(Rectangle rect)
{
return new RECT(rect.Left, rect.Top, rect.Right, rect.Bottom);
}
}
[System.Security.SuppressUnmanagedCodeSecurity()]
internal sealed class NativeMethods
{
[DllImport("user32.dll")]
internal static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
/// <summary>
/// Get a windows client rectangle in a .NET structure
/// </summary>
/// <param name="hwnd">The window handle to look up</param>
/// <returns>The rectangle</returns>
internal static Rectangle GetClientRect(IntPtr hwnd)
{
RECT rect = new RECT();
GetClientRect(hwnd, out rect);
return rect.AsRectangle;
}
/// <summary>
/// Get a windows rectangle in a .NET structure
/// </summary>
/// <param name="hwnd">The window handle to look up</param>
/// <returns>The rectangle</returns>
internal static Rectangle GetWindowRect(IntPtr hwnd)
{
RECT rect = new RECT();
GetWindowRect(hwnd, out rect);
return rect.AsRectangle;
}
internal static Rectangle GetAbsoluteClientRect(IntPtr hWnd)
{
Rectangle windowRect = NativeMethods.GetWindowRect(hWnd);
Rectangle clientRect = NativeMethods.GetClientRect(hWnd);
// This gives us the width of the left, right and bottom chrome - we can then determine the top height
int chromeWidth = (int)((windowRect.Width - clientRect.Width) / 2);
return new Rectangle(new Point(windowRect.X + chromeWidth, windowRect.Y + (windowRect.Height - clientRect.Height - chromeWidth)), clientRect.Size);
}
}
#endregion
}
Ошибка CS0246 Не удалось найти тип или имя пространства имен "SlimDX" (возможно, отсутствует директива using или ссылка на сборку)
Решение задачи: «Скриншот гаджетов рабочего стола windows 7»
textual
Листинг программы
private void button1_Click(object sender, EventArgs e)
{
using (Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height))
{
using (Graphics graphics = Graphics.FromImage(printscreen as Image))
{
graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);
printscreen.Save(@"screenshot.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}