Пример #1
0
        private void AddThumbnail(object sender, RoutedEventArgs e)
        {
            FixedDocumentSequence docSeq    = _xpsDocument.GetFixedDocumentSequence();
            DocumentPaginator     paginator = docSeq.DocumentPaginator;
            DocumentPage          page      = paginator.GetPage(0);
            // Create a rendertarget for the glyphs element. Round up width and height
            RenderTargetBitmap rt = new RenderTargetBitmap((int)page.Size.Width,
                                                           (int)page.Size.Height,
                                                           96, 96,
                                                           PixelFormats.Default);


            FixedPage fixedPage = (FixedPage)page.Visual;

            fixedPage.Measure(new Size(page.Size.Width, page.Size.Height));
            fixedPage.Arrange(new Rect(new Size(page.Size.Width, page.Size.Height)));
            rt.Render(fixedPage);
            JpegBitmapEncoder jpegEncoder = new JpegBitmapEncoder();

            jpegEncoder.Frames.Add(BitmapFrame.Create((BitmapSource)rt));
            Stream stream = new MemoryStream();

            jpegEncoder.Save(stream);
            XpsThumbnail thumbnail = _xpsDocument.AddThumbnail(XpsImageType.JpegImageType);
            Stream       dstStream = thumbnail.GetStream();

            byte[] thumbnailData = new byte[stream.Length];
            // let's read the whole block into our buffer
            int totalBytesRead = 0;
            int requestedCount = (int)stream.Length;

            stream.Position = 0;
            while (totalBytesRead < requestedCount)
            {
                int bytesRead = stream.Read(thumbnailData,
                                            totalBytesRead,
                                            requestedCount - totalBytesRead);
                if (bytesRead == 0)
                {
                    break;
                }
                totalBytesRead += bytesRead;
            }
            dstStream.Write(thumbnailData, 0, (int)stream.Length);
            dstStream.Close();
            thumbnail.Commit();
            _xpsDocument.Close();
            stream.Position = 0;
            ImageBrush imageBrush = new ImageBrush(BitmapFrame.Create(stream));

            imageBrush.Stretch    = Stretch.Uniform;
            thumbnailDisplay.Fill = imageBrush;

            // Once a thumbnail is set disable the "Add Thumbnail" button.
            addThumbnailButton.IsEnabled = false;
        }
Пример #2
0
        public bool WriteDocument(List <byte[]> images, string file_out)
        {
            XpsDocument doc = null;
            IXpsFixedDocumentSequenceWriter docSeqWriter = null;
            IXpsFixedDocumentWriter         docWriter    = null;

            if (LogHelper.CanDebug())
            {
                LogHelper.Begin("XpsHelper.WriteDocument");
            }
            try
            {
                // xps doc does not manage overwrite, so delete before if exist
                if (File.Exists(file_out))
                {
                    File.Delete(file_out);
                }

                // create the document
                doc = new XpsDocument(file_out, FileAccess.ReadWrite, CompressionOption.NotCompressed);

                // Create the document sequence
                docSeqWriter = doc.AddFixedDocumentSequence();

                // Create the document
                docWriter = docSeqWriter.AddFixedDocument();

                for (int i = 0; i < images.Count; ++i)
                {
                    // Create the Page
                    IXpsFixedPageWriter pageWriter = docWriter.AddFixedPage();

                    // Get the XmlWriter
                    XmlWriter xmlWriter = pageWriter.XmlWriter;

                    //create the xps image resource
                    XpsImage xpsImage = pageWriter.AddImage(XpsImageType.JpegImageType);
                    using (Stream dstImageStream = xpsImage.GetStream())
                    {
                        dstImageStream.Write(images[i], 0, images[i].Length);
                        xpsImage.Commit();
                    }

                    //this is just to get the real WPF image size as WPF display units and not image pixel size !!
                    using (MemoryStream ms = new MemoryStream(images[i]))
                    {
                        BitmapImage myImage = new BitmapImage();
                        myImage.BeginInit();
                        myImage.CacheOption  = BitmapCacheOption.OnLoad;
                        myImage.StreamSource = ms;
                        myImage.EndInit();

                        //write the page
                        WritePageContent(xmlWriter, xpsImage, myImage.Width, myImage.Height);
                    }

                    //with the first page, create the thumbnail of the xps document
                    if (i == 0)
                    {
                        XpsThumbnail thumb = doc.AddThumbnail(XpsImageType.JpegImageType);

                        using (MemoryStream ms = new MemoryStream(images[i]))
                        {
                            BitmapImage thumbImage = new BitmapImage();
                            thumbImage.BeginInit();
                            thumbImage.DecodePixelWidth = 256;
                            thumbImage.CacheOption      = BitmapCacheOption.OnLoad;
                            thumbImage.StreamSource     = ms;
                            thumbImage.EndInit();

                            using (Stream xpsThumbStream = thumb.GetStream())
                            {
                                JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                                encoder.Frames.Add(BitmapFrame.Create(thumbImage));
                                encoder.Save(xpsThumbStream);
                            }
                        }
                        thumb.Commit();
                    }

                    xmlWriter.Flush();

                    // Close the page
                    pageWriter.Commit();
                }

                return(true);
            }
            catch (Exception err)
            {
                LogHelper.Manage("XpsHelper.WriteDocument", err);
                return(false);
            }
            finally
            {
                if (docWriter != null)
                {
                    docWriter.Commit();
                }

                if (docSeqWriter != null)
                {
                    docSeqWriter.Commit();
                }

                if (doc != null)
                {
                    doc.Close();
                }

                LogHelper.End("XpsHelper.WriteDocument");
            }
        }
Пример #3
0
        public BitmapImage GetXpsThumbnail(string xpsFilePath)
        {
            XpsDocument document   = null;
            BitmapImage thumbImage = null;

            if (LogHelper.CanDebug())
            {
                LogHelper.Begin("XpsHelper.GetXpsThumbnail");
            }
            try
            {
                document = new XpsDocument(xpsFilePath, FileAccess.Read);
                XpsThumbnail thumb = document.Thumbnail;

                if (thumb != null)
                {
                    using (Stream xpsThumbStream = thumb.GetStream())
                    {
                        Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart) delegate
                        {
                            thumbImage = new BitmapImage();
                            thumbImage.BeginInit();
                            thumbImage.DecodePixelWidth = 70;
                            thumbImage.CacheOption      = BitmapCacheOption.OnLoad;
                            thumbImage.StreamSource     = xpsThumbStream;
                            thumbImage.EndInit();
                        });
                    }
                }
                else
                {
                    using (Stream xpsThumb = GenerateThumbnailFromFirstPage(xpsFilePath, XpsImageType.JpegImageType, ThumbnailQuality.Low))
                    {
                        Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart) delegate
                        {
                            thumbImage = new BitmapImage();
                            thumbImage.BeginInit();
                            thumbImage.DecodePixelWidth = 70;
                            thumbImage.CacheOption      = BitmapCacheOption.OnLoad;
                            thumbImage.StreamSource     = xpsThumb;
                            thumbImage.EndInit();
                        });
                    }
                }
            }
            catch (Exception err)
            {
                LogHelper.Manage("XpsHelper.GetXpsThumbnail", err);
            }
            finally
            {
                if (document != null)
                {
                    document.Close();
                }

                LogHelper.End("XpsHelper.GetXpsThumbnail");
            }

            return(thumbImage);
        }
Пример #4
0
 public XpsThumbnail EnsureThumbnail(INode parent, PackagePart part)
 {
     XpsThumbnail thumbnail = null;
     PackagePart thumbnailPart = this.GetThumbnailPart(part);
     if (thumbnailPart != null)
     {
         thumbnail = new XpsThumbnail(this, parent, thumbnailPart);
     }
     return thumbnail;
 }
Пример #5
0
 public XpsThumbnail AddThumbnail(XpsImageType imageType, INode parent, XpsThumbnail oldThumbnail)
 {
     if (oldThumbnail != null)
     {
         throw new XpsPackagingException(SR.Get("ReachPackaging_AlreadyHasThumbnail"));
     }
     if ((imageType != XpsImageType.JpegImageType) && (imageType != XpsImageType.PngImageType))
     {
         throw new XpsPackagingException(SR.Get("ReachPackaging_UnsupportedThumbnailImageType"));
     }
     return new XpsThumbnail(this, parent, this.GenerateUniquePart(ImageTypeToString(imageType)));
 }
Пример #6
0
 private void EnsureThumbnail()
 {
     if (this._thumbnail == null)
     {
         this._thumbnail = base.CurrentXpsManager.EnsureThumbnail(this, null);
     }
 }
Пример #7
0
 protected virtual void Dispose(bool disposing)
 {
     if (!this._disposed)
     {
         if (disposing)
         {
             this._thumbnail = null;
             this._reachSignatures = null;
             this._reachSignatureList = null;
             this._opcPackage = null;
             base.CurrentXpsManager.Close();
             this.CommitInternal();
         }
         this._disposed = true;
     }
 }
Пример #8
0
 public XpsThumbnail AddThumbnail(XpsImageType imageType)
 {
     this.CheckDisposed();
     if (base.CurrentXpsManager == null)
     {
         throw new InvalidOperationException(SR.Get("ReachPackaging_DocumentWasClosed"));
     }
     this._thumbnail = base.CurrentXpsManager.AddThumbnail(imageType, this, this.Thumbnail);
     base.CurrentXpsManager.MetroPackage.CreateRelationship(this._thumbnail.Uri, TargetMode.Internal, XpsS0Markup.ThumbnailRelationshipName);
     return this._thumbnail;
 }