{"record":{"id":"360361f329802d8d","repo":"MiniMax-AI/skills","slug":"relationship-oldrelid-does-not-point-to-an-image","errorCode":null,"errorMessage":"Relationship {oldRelId} does not point to an ImagePart.","messagePattern":"Relationship (.+?) does not point to an ImagePart\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Samples/ImageSamples.cs","lineNumber":500,"sourceCode":"    // ── 8. Replace Existing Image ──────────────────────────────────────\n\n    /// <summary>\n    /// Replaces an existing image by updating the ImagePart data behind a\n    /// known relationship ID. The Blip.Embed attribute (rId) stays the same;\n    /// only the binary content changes. This avoids needing to rebuild the\n    /// entire Drawing XML tree.\n    /// </summary>\n    /// <param name=\"mainPart\">The MainDocumentPart containing the image relationship.</param>\n    /// <param name=\"oldRelId\">The existing relationship ID (e.g., \"rId5\") of the image to replace.</param>\n    /// <param name=\"newImagePath\">Path to the replacement image file.</param>\n    public static void ReplaceExistingImage(\n        MainDocumentPart mainPart, string oldRelId, string newImagePath)\n    {\n        // Look up the existing ImagePart by its relationship ID\n        OpenXmlPart part = mainPart.GetPartById(oldRelId);\n        if (part is not ImagePart imagePart)\n        {\n            throw new InvalidOperationException(\n                $\"Relationship {oldRelId} does not point to an ImagePart.\");\n        }\n\n        // Feed new image data into the existing part.\n        // This replaces the binary content while keeping the same rId.\n        using (FileStream stream = new FileStream(newImagePath, FileMode.Open))\n        {\n            imagePart.FeedData(stream);\n        }\n\n        // NOTE: If the new image has different dimensions, you should also\n        // update the Extent.Cx/Cy and A.Extents.Cx/Cy in the Drawing element.\n        // Find all Blip elements referencing this relId:\n        //\n        //   var blips = mainPart.Document.Descendants<A.Blip>()\n        //       .Where(b => b.Embed == oldRelId);\n        //   foreach (var blip in blips)\n        //   {","sourceCodeStart":482,"sourceCodeEnd":518,"githubUrl":"https://github.com/MiniMax-AI/skills/blob/60aaae52bb2af8162732751a4332f62a5fef518b/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Samples/ImageSamples.cs#L482-L518","documentation":"`throw new InvalidOperationException($\"Relationship {oldRelId} does not point to an ImagePart.\")` in `ImageSamples.ReplaceExistingImage`. The method looks up a part by relationship id (`mainPart.GetPartById(oldRelId)`) and checks whether it is an `ImagePart`. If the rId points to a different part type (hyperlink, stylesheet, header, embedded object) the replace cannot proceed because `FeedData` would corrupt unrelated content.","triggerScenarios":"Passing an `oldRelId` that exists but references a non-image relationship — e.g. a hyperlink rId, a styles/header/footer relationship, or a media relationship that is not an `ImagePart`. Also passing a stale rId copied from the wrong element.","commonSituations":"Reading an rId off the wrong XML element (e.g. a `w:hyperlink r:id` instead of an `a:blip r:embed`), an rId from an older version of the document, or assuming every rId in `document.xml` is an image.","solutions":["Extract the rId from the image's `a:blip r:embed` (Drawing) element, not from hyperlinks or other relationships.","Before calling, verify with `mainPart.GetPartById(oldRelId) is ImagePart`.","Enumerate `mainPart.ImageParts` to find the correct rId for the image you intend to replace."],"exampleFix":"// before — rId came from a hyperlink, not the image blip\nImageSamples.ReplaceExistingImage(mainPart, \"rId3\", newPath); // rId3 is a hyperlink\n\n// after — use the blip's r:embed value\n// <a:blip r:embed=\"rId5\"/>\nImageSamples.ReplaceExistingImage(mainPart, \"rId5\", newPath);","handlingStrategy":"type-guard","validationCode":"void ReplaceImage(MainDocumentPart mainPart, string oldRelId, string newPath)\n{\n    if (mainPart.GetPartById(oldRelId) is not ImagePart)\n        throw new ArgumentException($\"{oldRelId} is not an image relationship.\", nameof(oldRelId));\n    ImageSamples.ReplaceExistingImage(mainPart, oldRelId, newPath);\n}","typeGuard":"bool IsImageRelationship(MainDocumentPart mainPart, string relId) =>\n    mainPart.GetPartById(relId) is ImagePart;","tryCatchPattern":"try\n{\n    ImageSamples.ReplaceExistingImage(mainPart, oldRelId, newImagePath);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"ImagePart\"))\n{\n    // find the real image rId from the Drawing's a:blip r:embed\n    var realRid = FindBlipEmbedId(mainPart);\n    ImageSamples.ReplaceExistingImage(mainPart, realRid, newImagePath);\n}","preventionTips":["Always read the rId from the image's `<a:blip r:embed=\"...\">`, never from hyperlinks or other relationships.","Pre-check `mainPart.GetPartById(relId) is ImagePart` before calling.","Enumerate `mainPart.ImageParts` to discover valid image rIds.","Log the part type when the check fails so the wrong source element is obvious."],"tags":["csharp","openxml","docx","images","relationships","validation"],"backgroundTag":null,"analyzedSha":"60aaae52bb2af8162732751a4332f62a5fef518b","analyzedAt":"2026-08-13T17:32:34.717Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}