Try - Catch 块中未处理的异常

发布于 2024-12-09 11:13:01 字数 1250 浏览 0 评论 0原文

我已经查看了与此问题相关的其他问题,但我没有看到对我有帮助的答案(或者可能更多到我理解的程度)。

此代码:

   public static Bitmap GetLibraryObjectImage(Guid guid) {
            try {
                string tempPath = GetLibraryObjectImagePath(guid);
                if (tempPath != string.Empty) {
                    var bytes = File.ReadAllBytes(tempPath);
                    var ms = new MemoryStream(bytes);
                    return (Bitmap)Image.FromStream(ms);
                }
            }
            catch {
                return (Bitmap)Image.FromFile(Application.StartupPath + @"\na.bmp");
            }

            return (Bitmap)Image.FromFile(Application.StartupPath + @"\na.bmp");
        }

旨在处理找不到图像文件或以其他方式无效的任何情况。我还没有确定任何特定的异常类型,希望它能捕获任何内容。

对于我的一位用户,它抛出了这个异常:

参数无效。
在 System.Drawing.Image.FromStream(Stream 流、布尔值 useEmbeddedColorManagement、布尔值 validateImageData) 在 System.Drawing.Image.FromStream(Stream 流)
在 ScruffyDuck.AirportDesignEditor.Helpers.U.GetLibraryObjectImage(Guid 指南)

我之前在处理图像的 try-catch 块的其他情况下见过这种情况。我认为上面的 try-catch 会得到一切,包括非托管异常,但也许我错了。

我不知道导致异常的情况,但我真的不希望我的应用程序崩溃并烧毁,即使我试图解决问题。它被我的全局异常处理程序捕获,但当然那时已经有点晚了。

非常感谢您对避免这种情况的任何见解

I have looked at the other questions related to this problem but I do not see answer that helps me (or perhaps more to the point that I understand).

This code:

   public static Bitmap GetLibraryObjectImage(Guid guid) {
            try {
                string tempPath = GetLibraryObjectImagePath(guid);
                if (tempPath != string.Empty) {
                    var bytes = File.ReadAllBytes(tempPath);
                    var ms = new MemoryStream(bytes);
                    return (Bitmap)Image.FromStream(ms);
                }
            }
            catch {
                return (Bitmap)Image.FromFile(Application.StartupPath + @"\na.bmp");
            }

            return (Bitmap)Image.FromFile(Application.StartupPath + @"\na.bmp");
        }

is intended to deal with any situation in which the image file is not found or is invalid in some other way. I have not identified any specific exception type in the hope that it will catch anything.

For one of my users it threw this exception:

Invalid Parameter.
At System.Drawing.Image.FromStream(Stream
stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
At System.Drawing.Image.FromStream(Stream stream)
At ScruffyDuck.AirportDesignEditor.Helpers.U.GetLibraryObjectImage(Guid
guid)

I have seen this before in other situations in try-catch blocks where handling images. I thought that the above try-catch would get everything including unmanaged exceptions but perhaps I am wrong.

I have no idea regarding the circumstances causing the exception but I really don't want my app to crash and burn even if I am trying to manage the problems. It was caught by my global exception handler but of course by then it is a bit late.

Many thanks for any insight into avoiding this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

長街聽風 2024-12-16 11:13:01

正如 @KeithS 在评论中提到的,堆栈跟踪显示异常源自 Image.FromStream 方法。您是否已验证导致该调用的所有代码是否正确返回预期数据?文件路径必须存在(否则 File.ReadAllBytes 会引发异常),并且 MemoryStream 构造函数返回有效流(否则会引发异常),但是有没有可能内存流中的数据实际上不是图像?

另外,您可能想尝试使用 File.OpenRead 方法。这会返回一个 FileStream,因此您可以将其直接传递到 Image.FromStream 方法。这看起来类似于以下代码。 (我将其更改为使用 using 语句,并且只有一个 return 语句。)

public static Bitmap GetLibraryObjectImage(Guid guid) 
{ 
   Bitmap bitmap = null;

   try
   { 
      string tempPath = GetLibraryObjectImagePath(guid); 
      if (!String.IsNullOrEmpty(tempPath) 
      {
         using (var stream = File.OpenRead(tempPath))
         {
            bitmap = (Bitmap)Image.FromStream(stream);
         }
      } 
   } 
   catch
   { 
      bitmap = (Bitmap)Image.FromFile(Application.StartupPath + @"\na.bmp"); 
   }

   if (bitmap == null)
   {
      bitmap = (Bitmap)Image.FromFile(Application.StartupPath + @"\na.bmp"); 
   }

   return bitmap;
} 

As @KeithS mentioned in the comments, the stack trace is showing that the exception is originating in the Image.FromStream method. Have you verified that all of the code leading up to that call is correctly returning the expected data? The file path must exist (or the File.ReadAllBytes would have thrown an exception) and the MemoryStream constructor is returning a valid stream (or it would have thrown an exception) but is it possible that the data in the memory stream isn't actually an image?

Also, you might want to try using the File.OpenRead method instead. This returns a FileStream so you can pass that directly in to the Image.FromStream method. This would look similar to the following code. (I changed it some to use a using statement and only have a single return statement.)

public static Bitmap GetLibraryObjectImage(Guid guid) 
{ 
   Bitmap bitmap = null;

   try
   { 
      string tempPath = GetLibraryObjectImagePath(guid); 
      if (!String.IsNullOrEmpty(tempPath) 
      {
         using (var stream = File.OpenRead(tempPath))
         {
            bitmap = (Bitmap)Image.FromStream(stream);
         }
      } 
   } 
   catch
   { 
      bitmap = (Bitmap)Image.FromFile(Application.StartupPath + @"\na.bmp"); 
   }

   if (bitmap == null)
   {
      bitmap = (Bitmap)Image.FromFile(Application.StartupPath + @"\na.bmp"); 
   }

   return bitmap;
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文