Free Support Forum - aspose.com

Aspose failed to replace image back to ppt file

Hi,

I’m using Aspose .NET product and I got an issue with PPT file when I try replace the image in PPT file.

Below is my sample code:
FileStream fStream = new FileStream(inputPptFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
Presentation presetation = new Presentation(fStream);
foreach (IPPImage Image in presentation.Images)
{
string target = “C:\temp.image”; // Random filePath
File.WriteAllBytes(target, Image.BinaryData);
byte[] tempImage = File.ReadAllBytes(target);
Image.ReplaceImage(tempImage);
tempImage = null;
}
presentation.Save(outputPptFile, SaveFormat.Ppt);

My code only try to load the image to some random file and save it back to the PPT file. After saving image, my PPT file was corrupted. When I try to recover it then the image turn into black.

Here is my sample PPT file and sample text code: Sample.zip (985.6 KB)

I’m using Aspose .NET Slides 18.7 too.

Please help and give me some advise for this issue.

Thanks,

Dung H. Nguyen

@dunghnguyen,

I have observed the sample code shared by you. You need to comment following line in your sample.

      //  File.WriteAllBytes(target, Image.BinaryData);

In your example, you are loading the image from presentation slide image. Then again setting the same image back to slide image. Aspose.Slides is doing correct and there is issue in logic while setting image. I have used following code on my end.

    public static void TestReplaceImage()
    {
        String path = @"C:\Users\Muhammad\Downloads\ReplaceImage\";
        String inputPptFile = path + "ImageLost.ppt";
        FileStream fStream = new FileStream(inputPptFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
        fStream.Position = 0;
        Presentation presetation = new Presentation(fStream);
        foreach (IPPImage Image in presetation.Images)
        {
            string target = @"C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg"; // Random filePath
            //    string target = path+"test.jpg"; // Random filePath
        
          //  File.WriteAllBytes(target, Image.BinaryData);
            byte[] tempImage = File.ReadAllBytes(target);
            Image.ReplaceImage(tempImage);
            tempImage = null;
        }
        presetation.Save(path+"Saved.ppt", Aspose.Slides.Export.SaveFormat.Ppt);

    }

I have also attached the source image and generated presentation for reference.
ReplaceImage.zip (2.5 MB)