Exemplo n.º 1
0
        private static void AddDocumentContent(IXpsFixedDocumentWriter fixedDocumentWriter,
                                               XmlNodeList selectedThumbnails)
        {
            Console.WriteLine("Using PackWebRequest and PackWebResponse APIs to retrieve selected photos ...");
            Console.WriteLine("------------------------------------------------------------------------------");

            foreach (XmlNode node in selectedThumbnails)
            {
                Uri    packageUri;
                Uri    partUri;
                string selectedId = node.Attributes["Id"].InnerText;

                try
                {
                    GetSelectedPhotoUri(selectedId, out packageUri, out partUri);
                }
                catch (Exception e)
                {
                    // We catch all exceptions because we don't want any
                    // exception caused by invalid input crash our server.
                    // But we record all exceptions, and try to continue.
                    Console.WriteLine("Hit exception while resolving photo Uri "
                                      + "for selected thumbnail ID " + selectedId);
                    Console.WriteLine("Exception:");
                    Console.WriteLine(e.ToString());
                    continue;
                }

                // Using PackWebRequest to retrieve the photo.
                using (WebResponse response =
                           PackWebRequestForPhotoPart(packageUri, partUri))
                {
                    Stream s = response.GetResponseStream();

                    // Add a fixed page into the fixed document.
                    IXpsFixedPageWriter fixedPageWriter =
                        fixedDocumentWriter.AddFixedPage();

                    // Add the photo image resource to the page.
                    XpsImage xpsImage =
                        fixedPageWriter.AddImage(response.ContentType);

                    SharedLibrary.CopyStream(s, xpsImage.GetStream());

                    xpsImage.Commit();

                    s.Seek(0, SeekOrigin.Begin);

                    // Use BitmapFrame to get the dimension of the photo image.
                    BitmapFrame imgFrame = BitmapFrame.Create(s);

                    // Write the XML description of the fixed page.
                    WritePageContent(fixedPageWriter.XmlWriter, xpsImage.Uri,
                                     imgFrame);

                    fixedPageWriter.Commit();
                }
            }
            Console.WriteLine("------------------------------------------------------------------------------");
            Console.WriteLine("All selected photos retrieved.");
        }