public static Document Load(System.IO.Stream input) { // Load and decompress Photoshop file structures var loadContext = new DocumentLoadContext(); var psdFile = new PsdFile(input, loadContext); // Multichannel images are loaded by processing each channel as a // grayscale layer. if (psdFile.ColorMode == PsdColorMode.Multichannel) { CreateLayersFromChannels(psdFile); psdFile.ColorMode = PsdColorMode.Grayscale; } // Convert into Paint.NET internal representation var document = new Document(psdFile.ColumnCount, psdFile.RowCount); if (psdFile.Layers.Count == 0) { psdFile.BaseLayer.CreateMissingChannels(); var layer = PDNWrapper.Layer.CreateBackgroundLayer(psdFile.ColumnCount, psdFile.RowCount); ImageDecoderPdn.DecodeImage(layer, psdFile.BaseLayer); layer.Name = String.IsNullOrEmpty(psdFile.BaseLayer.Name)? "Background" : psdFile.BaseLayer.Name; layer.Opacity = psdFile.BaseLayer.Opacity; layer.Visible = psdFile.BaseLayer.Visible; layer.IsGroup = psdFile.BaseLayer.IsGroup; layer.LayerID = psdFile.BaseLayer.LayerID; layer.BlendMode = BlendModeMapping.FromPsdBlendMode(psdFile.BaseLayer.BlendModeKey); document.Layers.Add(layer); } else { psdFile.VerifyLayerSections(); ApplyLayerSections(psdFile.Layers); //var pdnLayers = psdFile.Layers.AsParallel().AsOrdered() // .Select(psdLayer => psdLayer.DecodeToPdnLayer()) // .ToList(); //document.Layers.AddRange(pdnLayers); /* * foreach (var l in psdFile.Layers) * { * document.Layers.Add(l.DecodeToPdnLayer()); * } */ BitmapLayer parent = null; JobHandle jobHandle = default(JobHandle); foreach (var l in Enumerable.Reverse(psdFile.Layers)) { if (l.IsEndGroupMarker) { parent = parent != null ? parent.ParentLayer : null; continue; } BitmapLayer b = null; jobHandle = l.DecodeToPdnLayer(jobHandle, out b); b.ParentLayer = parent; if (parent != null) { parent.ChildLayer.Add(b); } else { document.Layers.Add(b); } if (b.IsGroup) { parent = b; } } jobHandle.Complete(); } SetPdnResolutionInfo(psdFile, document); psdFile.Cleanup(); return(document); }