Free Support Forum - aspose.com

Replace TIFF pages (frames)


#1

For example, we have mulitpaged TIFF file: input.zip (599.2 KB)
Suggest, I’m annotated some page, and need to save changes. Problem, that Aspose.Imaging don’t have method somthing like ReplaceFrame(int index, TiffFrame frame). We have only 3 methods: AddFrame, RemoveFrame, InsertFrame. I’m found solution on this forum like that.

TiffImage.ActiveFrame = TiffImage.Frames[1];
TiffImage.RemoveFrame(0);

But what if need replace all frames? I get an exception. For example, suggest we have 2 frames inside TIFF file. I’m changed frames, added some annotations, and I need to save this changes. My code:
public void ReplaceFrame(TiffImage image, int pageNumber)
{
// In this case suggest pageNumber = 0;
// New TiffFrame with changes
TiffFrame tempFrame = TiffFrame.CopyFrame(image.Frames[0]);
Tiff.ActiveFrame = Tiff.Frames[1];
// Remove original (old) frame
Tiff.RemoveFrame(pageNumber);
// Insert new frame with changes
Tiff.InsertFrame(pageNumber, tempFrame);
}

Next step, I want change second page
public void ReplaceFrame(TiffImage image, int pageNumber)
{
// Now, suggest pageNumber = 1;
TiffFrame tempFrame = TiffFrame.CopyFrame(image.Frames[0]);
// Here we get exception
Tiff.ActiveFrame = Tiff.Frames[0];
Tiff.RemoveFrame(pageNumber);
Tiff.InsertFrame(pageNumber, tempFrame);
}

Exception: “The active frame cannot be set as it belongs to another image”.
So, if I’m changed some frame, I cant make more this frame active. Any solutions? Main task - possibility to save any frame changes. Or, alternative variant - save every frame to disk, and then create new TiffImage, but this is VERY VERY BAD crutch.


#2

@tvv91,

I have observed your requirements and regret to share that present there is no option available to replace existing frame or copying active frame. An issue with ID 14188 has been created in our issue tracking system to further investigate the requirement. This thread has been linked with the issue so that you may be notified once the support will be available.


#3

I found solution and write next class (hope willbe help for somebody)

  1. I’m create 2 TiffImage objects. One object is public (as property), and initiate by constructor:
  2. Create TiffFrame[] array, and copy to this array original frames.
  3. Method ReplaceFrameImage changed frame in this array (we passed TiffImage and pageNumber parameter).
  4. When want export, we create new TiffImage from TiffFrame[] array.
  5. ??? PROFIT.

public class TiffParser
{
private readonly Stream imageStream = new MemoryStream();
private TiffImage Result;
private TiffFrame[] changedFrames;
public TiffImage Tiff { get; }
public int FrameCount { get; }
public TiffParser(Stream file)
{
Tiff = (TiffImage) Image.Load(file);
FrameCount = Tiff.Frames.Length;
changedFrames = new TiffFrame[FrameCount];
CopySourceFrames();
}
public TiffFrame GetFrame(int frameNumber)
{
return Tiff.Frames[frameNumber];
}
public void ReplaceFrameImage(TiffImage image, int pageNumber)
{
TiffFrame tempFrame = TiffFrame.CopyFrame(image.Frames[0]);
changedFrames[pageNumber] = tempFrame;
}
public Stream GetImageStream()
{
Result = new TiffImage(changedFrames);
Result.Save(imageStream);
return imageStream;
}
private void CopySourceFrames()
{
for (int i = 0; i < FrameCount; i++)
{
TiffFrame tempFrame = TiffFrame.CopyFrame(Tiff.Frames[i]);
changedFrames[i] = tempFrame;
}
}
}


#4

@tvv91,

I have seen the workaround approach shared by you and appreciate the effort. However, we have created the issue with ID 14188 as well to further investigate if we may add any approach to copy the active frame which in present case is throwing exception.


#5

Yeah, it’s would be great, if I can do this by Aspose.Imaging library, instead of bicycle creating.


#6

@tvv91,

Sure, we have created investigation ticket and we will share good news with you soon. I request for your patience.