{"record":{"id":"e59668c244e2c353","repo":"dotnet/wpf","slug":"packagepart-already-has-associated-thumbnail","errorCode":null,"errorMessage":"PackagePart already has associated Thumbnail.","messagePattern":"PackagePart already has associated Thumbnail\\.","errorType":"exception","errorClass":"XpsPackagingException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsManager.cs","lineNumber":720,"sourceCode":"            //\n            PackagePart propertiesPart = GetDocumentPropertiesPart();\n\n            if( propertiesPart == null )\n            {\n                propertiesPart = GenerateUniquePart(XpsS0Markup.CoreDocumentPropertiesType );\n                _metroPackage.CreateRelationship(propertiesPart.Uri, TargetMode.Internal, XpsS0Markup.CorePropertiesRelationshipType );\n            }\n            return propertiesPart;\n        }\n\n        public\n        XpsThumbnail\n        AddThumbnail(XpsImageType imageType, INode parent, XpsThumbnail oldThumbnail )\n        {\n            XpsThumbnail newThumbnail = null;\n            if( oldThumbnail != null )\n            {\n                throw new XpsPackagingException(SR.ReachPackaging_AlreadyHasThumbnail);\n            }    \n            if( !( imageType == XpsImageType.JpegImageType ||\n                    imageType == XpsImageType.PngImageType ) )\n            {\n                throw new XpsPackagingException(SR.ReachPackaging_UnsupportedThumbnailImageType);\n            }   \n            newThumbnail = new XpsThumbnail(this,\n                                        parent,\n                                        GenerateUniquePart(ImageTypeToString(imageType))\n                                        );\n            return newThumbnail;\n        }\n\n        public\n        XpsThumbnail\n        EnsureThumbnail( INode parent, PackagePart part )\n        {\n            XpsThumbnail thumbnail = null;","sourceCodeStart":702,"sourceCodeEnd":738,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsManager.cs#L702-L738","documentation":"XpsManager.AddThumbnail throws this XpsPackagingException when an XPS package part already has an associated thumbnail. The XPS specification allows at most one thumbnail part per package or FixedDocumentSequence/FixedDocument element, so attempting to add a second thumbnail (via AddThumbnail with a non-null oldThumbnail) is rejected.","triggerScenarios":"Calling AddThumbnail(imageType, parent, oldThumbnail) where oldThumbnail is not null — i.e. the parent node (package, document sequence, or document) already owns an XpsThumbnail and the caller is trying to replace/add another instead of committing and clearing the old one.","commonSituations":"Re-serializing an existing XPS document that already contains a thumbnail without first removing or consuming the old thumbnail; calling AddThumbnail twice in a custom XPS writing pipeline; resuming an interrupted save where the thumbnail was already created.","solutions":["Remove or delete the existing thumbnail before adding a new one (e.g. delete the old XpsThumbnail part from the package).","Only pass null as oldThumbnail when creating a brand-new thumbnail for a parent that has none.","Check whether the parent already has a thumbnail and skip AddThumbnail if so.","If re-writing a package, create a fresh XpsDocument/package instead of adding thumbnails to a pre-existing one."],"exampleFix":"// before\nXpsThumbnail thumb = manager.AddThumbnail(XpsImageType.PngImageType, node, existingThumb);\n// after\nif (existingThumb == null)\n{\n    XpsThumbnail thumb = manager.AddThumbnail(XpsImageType.PngImageType, node, null);\n}\nelse\n{\n    // keep or replace the existing thumbnail part first\n}","handlingStrategy":"validation","validationCode":"if (node != null && nodeThumbnail != null) { /* thumbnail already exists: skip or replace */ }","typeGuard":null,"tryCatchPattern":"catch (XpsPackagingException ex) { /* handle: thumbnail already present */ }","preventionTips":["Track per-node thumbnail state in your writer pipeline.","Never pass a non-null oldThumbnail when creating a new thumbnail.","Treat thumbnail creation as once-per-node."],"tags":["xps","packaging","duplicate-thumbnail","wpf"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}