【问题标题】:Window not disappearing on Screenshot屏幕截图上的窗口没有消失
【发布时间】:2015-11-18 10:43:25
【问题描述】:

我想在我按下窗口上的按钮时截取屏幕截图。问题是,在截屏之前窗口会消失。

这是我的代码:

主窗口:

private void OnButtonScreenshotClick(object sender, RoutedEventArgs routedEventArgs)
{
    this.Visibility = Visibility.Hidden;
    ScreenCapture capture = new ScreenCapture();
    this.Close();
    capture.Show();      
}

ScreenCapture(显示屏幕截图)

public ScreenCapture()
{
    InitializeComponent();
    this.ScreenCaptureImage.Source = Screenshot.TakeFullScreenshot(width, height);
}

如您所见,我尝试在初始化新的 ScreenCapture-Window 之前隐藏窗口。我也尝试过延迟 Thread.Sleep,但效果不佳。

这是我的截图代码:

public static ImageSource TakeFullScreenshot(int width, int height)
{
   Image bitmap = new Bitmap(width, height);
    {
        using (Graphics graphics = Graphics.FromImage(bitmap))
        {
            graphics.CopyFromScreen(new Point(0, 0), new Point(0, 0), bitmap.Size);
        }
    }
    return GetImageStream(bitmap);
}

private static BitmapSource GetImageStream(Image myImage)
{
    var bitmap = new Bitmap(myImage);
    IntPtr bmpPt = bitmap.GetHbitmap();
    BitmapSource bitmapSource =
    System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
          bmpPt,
          IntPtr.Zero,
          Int32Rect.Empty,
          BitmapSizeOptions.FromEmptyOptions());
    //freeze bitmapSource and clear memory to avoid memory leaks
    //bitmapSource.Freeze();
    //DeleteObject(bmpPt);
    return bitmapSource;
}

有什么建议吗?

【问题讨论】:

    标签: c# .net wpf image screenshot


    【解决方案1】:

    用 this 代替 this.Close();

    this.Hide();
    

    【讨论】:

    • 好的,这很简单。谢谢!我会尽快接受你的回答。
    猜你喜欢
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    相关资源
    最近更新 更多