As long as this layer is locked addtional layers will be applied to background. If I open the PSD, unlock background layer additional layer will be added as layers.
I already tried to create a new PSD with coding and import my exported PSD via strem as layer. This will lead to 2 Layers Background which is empty and the imported picture.
How can I unlock background via c# coding to get rid of unnecessary layer?
@BjE
When you open the PSD file you saw the 0 counts of layers, because the default background layer it’s just data in channels of PsdImage, and it does not contain in resources of the PSD file as a layer.
So when you add your first layer, we create a new background layer and add them both.
To get the just background layer you can add one regular layer and then remove it, letting only the new background layer.
Or create a new Layer based on data from PsdImage channels.
See this code example to solve your issue (Uncomment one of the methods to see the result):
string src = "test.psd";
string output = "out_test.psd";
using (var psdImage = (PsdImage)Image.Load(src))
{
// Way 1 - Adds a new regular layer and remove it.
// PseudoUnlockBackgroundLayer(psdImage);
// Way 2 - Creates a layer based on PsdImage data and put it in PsdImage
// CreateBackgroundLayer(psdImage);
// Save PsdImage with unlocked Background Layer
psdImage.Save(output);
}
void PseudoUnlockBackgroundLayer(PsdImage psdImage)
{
if (psdImage.Layers.Length != 0)
{
// Unsupported case.
return;
}
psdImage.AddRegularLayer();
psdImage.Layers = new Layer[] { psdImage.Layers[0] };
}
void CreateBackgroundLayer(PsdImage psdImage)
{
if (psdImage.Layers.Length != 0)
{
// Unsupported case.
return;
}
Layer backgroundLayer = new Layer(psdImage);
backgroundLayer.DisplayName = "Background";
// Add required resource
var layerResources = new List<LayerResource>(backgroundLayer.Resources);
layerResources.Add(new ShmdResource());
backgroundLayer.Resources = layerResources.ToArray();
psdImage.Layers = new Layer[] { backgroundLayer };
}