簡體   English   中英

截屏時拋出Win32異常[c#]

[英]Win32 exception is being thrown while taking a screenshot [c#]

我的應用程序正在使用Graphics.CopyFromScreen(...)函數記錄屏幕(獲取屏幕截圖)。 問題是,有時由於某種未知原因會引發“ ,, Invalid handle win32”異常。該程序在后台運行(這是一個帶有隱藏控制台的控制台應用程序項目)。我一直在尋找Google解決方案,但我在我的情況下沒有發現任何幫助。

我寫的函數:

    static Bitmap takeScreenshot()
    {
        Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                                        Screen.PrimaryScreen.Bounds.Height);
        using (Graphics g = Graphics.FromImage(bmpScreenCapture))
        {
            g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
                                Screen.PrimaryScreen.Bounds.Y,
                                0, 0,
                                bmpScreenCapture.Size,
                                CopyPixelOperation.SourceCopy);
        }
        return bmpScreenCapture;
    }

我剛剛發現該應用程序正在嘗試創建已經存在的文件。 不知何故導致了異常,但我不知道為什么。 我刪除了舊文件,現在程序似乎可以正常工作...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM