public MemoryStream GenerateThumbnailFromFirstPage(string xpsFilePath, XpsImageType imgType, ThumbnailQuality imgQuality = ThumbnailQuality.Medium) { if (LogHelper.CanDebug()) { LogHelper.Begin("XpsHelper.GenerateThumbnailFromFirstPage"); } try { BitmapEncoder bitmapEncoder = null; XpsDocument xpsDocument = new XpsDocument(xpsFilePath, FileAccess.Read); FixedDocumentSequence documentPageSequence = xpsDocument.GetFixedDocumentSequence(); string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(xpsFilePath); string fileExtension = string.Empty; switch (imgType) { case XpsImageType.JpegImageType: bitmapEncoder = new JpegBitmapEncoder(); break; case XpsImageType.PngImageType: bitmapEncoder = new PngBitmapEncoder(); break; } double imageQualityRatio = 1.0 / (double)imgQuality; DocumentPage documentPage = documentPageSequence.DocumentPaginator.GetPage(0); RenderTargetBitmap targetBitmap = new RenderTargetBitmap((int)(documentPage.Size.Width * imageQualityRatio), (int)(documentPage.Size.Height * imageQualityRatio), 96.0 * imageQualityRatio, 96.0 * imageQualityRatio, PixelFormats.Pbgra32); targetBitmap.Render(documentPage.Visual); bitmapEncoder.Frames.Add(BitmapFrame.Create(targetBitmap)); MemoryStream memoryStream = new MemoryStream(); bitmapEncoder.Save(memoryStream); xpsDocument.Close(); return(memoryStream); } catch (Exception err) { LogHelper.Manage("XpsHelper.GenerateThumbnailFromFirstPage", err); } finally { LogHelper.End("XpsHelper.GenerateThumbnailFromFirstPage"); } return(null); }
AddThumbnail( XpsImageType imageType ) { _thumbnail = CurrentXpsManager.AddThumbnail(imageType, this, Thumbnail); _metroPart.CreateRelationship(_thumbnail.Uri, TargetMode.Internal, XpsS0Markup.ThumbnailRelationshipName ); return(_thumbnail); }
}// end:AddImage() // --------------------------- GetXpsImage() -------------------------- /// <summary> /// Copies a given image to a specified XPS page.</summary> /// <param name="pageWriter"> /// The FixedPage writer to copy the image to.</param> /// <param name="imgPath"> /// The source file path for the image.</param> /// <param name="imageType"> /// The type of the image.</param> /// <returns> /// The package URI of the copied image.</returns> private string GetXpsImage( IXpsFixedPageWriter pageWriter, string imgPath, XpsImageType imageType) { XpsImage xpsImage = pageWriter.AddImage(imageType); Stream srcStream = new FileStream(imgPath, FileMode.Open); Stream dstStream = xpsImage.GetStream(); Byte[] streamBytes = new Byte[srcStream.Length]; srcStream.Read(streamBytes, 0, (int)srcStream.Length); dstStream.Write(streamBytes, 0, (int)srcStream.Length); xpsImage.Commit(); return(xpsImage.Uri.ToString()); }
AddThumbnail( XpsImageType imageType ) { CheckDisposed(); if (CurrentXpsManager == null) { throw new InvalidOperationException(SR.Get(SRID.ReachPackaging_DocumentWasClosed)); } _thumbnail = CurrentXpsManager.AddThumbnail(imageType, this, Thumbnail); Package metroPackage = CurrentXpsManager.MetroPackage; metroPackage.CreateRelationship(_thumbnail.Uri, TargetMode.Internal, XpsS0Markup.ThumbnailRelationshipName ); return(_thumbnail); }
}// end:GetXpsFont() // ---------------------------- AddImage() ---------------------------- /// <summary> /// Outputs the markup for adding an image.</summary> /// <param name="pageWriter"> /// The FixedPageWriter associated for getting the image.</param> /// <param name="writer"> The XmlWriter to output to.</param> /// <param name="imgPath"> The image path.</param> /// <param name="imageType">The image type</param> /// <param name="x"> The X position of the image.</param> /// <param name="y"> The Y position of the image.</param> /// <param name="height"> The height of the image.</param> /// <param name="width"> The width of the image.</param> private void AddImage( IXpsFixedPageWriter pageWriter, XmlWriter writer, string imgPath, XpsImageType imageType, float x, float y, float height, float width) { // Write the surrounding path. writer.WriteStartElement("Path"); // Form the path data string. // M="Move To"(start), L="Line To", Z="Close shape" string pathData = String.Format("M {0},{1} L {2},{3} {4},{5} {6},{7} z", x, y, x, y + height, x + width, y + height, x + width, y); writer.WriteAttributeString("Data", pathData); writer.WriteStartElement("Path.Fill"); writer.WriteStartElement("ImageBrush"); writer.WriteAttributeString("ImageSource", GetXpsImage(pageWriter, imgPath, imageType)); string viewPort = String.Format("{0},{1},{2},{3}", 0, 0, width, height); writer.WriteAttributeString("Viewport", viewPort); writer.WriteAttributeString("ViewportUnits", "Absolute"); string viewBox = String.Format("{0},{1},{2},{3}", x, y, x + width, y + height); writer.WriteAttributeString("Viewbox", viewBox); writer.WriteAttributeString("ViewboxUnits", "Absolute"); writer.WriteEndElement(); //ImageBrush writer.WriteEndElement(); //Path.Fill writer.WriteEndElement(); //Path }// end:AddImage()
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))); }
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; }
public static ContentType ImageTypeToString(XpsImageType imageType) { ContentType type = new ContentType(""); switch (imageType) { case XpsImageType.PngImageType: return XpsS0Markup.PngContentType; case XpsImageType.JpegImageType: return XpsS0Markup.JpgContentType; case XpsImageType.TiffImageType: return XpsS0Markup.TifContentType; case XpsImageType.WdpImageType: return XpsS0Markup.WdpContentType; } return type; }