1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
/* Copyright (C) 2020-2023 Artifex Software, Inc.
All Rights Reserved.
This software is provided AS-IS with no warranty, either express or
implied.
This software is distributed under license and may not be copied,
modified or distributed except as expressly authorized under the terms
of the license contained in the file LICENSE in this distribution.
Refer to licensing information at http://www.artifex.com or contact
Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
CA 94129, USA, for further information.
*/
using System;
using System.Collections.Generic;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Runtime.InteropServices;
using GhostNET;
namespace ghostnet_wpf_example
{
public partial class MainWindow
{
private static List<DocPage> m_thumbnails;
/* Assign current pages to blown up thumbnail images */
private void ThumbAssignMain(int page_num, int width, int height, double zoom_in, ref int offset)
{
m_pageType.Add(Page_Content_t.THUMBNAIL);
DocPage doc_page = new DocPage();
doc_page.Content = Page_Content_t.THUMBNAIL;
doc_page.Zoom = zoom_in;
doc_page.AA = m_aa;
doc_page.BitMap = m_thumbnails[page_num - 1].BitMap;
doc_page.Width = (int)(width / (Constants.SCALE_THUMB));
doc_page.Height = (int)(height / (Constants.SCALE_THUMB));
doc_page.PageNum = page_num;
m_docPages.Add(doc_page);
/* Set the page offsets. Used for determining which pages
* will be visible within viewport. */
m_toppage_pos.Add(offset + Constants.PAGE_VERT_MARGIN);
offset += doc_page.Height + 2 * Constants.PAGE_VERT_MARGIN;
/* Set page sizes for 1.0 scaling. This is used to quick
* rescale of pages prior to rendering at new zoom. */
pagesizes_t page_size = new pagesizes_t();
page_size.size.X = doc_page.Width;
page_size.size.Y = doc_page.Height;
m_page_sizes.Add(page_size);
}
/* Rendered all the thumbnail pages. Stick them in the appropriate lists */
private void ThumbsDone()
{
int offset = 0;
m_toppage_pos = new List<int>(m_list_thumb.Count);
for (int k = 0; k < m_list_thumb.Count; k++)
{
DocPage doc_page = new DocPage();
m_thumbnails.Add(doc_page);
doc_page.Width = m_list_thumb[k].width;
doc_page.Height = m_list_thumb[k].height;
doc_page.Content = Page_Content_t.THUMBNAIL;
doc_page.Zoom = m_list_thumb[k].zoom;
doc_page.BitMap = BitmapSource.Create(doc_page.Width, doc_page.Height,
72, 72, PixelFormats.Bgr24, BitmapPalettes.Halftone256, m_list_thumb[k].bitmap, m_list_thumb[k].raster);
doc_page.PageNum = m_list_thumb[k].page_num;
ThumbAssignMain(m_list_thumb[k].page_num, m_list_thumb[k].width, m_list_thumb[k].height, 1.0, ref offset);
}
m_toppage_pos.Add(offset);
xaml_ProgressGrid.Visibility = System.Windows.Visibility.Collapsed;
xaml_RenderProgress.Value = 0;
xaml_PageList.ItemsSource = m_docPages;
xaml_ThumbList.ItemsSource = m_thumbnails;
xaml_ThumbList.Items.Refresh();
xaml_ThumbGrid.Visibility = System.Windows.Visibility.Visible;
m_ghostscript.PageRenderedCallBack -= new GSNET.PageRendered(gsThumbRendered);
m_numpages = m_list_thumb.Count;
if (m_numpages < 1)
{
ShowMessage(NotifyType_t.MESS_STATUS, "File failed to open properly");
CleanUp();
}
else
{
xaml_TotalPages.Text = "/" + m_numpages;
xaml_currPage.Text = m_currpage.ToString();
m_list_thumb.Clear();
if (m_doc_type_has_page_access)
RenderMainRange();
else
RenderMainAll();
}
}
/* Callback from ghostscript with the rendered thumbnail. Also update progress */
private void ThumbPageCallback(int width, int height, int raster, double zoom_in,
int page_num, IntPtr data)
{
Byte[] bitmap = new byte[raster * height];
idata_t thumb = new idata_t();
Marshal.Copy(data, bitmap, 0, raster * height);
thumb.bitmap = bitmap;
thumb.page_num = page_num;
thumb.width = width;
thumb.height = height;
thumb.raster = raster;
thumb.zoom = zoom_in;
m_list_thumb.Add(thumb);
/* Dispatch progress bar update on UI thread */
System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
{
/* Logrithmic but it will show progress */
xaml_RenderProgress.Value = ((double) page_num / ((double) page_num + 1))* 100.0;
}));
}
/* Render the thumbnail images */
private void RenderThumbs()
{
xaml_RenderProgress.Value = 0;
xaml_RenderProgressText.Text = "Creating Thumbs";
xaml_ProgressGrid.Visibility = System.Windows.Visibility.Visible;
m_ghostscript.PageRenderedCallBack += new GSNET.PageRendered(gsThumbRendered);
m_ghostscript.DisplayDeviceRenderThumbs(m_currfile, Constants.SCALE_THUMB, false);
}
}
}
|