首页 > 解决方案 > 为什么我以随机频率收到此“GDI+ 中发生一般错误。在 System.Drawing.Image.Save”?

问题描述

这是我的基本代码:

#region privateMethods
private bool SaveImageFile(string absolutePathFile, byte[] file, ImageFormat format)
{
    try
    {
        string directoryPath = Path.GetDirectoryName(absolutePathFile);
        if (!Directory.Exists(directoryPath)) Directory.CreateDirectory(directoryPath);
        if (File.Exists(absolutePathFile)) File.Delete(absolutePathFile);

        using (MemoryStream ms = new MemoryStream(file))
        using (var bmp = Image.FromStream(ms))     
            bmp.Save(absolutePathFile, format);

        return true;

    }
    catch (ArgumentException argEx)
    {
        EventLogger.WriteLog("UploadImageFile: This file it isn't an image " + argEx.Message, System.Diagnostics.EventLogEntryType.Error);
    }
    catch (Exception ex)
    {
        EventLogger.WriteLog("UploadImageFile: " + ex.Message + " " + ex.StackTrace + "PathFile: " + absolutePathFile, System.Diagnostics.EventLogEntryType.Error);
    }
    return false;
}

这似乎很基本。哪个可能是原因?或者我怎样才能更好地调查?

它似乎并不总是发生,而是“有时”发生。这真的很奇怪,并且在控制之下,发生的随机性......

标签: c#imagefilegdi+

解决方案


推荐阅读