Converting pdf pages to image and adding to stackpanel,other than stackpanel any other method using c# RRS feed

  • Question

  • Scenario:

    We are converting pdf page to image as night mode and added that image to stackpanel,

    what will be the case if we convert for ex:50 pdf pages,it will take time in conversion and stackpanel children items increase ,any other method using c#

    Below code:


       StorageFolder installedLocation = Package.Current.InstalledLocation;
                var pdfFile = await installedLocation.GetFileAsync(App.PDFPATHHOLDER);
                PdfDocument pdfDoc = await PdfDocument.LoadFromFileAsync(pdfFile);
                var pdfPagecounter = pdfDoc.PageCount;
                pp.IsActive = true;
                for (ise = 0; ise <= pdfPagecounter - 1; ise++)//loop total num of pages to convert
                    PdfPage pdfPage = pdfDoc.GetPage(ise);

                    using (IRandomAccessStream stream = new InMemoryRandomAccessStream())
                        PdfPageRenderOptions options = new PdfPageRenderOptions();

                        await pdfPage.RenderToStreamAsync(stream, options);

                        WriteableBitmap wb = new WriteableBitmap((int)pdfPage.Size.Height, (int)pdfPage.Size.Width);
                        await wb.SetSourceAsync(stream);

                        using (Stream pixels = wb.PixelBuffer.AsStream())
                            pixels.Seek(0, SeekOrigin.Begin);

                            for (int i = 0; i < pixels.Length; i++)
                                byte subPixel = (byte)pixels.ReadByte();
                                // WB pixels are RGBA. Only change RGB, not A
                                if ((i + 1) % 4 != 0)
                                    // write over the same pixel we just read
                                    pixels.Seek(-1, SeekOrigin.Current);
                                    // write the modified pixel (inverted colour in this case)
                                    pixels.WriteByte((byte)(byte.MaxValue - subPixel));
                        // Display the page on an Image in our Xaml Visual Tree
                        Image IMG= new Image();

                        IMG.Source = wb;
                        IMG.Margin = new Thickness(0, 10, 0, 0);

                        StackImg.Children.Add(IMG);//Stack Panel conversion of Pdf page  as image and added to stackpanel


    Friday, January 2, 2015 12:58 PM


  • Hi sandepmsandy,

    As we can see the time spent is on two parts:

    1, PDF page render to image

    2, image convert to night mode

    To short the render time, I would suggest:

    Load less PDF pages one time, here you have 50 pages per time, how about render 5 pages with 10 times. Render 1-5 page at first time, when user read to 3rd or 4th page, render 6-10 pages. etc..

    You can use something like ListView or FlipView control to let user switch pages base on their reading progress and have page load when meet some requirements.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, January 5, 2015 5:53 AM