屏幕截圖上的窗口沒有消失 (Window not disappearing on Screenshot)


問題描述

屏幕截圖上的窗口沒有消失 (Window not disappearing on Screenshot)

我想在我按下窗口上的按鈕時截取屏幕截圖。問題是,在截屏之前窗口會消失。

這是我的代碼:

MainWindow:

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

ScreenCapture(顯示屏幕截圖)

p>
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;
}

有什麼建議嗎?


參考解法

方法 1:

use this instead of this.Close();

this.Hide();

(by FKutscheNoxious Reptile)

參考文件

  1. Window not disappearing on Screenshot (CC BY‑SA 2.5/3.0/4.0)

#screenshot #wpf #image #.net #C#






相關問題

沒有 iPhone,如何使用 XCode 截屏? (Without iPhone, how to take screenshot with XCode?)

mapViewDidFinishLoadingMap:調用過早 (mapViewDidFinishLoadingMap: called too early)

屏幕截圖上的窗口沒有消失 (Window not disappearing on Screenshot)

Appium 中用於混合應用程序的多個屏幕截圖 (Multiple Screenshots in Appium for Hybrid Apps)

在Linux上通過Python腳本拍攝屏幕截圖 (Take a screenshot via a Python script on Linux)

如何使用服務器端腳本生成網頁的屏幕截圖? (How can I generate a screenshot of a webpage using a server-side script?)

如何使用 ruby 和 unix 服務器截取網頁截圖? (How do I take screenshots of web pages using ruby and a unix server?)

使用 jquery 生成網站的屏幕截圖 (generating a screenshot of a website using jquery)

畫面區域的選擇 (Selection of screen area)

使用 PHP 的網頁截圖? (Web Page Screenshots with PHP?)

iPhone:如何在使用 3d 圖層轉換時以編程方式拍攝屏幕截圖 (iPhone: how to take screen shots programmatically when using 3d layer transformations)

防止 VideoView Activity 中的視頻屏幕截圖 - Android (Prevent Video screen capture in VideoView Activity - Android)







留言討論