お世話になります。
.Net4.5です。
System.Diagnostics.Processで指定されている別プロセスのフォームを、枠抜きしたクライアント
領域のみメインのアプリからキャプチャしたいのですが、そういった方法はありますでしょうか。
ご助力ください。
よろしくお願いいたします。
スタック・オーバーフローはプログラマーとプログラミングに熱心な人のためのQ&Aサイトです。すぐ登録できます。
登録してこのコミュニティに参加するまずProcess
のウィンドウハンドルはMainWindowHandle
で取れると思います。ただし対象アプリ次第なので確実ではありません。
対象ウィンドウが求められたらP/InvokeのClientToScreen
でクライアント領域のスクリーン座標を、GetClientRect
でクライアント領域のサイズを求めます。
Point origin = default(Point);
ClientToScreen(process.MainWindowHandle, ref origin);
RECT rect;
GetClientRect(process.MainWindowHandle, out rect);
なお上記の2メソッドはpinvoke.netで定義された宣言を使用しています。
スクリーン上の領域が求められたら、書き込むBitmap
を作成してGraphics.CopyFromScreen
でキャプチャを実行します。
var bmp = new Bitmap(rect.Width, rect.Height);
using (var g = Graphics.FromImage(bmp))
{
g.CopyFromScreen(new Point(origin.X + rect.Left, origin.Y + rect.Top), new Point(0, 0), rect.Size);
}
pinvoke.netから参照しているメンバーの宣言を転記しておきます。
[DllImport("user32.dll")]
static extern bool ClientToScreen(IntPtr hWnd, ref Point lpPoint);
[DllImport("user32.dll")]
static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left, Top, Right, Bottom;
public RECT(int left, int top, int right, int bottom)
{
Left = left;
Top = top;
Right = right;
Bottom = bottom;
}
public RECT(System.Drawing.Rectangle r) : this(r.Left, r.Top, r.Right, r.Bottom) { }
public int X
{
get { return Left; }
set { Right -= (Left - value); Left = value; }
}
public int Y
{
get { return Top; }
set { Bottom -= (Top - value); Top = value; }
}
public int Height
{
get { return Bottom - Top; }
set { Bottom = value + Top; }
}
public int Width
{
get { return Right - Left; }
set { Right = value + Left; }
}
public System.Drawing.Point Location
{
get { return new System.Drawing.Point(Left, Top); }
set { X = value.X; Y = value.Y; }
}
public System.Drawing.Size Size
{
get { return new System.Drawing.Size(Width, Height); }
set { Width = value.Width; Height = value.Height; }
}
public static implicit operator System.Drawing.Rectangle(RECT r)
{
return new System.Drawing.Rectangle(r.Left, r.Top, r.Width, r.Height);
}
public static implicit operator RECT(System.Drawing.Rectangle r)
{
return new RECT(r);
}
public static bool operator ==(RECT r1, RECT r2)
{
return r1.Equals(r2);
}
public static bool operator !=(RECT r1, RECT r2)
{
return !r1.Equals(r2);
}
public bool Equals(RECT r)
{
return r.Left == Left && r.Top == Top && r.Right == Right && r.Bottom == Bottom;
}
public override bool Equals(object obj)
{
if (obj is RECT)
return Equals((RECT)obj);
else if (obj is System.Drawing.Rectangle)
return Equals(new RECT((System.Drawing.Rectangle)obj));
return false;
}
public override int GetHashCode()
{
return ((System.Drawing.Rectangle)this).GetHashCode();
}
public override string ToString()
{
return string.Format(System.Globalization.CultureInfo.CurrentCulture, "{{Left={0},Top={1},Right={2},Bottom={3}}}", Left, Top, Right, Bottom);
}
}
なおコメントで指摘されているP/InvokeにSystem.Drawing.Point
を使うことの是非ですが、Point
にはStructLayoutAttribute属性が明示されていません。しかし、C#コンパイラは値型に対して既定でSequentialレイアウトを行うとされているので、実際にはpinvoke.netで定義されているPOINT型と完全に同一レイアウトになります。なのであえて独自の型を用意する必要はないです。
System.Drawing.Point
ではなくPOINTであり大文字小文字の整合性が取れていません。
StructLayout
が付けられていない=ネイティブレイアウトを考慮していない構造体を偶然の一致により問題なしと判断するのは一般論としては危険に感じます。なお、System.Drawing.Point
に関しては32bit / 64bit共にPOINT
型と一致することは確認しています。ただしクラスライブラリ内部でもこのような使い方はされておらず別途POINT型を定義しそちらを使っています。
ClientToScreen
とGetClientRect
でクライアント領域を取得するサンプルアプリケーションを組んでみました。
コードの内容はpgrhoさんの回答とほとんどかぶっていますが、サンプルアプリケーションが問題なく動作すれば、それを足掛かりにエラーの原因箇所が特定できるかもしれません。
RECT
とPOINT
構造体にはStructLayout
を付けています。ClientCapture.cs
のGetBitmap
メソッドはアプリケーション開発時のUIテストに使った実績があります。(Windows7 32bitで使用) Graphics#CopyFromScreen
を採用した理由は以下のとおりです。
以下はサンプルアプリケーションのコード
参照設定にSystem.Drawing
を追加する必要があります。
ClientCapture.cs
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Media.Imaging;
namespace WpfApplication1
{
public class ClientCapture
{
//クライアント領域キャプチャ用のメソッドと、戻り値用の構造体
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[StructLayout(LayoutKind.Sequential)]
private struct POINT
{
public int x;
public int y;
}
[DllImport("user32.dll")]
private static extern int GetClientRect(IntPtr hwnd, out RECT lpRect);
[DllImport("user32.dll")]
private static extern bool ClientToScreen(IntPtr hwnd, out POINT lpPoint);
/// <summary>
/// 画面をキャプチャしてBitmapを返す。
/// </summary>
/// <param name="handle"></param>
/// <returns>クライアント領域のBitmap。キャプチャに失敗した場合null。</returns>
public static Bitmap GetBitmap(IntPtr handle)
{
var rect = new RECT();
if (GetClientRect(handle, out rect) == 0)
{
//キャプチャ失敗
return null;
}
var size = new Size(rect.right - rect.left, rect.bottom - rect.top);
if (size.Width <= 0 || size.Height <= 0)
{
//キャプチャ失敗
return null;
}
var img = new Bitmap(size.Width, size.Height);
var pt = new POINT { x = rect.left, y = rect.top };
ClientToScreen(handle, out pt);
using (var g = Graphics.FromImage(img))
{
g.CopyFromScreen(pt.x, pt.y, 0, 0, img.Size);
}
return img;
}
/// <summary>
/// 画面をキャプチャしてBitmapImageを返す。
/// </summary>
/// <param name="handle"></param>
/// <returns>クライアント領域のBitmapImage。キャプチャに失敗した場合null。</returns>
public static BitmapImage GetBitmapImage(IntPtr handle)
{
var bitmap = GetBitmap(handle);
if(bitmap == null)
{
//キャプチャ失敗
return null;
}
//変換処理
//see http://stackoverflow.com/a/1069509
using (var memory = new MemoryStream())
{
bitmap.Save(memory, ImageFormat.Png);
memory.Position = 0;
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
return bitmapImage;
}
}
}
}
MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="{Binding WindowTitle}" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="{Binding ExePathCaption}"/>
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding ExePath}"/>
<Button Grid.Row="0" Grid.Column="2" Content="{Binding RunExeCaption}" Command="{Binding Path=RunExeCommand}" Padding="10,0"/>
<Image Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" Name="ImageView" Source="{Binding CapturedImage}" Stretch="None"/>
<Label Grid.Row="2" Grid.Column="0" Content="{Binding RecCaption}" Foreground="DarkRed" Visibility="{Binding RecVisibility}"/>
</Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Timers;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Imaging;
namespace WpfApplication1
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window, ICommand, INotifyPropertyChanged
{
const double REC_INTERVAL = 1000d;
#region xaml とのバインディング関連プロパティ
public string WindowTitle { get { return "1秒ごとにキャプチャするサンプル"; } }
public string ExePathCaption { get { return "exeファイルのパス"; } }
public string RunExeCaption { get { return "起動"; } }
public string RecCaption { get { return "●Rec"; } }
public string ExePath { get; set; }
public Visibility RecVisibility { get; private set; }
public BitmapImage CapturedImage { get; private set; }
#endregion
private Timer CaptureTimer;
private Process ExeProcess;
#region キャプチャに関係のない処理
public MainWindow()
{
InitializeComponent();
ExePath = "notepad";
SetRecVisible(false);
DataContext = this;
CaptureTimer = new Timer(REC_INTERVAL);
Closed += MainWindow_Closed;
}
private void SetRecVisible(bool isVisible)
{
RecVisibility = isVisible ? Visibility.Visible : Visibility.Collapsed;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("RecVisibility"));
}
}
private void MainWindow_Closed(object sender, EventArgs e)
{
CaptureTimer.Dispose();
if (ExeProcess != null)
{
ExeProcess.Dispose();
}
}
#endregion
public ICommand RunExeCommand { get { return this; } }
public bool CanExecute(object parameter) { return true; }
public event EventHandler CanExecuteChanged;
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// ボタンを押したときの処理。プロセス実行→クライアント領域を一定間隔でキャプチャする。
/// </summary>
/// <param name="parameter"></param>
/// <remarks>エラー回避してないので注意すること</remarks>
public void Execute(object parameter)
{
ExeProcess = Process.Start(ExePath);
CaptureTimer.Elapsed += Timer_Elapsed;
CaptureTimer.Start();
SetRecVisible(true);
}
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (ExeProcess == null || ExeProcess.HasExited)
{
//プロセスがなくなったらタイマーも停止する
CaptureTimer.Stop();
SetRecVisible(false);
return;
}
//非同期処理なのでメインスレッドでキャプチャ処理
Dispatcher.BeginInvoke(new Action(SetImage), null);
}
private void SetImage()
{
//クライアント領域をキャプチャ
CapturedImage = ClientCapture.GetBitmapImage(ExeProcess.MainWindowHandle);
if (PropertyChanged != null)
{
//変更を通知して再描画
PropertyChanged(this, new PropertyChangedEventArgs("CapturedImage"));
}
}
}
}