Free Support Forum - groupdocs.com

How to get the Viewer output (png) as stream and not saving to file system in .NET

I would like to get the output (png images) of the Viewer as stream and not to save it into a directory.

I have investigated the CreatePageStream Interface / Delegate. However, I can only find a sample where the stream is also saved to the file system:

Viewer DocViewer = new Viewer(“D:\Path\Presentation.pptx”);

CreatePageStream createDelegate = delegate (int pageNumber)
{
var pagePath = System.IO.Path.Combine(@“C:\Temp”, string.Format(“page_{0}.png”, pageNumber));
return System.IO.File.Create(pagePath);
};

PngViewOptions Options = new PngViewOptions(createDelegate);
DocViewer.View(Options);

How can I get the png file as stream?

1 Like

@markusb

Instead of passing FileStream just pass MemoryStream and you will be able to get an output stream. Please have a look at this documentation article and below code:

MemoryStream outputStream = new MemoryStream();
Viewer DocViewer = new Viewer("D:\\Path\\Presentation.pptx");
CreatePageStream createDelegate = delegate (int pageNumber)
{
    return outputStream;
};
ReleasePageStream releaseDelegate = delegate (int pageNumber, Stream pageStream)
{
    /* Do not dispose the pageStream here in case you're planning to use the stream outside this block */
};
PngViewOptions Options = new PngViewOptions(createDelegate, releaseDelegate);
DocViewer.View(Options);
// Process outputStream

Basically the sample works fine. But how can I set the page number to convert?

I saw the number in the delegate as argument (int pageNumber) but where can I set the value?

1 Like

@markusb

We are investigating this scenario with ID VIEWERNET-2691. You’ll be notified as there’s any update.

And one further question: If we set the width of the png to a larger size the image becomes a little bit unsharp even if the width which is 1024 pixel is smaller than the normal screen output on the monitor. Is there a possibility to set the quality of the png rendering?

@markusb

We have an update on VIEWERNET-2691. You can just pass the page number that you want to convert to the View() method as a second parameter see:

PngViewOptions Options = new PngViewOptions(createDelegate, releaseDelegate);
DocViewer.View(Options, 3);

Please clarify how do you set the image size so that we can check it at our end.