-2

WPF アプリケーションで特定のウィンドウをキャプチャするためのコードを誰かに提供してもらえますか? Windows フォーム アプリケーションに実装しましたが、WPF では面倒です。

4

1 に答える 1

1

ウィンドウの画像を保存したい場合は、RenderTargetBitmap オブジェクトを作成してウィンドウをレンダリングできます。

/// <summary>
/// Captures a controls visual and saves it to the file system.
/// </summary>
/// <param name="visual">A reference to the <see cref="Visual"/> that you want to capture.</param>
/// <param name="fileName">The file name that you want the image saved as.</param>
void CaptureImage(Visual visual, string fileName)
{
    if (File.Exists(fileName))
        File.Delete(fileName);

    using (FileStream fileStream = new FileStream(fileName, FileMode.CreateNew))
    {
        RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap((int)Width, (int)Height, 96, 96, PixelFormats.Pbgra32);
        renderTargetBitmap.Render(this);
        var encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
        encoder.Save(fileStream);
    }
}

これはラインで使用されます。

CaptureImage(this, @"c:\temp\screencap.png");
于 2013-09-11T14:43:46.350 に答える