{
  "id": "dotnet/gdiplus-out-of-memory",
  "signature": "System.OutOfMemoryException: Out of memory. at System.Drawing.Graphics.FromImage(Image image)",
  "signature_zh": "System.OutOfMemoryException: 内存不足。在 System.Drawing.Graphics.FromImage(Image image)",
  "regex": "System\\.OutOfMemoryException: Out of memory\\. at System\\.Drawing\\.Graphics\\.FromImage",
  "domain": "dotnet",
  "category": "resource_error",
  "subcategory": null,
  "root_cause": "GDI+ throws OutOfMemoryException when creating a Graphics object from an Image with invalid pixel format or dimensions exceeding 2^16 pixels, often due to corrupt image files or oversized bitmaps.",
  "root_cause_type": "generic",
  "root_cause_zh": "GDI+ 在从像素格式无效或尺寸超过 2^16 像素的 Image 创建 Graphics 对象时抛出 OutOfMemoryException，通常是由于图像文件损坏或位图过大。",
  "versions": [
    {
      "version": ".NET 6.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": ".NET 7.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": ".NET 8.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": ".NET Framework 4.8",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Increase GC memory limit via gcAllowVeryLargeObjects or -gcremove",
      "why_fails": "The error is not about managed memory; GDI+ unmanaged memory is limited by OS and pixel format, not GC.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    },
    {
      "action": "Wrap in try-catch and retry with Thread.Sleep",
      "why_fails": "Retrying does not fix the underlying corrupt image or invalid pixel format; the same error will recur.",
      "fail_rate": 0.98,
      "condition": "",
      "sources": []
    },
    {
      "action": "Set Image.FromFile to use a larger buffer",
      "why_fails": "The error occurs during Graphics creation, not file loading; buffer size is irrelevant.",
      "fail_rate": 0.99,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Validate image dimensions before creating Graphics: if (image.Width > 65535 || image.Height > 65535) throw new InvalidOperationException(\"Image too large\");",
      "success_rate": 0.85,
      "how": "Validate image dimensions before creating Graphics: if (image.Width > 65535 || image.Height > 65535) throw new InvalidOperationException(\"Image too large\");",
      "condition": "",
      "sources": []
    },
    {
      "action": "Re-save the image with a valid pixel format using Image.Save with a fixed format like ImageFormat.Png before drawing.",
      "success_rate": 0.75,
      "how": "Re-save the image with a valid pixel format using Image.Save with a fixed format like ImageFormat.Png before drawing.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use SkiaSharp or ImageSharp instead of System.Drawing.Common for cross-platform image processing.",
      "success_rate": 0.95,
      "how": "Use SkiaSharp or ImageSharp instead of System.Drawing.Common for cross-platform image processing.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "在创建 Graphics 前验证图像尺寸：if (image.Width > 65535 || image.Height > 65535) throw new InvalidOperationException(\"图像过大\");",
    "使用 Image.Save 以固定格式（如 ImageFormat.Png）重新保存图像，确保有效像素格式。",
    "改用 SkiaSharp 或 ImageSharp 替代 System.Drawing.Common 进行跨平台图像处理。"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://learn.microsoft.com/en-us/dotnet/api/system.drawing.graphics.fromimage",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.85,
  "fix_success_rate": 0.78,
  "resolvable": "partial",
  "first_seen": "2023-04-12",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}