截屏WPF控件在Win8中不工作

截屏WPF控件在Win8中不工作,wpf,windows-8,controls,Wpf,Windows 8,Controls,我有一个功能可以捕获WPF控件的屏幕截图: public System.Drawing.Bitmap CaptureScreenshot() { Point p = this.PointToScreen(new Point(0, 0)); System.Drawing.Rectangle rc = new System.Drawing.Rectangle((int)p.X, (int)p.Y, (int)this.ActualWidth, (int

我有一个功能可以捕获WPF控件的屏幕截图:

    public System.Drawing.Bitmap CaptureScreenshot()
    {
        Point p = this.PointToScreen(new Point(0, 0));
        System.Drawing.Rectangle rc = new System.Drawing.Rectangle((int)p.X, (int)p.Y, (int)this.ActualWidth, (int)this.ActualHeight);
        System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(rc.Width, rc.Height);
        using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
        {
            g.CopyFromScreen(rc.Location, System.Drawing.Point.Empty, rc.Size);
        }
        return bitmap;
    }
这在Win7中运行良好。但是,在Win8中,如果从上下文菜单调用此函数,捕获的屏幕截图也会显示上下文菜单。它不会等到我的上下文菜单消失后再截图

为什么它在Win7中工作而不是在Win8中工作?在截图之前,如何等待上下文菜单消失?我尝试了几种方法,如
DoEvents
UpdateLayout()
,但它们似乎没有帮助

更新: 根据pushpraj的评论,我使用RenderTargetBitmap使其工作:

        var bitmap = new RenderTargetBitmap((int)grid.ActualWidth, (int)grid.ActualHeight, 96, 96, PixelFormats.Default);
        bitmap.Render(grid);
        Clipboard.SetImage(bitmap);
除了一个问题外,它似乎在起作用。我的控件有一个浅色背景,当我粘贴剪贴板时,背景会变成全黑色。如果直接将位图保存到文件中,则不会出现此问题。怎么了

更新2:
好的,对于使用
RenderTargetBitmap
的背景问题,我在找到了一个修复方法。

尝试在位图上渲染相同的内容,而不是从屏幕上捕获。感谢您的提示。但是,我遇到了另一个问题,控件的灯光背景变黑,请参见上面我的更新问题。您能否发布一些示例xaml,我可以试一试。我现在得到了修复,请参见上面我的更新问题。非常感谢你!