/// <inheritdoc/> public void GetThumbnail(uint cx, out IntPtr phbmp, out WTS_ALPHATYPE pdwAlpha) { phbmp = IntPtr.Zero; pdwAlpha = WTS_ALPHATYPE.WTSAT_UNKNOWN; if (cx == 0 || cx > MaxThumbnailSize) { return; } using (var stream = new ReadonlyStream(this.Stream as IStream)) { using (var memStream = new MemoryStream()) { stream.CopyTo(memStream); memStream.Position = 0; using (Bitmap thumbnail = GetThumbnail(memStream, cx)) { if (thumbnail != null && thumbnail.Size.Width > 0 && thumbnail.Size.Height > 0) { phbmp = thumbnail.GetHbitmap(System.Drawing.Color.Transparent); pdwAlpha = WTS_ALPHATYPE.WTSAT_ARGB; } } } } }
/// <inheritdoc/> public void GetThumbnail(uint cx, out IntPtr phbmp, out WTS_ALPHATYPE pdwAlpha) { phbmp = IntPtr.Zero; pdwAlpha = WTS_ALPHATYPE.WTSAT_UNKNOWN; if (cx == 0 || cx > MaxThumbnailSize) { return; } using var dataStream = new ReadonlyStream(this.Stream as IStream); using var memStream = new MemoryStream(); dataStream.CopyTo(memStream); memStream.Position = 0; // AsRandomAccessStream() extension method from System.Runtime.WindowsRuntime var pdf = PdfDocument.LoadFromStreamAsync(memStream.AsRandomAccessStream()).GetAwaiter().GetResult(); if (pdf.PageCount > 0) { using var page = pdf.GetPage(0); var image = PageToImage(page, cx); using Bitmap thumbnail = new Bitmap(image); phbmp = thumbnail.GetHbitmap(); pdwAlpha = WTS_ALPHATYPE.WTSAT_RGB; } }
/// <summary> /// Start the preview on the Control. /// </summary> /// <param name="dataSource">Stream reference to access source file.</param> public override void DoPreview <T>(T dataSource) { this.SuspendLayout(); try { using (var dataStream = new ReadonlyStream(dataSource as IStream)) { var memStream = new MemoryStream(); dataStream.CopyTo(memStream); memStream.Position = 0; try { // AsRandomAccessStream() extension method from System.Runtime.WindowsRuntime var pdf = PdfDocument.LoadFromStreamAsync(memStream.AsRandomAccessStream()).GetAwaiter().GetResult(); if (pdf.PageCount > 0) { InvokeOnControlThread(() => { _flowLayoutPanel = new FlowLayoutPanel { AutoScroll = true, AutoSize = true, Dock = DockStyle.Fill, FlowDirection = FlowDirection.TopDown, WrapContents = false, }; _flowLayoutPanel.Resize += FlowLayoutPanel_Resize; // Only show first 10 pages. for (uint i = 0; i < pdf.PageCount && i < 10; i++) { using (var page = pdf.GetPage(i)) { var image = PageToImage(page); var picturePanel = new Panel() { Name = "picturePanel", Margin = new Padding(6, 6, 6, 0), Size = CalculateSize(image), BorderStyle = BorderStyle.FixedSingle, }; var picture = new PictureBox { Dock = DockStyle.Fill, Image = image, SizeMode = PictureBoxSizeMode.Zoom, }; picturePanel.Controls.Add(picture); _flowLayoutPanel.Controls.Add(picturePanel); } } if (pdf.PageCount > 10) { var messageBox = new RichTextBox { Name = "messageBox", Text = Resources.PdfMorePagesMessage, BackColor = Color.LightYellow, Dock = DockStyle.Fill, Multiline = true, ReadOnly = true, ScrollBars = RichTextBoxScrollBars.None, BorderStyle = BorderStyle.None, }; messageBox.ContentsResized += RTBContentsResized; _flowLayoutPanel.Controls.Add(messageBox); } Controls.Add(_flowLayoutPanel); }); } } catch (Exception ex) { if (ex.Message.Contains("Unable to update the password. The value provided as the current password is incorrect.", StringComparison.Ordinal)) { InvokeOnControlThread(() => { Controls.Clear(); _infoBar = GetTextBoxControl(Resources.PdfPasswordProtectedError); Controls.Add(_infoBar); }); } else { throw; } } finally { memStream.Dispose(); } } PowerToysTelemetry.Log.WriteEvent(new PdfFilePreviewed()); } catch (Exception ex) { PowerToysTelemetry.Log.WriteEvent(new PdfFilePreviewError { Message = ex.Message }); InvokeOnControlThread(() => { Controls.Clear(); _infoBar = GetTextBoxControl(Resources.PdfNotPreviewedError); Controls.Add(_infoBar); }); } finally { base.DoPreview(dataSource); } this.ResumeLayout(false); this.PerformLayout(); }