public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (!LabelOperator.isObjectExistingFile(value)) { return(null); } string btwFileName = (string)value; try { System.Drawing.Image btwImage = LabelFormatThumbnail.Create(btwFileName, System.Drawing.Color.Gray, 500, 500); using (var ms = new MemoryStream()) { btwImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png); ms.Position = 0; var bi = new BitmapImage(); bi.BeginInit(); bi.CacheOption = BitmapCacheOption.OnLoad; bi.StreamSource = ms; bi.EndInit(); btwImage.Dispose(); //if bmp is not used further. return(bi); } } catch (Exception ex) { Log.Instance.Logger.Error($"获取缩略图错误:{ex.Message}"); return(null); } }
/// <summary> /// Loads thumbnails in a background thread so the UI doesn't hang. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void thumbnailCacheWorker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = (BackgroundWorker)sender; int index; string fileName; // Loop until the queue of items that need to be loaded is empty or the worker was cancelled. while ((generationQueue.Count > 0) && !worker.CancellationPending && !isClosing) { lock (generationQueue) { // Get the index to use. index = generationQueue.Dequeue(); } // If this is way out of our view don't bother generating it. if (Math.Abs(index - topIndex) < 30) { fileName = browsingFormats[index]; // Check to see if the listitem is already generated and just waiting for a thumbnail. ListViewItem item = (ListViewItem)listItems[fileName]; if (item == null) { item = new ListViewItem(fileName); item.ImageIndex = (int)ImageIndex.LoadingFormatImage; listItems.Add(fileName, item); } // If the listitem doesn't already have a thumbnail, generate it. if (item.ImageIndex == (int)ImageIndex.LoadingFormatImage) { try { Image btwImage = LabelFormatThumbnail.Create(fileName, Color.Gray, 100, 100); object[] progressReport = new object[] { item, btwImage }; worker.ReportProgress(100, progressReport); } // If the file isn't an actual btw format file we will get an exception. catch { object[] progressReport = new object[] { item, null }; worker.ReportProgress(0, progressReport); } } } } }
/// <summary> /// Finds all BarTender documents in the server's Documents directory, generates thumbnails for them, /// and returns a list of valid files. In a real application, we recommend implementing some form of caching. /// Thumbnail generation can be slow, so our own implementation keeps track of file modification times to /// avoid re-generating thumbnails unnecessarily. /// </summary> public static List <WebLabelPrintDocument> GenerateDocumentsList() { List <WebLabelPrintDocument> documentsList = new List <WebLabelPrintDocument>(); string documentsFullPath = HttpContext.Current.Server.MapPath("~/Documents"); if (!Directory.Exists(documentsFullPath)) { return(documentsList); } foreach (string fileName in Directory.GetFiles(documentsFullPath)) { // Filter for BarTender documents (.btw files) if (!fileName.ToLowerInvariant().EndsWith("btw")) { continue; } string thumbnailFileName = fileName.Substring(0, fileName.Length - 3) + "png"; // Use the BarTender .NET Print SDK to generate a thumbnail. Note that this does not go through a Print Engine like many // of the other Print SDK functions. Communication with BarTender occurs via the Print Scheduler service. using (Image thumbnailImage = LabelFormatThumbnail.Create(fileName, Color.Transparent, 150, 150)) { if (thumbnailImage != null) { thumbnailImage.Save(thumbnailFileName); WebLabelPrintDocument document = new WebLabelPrintDocument(); document.FullPath = fileName; document.DisplayName = Path.GetFileName(fileName); document.ThumbnailRelativePath = "~/Documents/" + document.DisplayName.Substring(0, document.DisplayName.Length - 3) + "png"; documentsList.Add(document); } } } return(documentsList); }