How to clone PSDImage

Hello Team,
What is the best way to clone (Deep Copy) large PSD images in Aspose library.
Can you please share the sample code for the same.

@merlinwand

Can you please elaborate more about it so that I can assist you better in this regard.

As per our requirement, we need to perform various operations and generate multiple PSD files from a single PSD file.
To optimize this operation we want to eliminate the operation of loading of PSD file (Main file) from disk multiple times, instead, we want to load it once and cache it as a Mastercopy in Application cache provider.
So that I can create multiple local working copies of the cached MasterCopy file and perform the operation on that local copy.

@merlinwand

You can consider saving the loaded PSD to different file name and it will be all together a different file as compared to a source file. Will that work for you?

Can you please elaborate more or share sample code.

@merlinwand

You can consider opening a PSD and saving that as PSD right away or you can create or load an existing PSD and then add PSD as layer inside that.

We are mainly dealing with Text layers in our operations on PSD. I cant create a copy of Text layer for operations from mastercopy.
Why i need to create a copy from master copy only: I want to preserve the Text properties such as fonts colors, positions and everything except replacing some word with input that i have received from user.
Is there any way to create a new TextLayer which is same replica of original TextLayer so that i can maitain the text properties.

private TextLayer cloneTextLayer(TextLayer textLayer) {
byte[] layerBytes;
MemoryStream layerMem = new MemoryStream();
OutputStream dstStream = layerMem.toOutputStream();
textLayer.save(dstStream, new PsdOptions());
layerBytes = layerMem.toArray();
layerMem = new MemoryStream(layerBytes);
return (TextLayer)new Layer(layerMem.toInputStream());
}

The above code is giving ClassCast Exception Can not cast from Layer to TextLayer

@merlinwand

Actually, earlier you mentioned about PSD. You can please visit the following thread link to see how to copy one layer from PSD and moving to other.

Yes absolutely correct, As I mentioned earlier that there is no way to do a deep copy of PSD,
As per your suggestion, we are creating a blank PSD and adding a layer in that, But with reference to the previous comment we need to create a new PSD by adding a layer of type text from the Mastercopy PSD file, Only here catch is I want to update the original copy of Layer (Change Text and maintain rest of all the other properties of TextLayer) before adding to newly created PSD file.
But with the above code, I can create a generic Layer type cant cast to TextLayer.

@merlinwand

Can you please provide the sample code that you have used along with source file and what you desire.

The layer is base class and you can type cast to respective specific type like TextLayer too.

var txtLayer = (TextLayer)psdImage.Layers[1];

If I follow the above line then it will not create a copy (Deep Copy) of layer.
var txtLayer = (TextLayer)psdImage.Layers[1];
after this statement if i change the text content of layer “txtLayer” then it will do the changes in text Layer psdImage.Layers[1] as well. Which cant be used as a master copy for next iteration.
I need to do the deep copy of Layer Object instead copy of Layer object reference.

To Perform Deep copy if i follow below code as per article pointed previously its not letting to do the Type casting of generic layer to Text Layer.

private TextLayer cloneTextLayer(TextLayer textLayer) {
byte[] layerBytes;
MemoryStream layerMem = new MemoryStream();
OutputStream dstStream = layerMem.toOutputStream();
textLayer.save(dstStream, new PsdOptions());
layerBytes = layerMem.toArray();
layerMem = new MemoryStream(layerBytes);
return (TextLayer)new Layer(layerMem.toInputStream());
}

@merlinwand

We are investigating the requirements further on our end and will be able to share further feedback with you as soon as possible in this regard and request for your patience.