2016-07-02 28 views
2

コードビハインドを使ってスクリーンショットの機能を実現したいWPFアプリケーションがあります。C#WPFアプリケーションのスクリーンショットを取る

我々はこのために、ユーザーのマシン上で我々のアプリケーションのことテイクスクリーンショット(プリントスクリーン全体ではない)

する必要がありますしたいときはいつでも、私はいくつかのGoogleに行われ、DllImport("user32.dll")はこの点で私を助けることがわかりました。しかし、私はどのようにこれを使用する手掛かりを持っていない?どの方法をここで参照すべきですか?

私はコードの下で試みたが、何のluck-

[DllImport("User32.dll")] 
public static extern int SetForegroundWindow(IntPtr point); 
Process p = Process.GetCurrentProcess(); 
p.WaitForInputIdle(); 
IntPtr h = p.MainWindowHandle; 
SetForegroundWindow(h); 
SendKeys.SendWait("k"); 
IntPtr processFoundWindow = p.MainWindowHandle; 

が示唆ませんしてください。

+0

関連:http://stackoverflow.com/questions/24466482/how-to-take-a-screenshot-of-a-wpf-control –

+0

あなたは上記のコードには必要ありません。ウィンドウを画像として取得するには 'RenderTargetBitmap'を使います。 –

+1

wpfコンテンツ(ウィンドウ内のルートコントロールは何でも)、またはウィンドウのフレームタイトルと右上のボタンだけをキャプチャしますか? –

答えて

1

これは以前のアプリケーションでこれまで使用してきた方法です。

私はスクリーンショット機能を扱うクラスを作成しました。

public sealed class snapshotHandler 
{ 
    [StructLayout(LayoutKind.Sequential)] 
    private struct RECT 
    { 
     public int m_left; 
     public int m_top; 
     public int m_right; 
     public int m_bottom; 
    } 

    [DllImport("user32.dll")] 
    private static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect); 

    public static void Savesnapshot(IntPtr handle_) 
    { 
     RECT windowRect = new RECT(); 
     GetWindowRect(handle_, ref windowRect); 

     Int32 width = windowRect.m_right - windowRect.m_left; 
     Int32 height = windowRect.m_bottom - windowRect.m_top; 
     Point topLeft = new Point(windowRect.m_left, windowRect.m_top); 

     Bitmap b = new Bitmap(width, height); 
     Graphics g = Graphics.FromImage(b); 
     g.CopyFromScreen(topLeft, new Point(0, 0), new Size(width, height)); 
     b.Save(SNAPSHOT_FILENAME, ImageFormat.Jpeg); 
    } 
} 

上記の機能を使用するには、SaveSnapshotメソッドを呼び出します。

SnapshotHandler.SaveSnapshot(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle); 
+0

完璧!ありがとう:) – Rohit

関連する問題