簡體   English   中英

為什么我得到Win32Exception:操作成功完成?

[英]Why im getting Win32Exception: The operation completed successfully?

這是我的代碼,只是截取了整個屏幕的屏幕截圖。 在工作了一段時間之后,它引發了異常。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace Capture_ScreenShots
{
    public partial class Form1 : Form
    {
        string bfbc;
        string save_location;
        int save_image;
        int screenWidth;
        int screenHeight;

        public Form1()
        {
            InitializeComponent();
            save_location = @"c:\screenshots\sc1\";
            timer1.Enabled = true;
            timer1.Interval = 200;
            save_image = 0;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            save_image = save_image + 1;
            bfbc = save_location  + save_image.ToString("D3") + ".jpg";

            screenWidth = Screen.GetBounds(new Point(0, 0)).Width;
            screenHeight = Screen.GetBounds(new Point(0, 0)).Height;
            Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight);
            Graphics gfx = Graphics.FromImage((Image)bmpScreenShot);
            gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));
            bmpScreenShot.Save(bfbc, ImageFormat.Jpeg);
        }
    }
}

唯一的例外是:

gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));

這是異常消息:

System.ComponentModel.Win32Exception was unhandled
  HResult=-2147467259
  Message=The operation completed successfully
  Source=System.Drawing
  ErrorCode=-2147467259
  NativeErrorCode=0
  StackTrace:
       at System.Drawing.Graphics.CopyFromScreen(Int32 sourceX, Int32 sourceY, Int32 destinationX, Int32 destinationY, Size blockRegionSize, CopyPixelOperation copyPixelOperation)
       at System.Drawing.Graphics.CopyFromScreen(Int32 sourceX, Int32 sourceY, Int32 destinationX, Int32 destinationY, Size blockRegionSize)
       at Capture_ScreenShots.Form1.timer1_Tick(Object sender, EventArgs e) in d:\C-Sharp\Capture_ScreenShots\Capture_ScreenShots\Capture_ScreenShots\Form1.cs:line 44
       at System.Windows.Forms.Timer.OnTick(EventArgs e)
       at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at Capture_ScreenShots.Program.Main() in d:\C-Sharp\Capture_ScreenShots\Capture_ScreenShots\Capture_ScreenShots\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:

不知道為什么會發生異常。 它工作正常,但僅在很短的時間內。

BitMapGraphics實現IDisposible接口。

您不必在方法結束時處理對象。 using構造提供了一種簡便的方法。

替換最后四行:

        Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight);
        Graphics gfx = Graphics.FromImage((Image)bmpScreenShot);
        gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));
        bmpScreenShot.Save(bfbc, ImageFormat.Jpeg);

帶有:

        using (Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight))
        using (Graphics gfx = Graphics.FromImage((Image)bmpScreenShot))
        {
            gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));
            bmpScreenShot.Save(bfbc, ImageFormat.Jpeg);
        }

using構造確保了這些類使用的系統資源是免費的,因此您不會遇到沖突。

該消息來自NativeErrorCode == 0 ,這實際上根本沒有錯誤。

要查看實際失敗,您必須查看HResult字段,該字段為0x80004005。 不幸的是,這是一個非常普通的失敗。

非零的HResult和零的NativeErrorCode的組合表示COM組件發生故障,該組件直接返回HRESULT,而無需使用SetLastError()。

您唯一的其他線索是發生異常的位置。 在圖形處理的上下文中,肯定是GDI +調用失敗了,因為GDI +是基於COM的,而GDI不是。

暫無
暫無

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

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