Free Support Forum - aspose.com

PresentationEx.Save() to MemoryStream results in a corrupt file message

Hello,


when I try to save a PresentationEx to a MemoryStream and using the buffer bytes to save it to a file, PowerPoint shows a repair message on opening the pptx. If I save the PresentationEx directly to a file, there is no such message. (I’m using latest version of Aspose.Slides.NET)

Here a very simple example:

class Program
{
static void Main(string[] args)
{
var pptx = new PresentationEx();
var idx = pptx.Slides[0].Shapes.AddAutoShape(ShapeTypeEx.Rectangle, 0, 0, 200, 250);
(pptx.Slides[0].Shapes[idx] as AutoShapeEx).TextFrame.Text = “Test”;
        <span style="color:blue;">byte</span>[] buffer;
        <span style="color:blue;">using</span> (<span style="color:blue;">var</span> ms = <span style="color:blue;">new</span> <span style="color:#2b91af;">MemoryStream</span>())
        {
            pptx.Save(ms, <span style="color:#2b91af;">SaveFormat</span>.Pptx);
            buffer = ms.GetBuffer();
        }

        <span style="color:blue;">var</span> id = <span style="color:#2b91af;">Guid</span>.NewGuid().ToString();
        <span style="color:green;">// save using bytes from MemoryStream</span>
        <span style="color:#2b91af;">File</span>.WriteAllBytes(<span style="color:#2b91af;">Path</span>.Combine(<span style="color:#2b91af;">Environment</span>.GetFolderPath(<span style="color:#2b91af;">Environment</span>.<span style="color:#2b91af;">SpecialFolder</span>.Desktop), <span style="color:#a31515;">"MemoryStream_"</span> + id + <span style="color:#a31515;">".pptx"</span>), buffer);
        <span style="color:green;">// save directly to file</span>
        pptx.Save(<span style="color:#2b91af;">Path</span>.Combine(<span style="color:#2b91af;">Environment</span>.GetFolderPath(<span style="color:#2b91af;">Environment</span>.<span style="color:#2b91af;">SpecialFolder</span>.Desktop), <span style="color:#a31515;">"Direct_"</span> + id + <span style="color:#a31515;">".pptx"</span>), <span style="color:#2b91af;">SaveFormat</span>.Pptx);
    }
}</pre></div>

Hi Daniel,


Please use the following sample code to serve the purpose. Please share, if I may help you further in this regard.

var pptx = new PresentationEx();
var idx = pptx.Slides[0].Shapes.AddAutoShape(ShapeTypeEx.Rectangle, 0, 0, 200, 250);
(pptx.Slides[0].Shapes[idx] as AutoShapeEx).TextFrame.Text = “Test”;
byte[] buffer;
using (var ms = new MemoryStream())
{
pptx.Save(ms, SaveFormat.Pptx);
ms.Position = 0;
buffer = new Byte[ms.Length];
ms.Read(buffer, 0,(int) ms.Length);
}
var id = Guid.NewGuid().ToString();
// save using bytes from MemoryStream
File.WriteAllBytes(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), “MemoryStream_” + id + “.pptx”), buffer);
// save directly to file
pptx.Save(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), “Direct_” + id + “.pptx”), SaveFormat.Pptx);


Many Thanks,

Thank you very much for your answer.


I’m sorry that I bothered you with that. I should have read the documentation of GetBuffer(), witch says, that there may be unused bytes in the resulting byte array.

Hi Daniel,


You are always welcome. Yes you have identified the cause of the issue rightly. Cheers. :).

Many Thanks,