This is a migrated thread and some comments may be shown as answers.

PDF Thumbnail returns transparent images

3 Answers 178 Views
PdfProcessing
This is a migrated thread and some comments may be shown as answers.
Iwhp
Top achievements
Rank 1
Iwhp asked on 10 Nov 2014, 02:13 PM
Why does the following code return a transparent image?
The WHITE is transparent.

PdfFormatProvider provider = new PdfFormatProvider(file, FormatProviderSettings.ReadOnDemand);
RadFixedDocument document = provider.Import();
ThumbnailFactory factory = new ThumbnailFactory();
RadFixedPage page = document.Pages[0];
System.Windows.Media.ImageSource source = factory.CreateThumbnail(page, page.Size);

var image = (BitmapSource)source;
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(image));
using (var ms = new MemoryStream())
{
   encoder.Save(ms);
   ms.Position = 0;
   return ms;
}

Thankx, Harry

3 Answers, 1 is accepted

Sort by
0
Deyan
Telerik team
answered on 12 Nov 2014, 08:49 AM
Hello Harry,

Thank you for contacting us.

You can create image and place it in a grid with white background to achieve the desired result. The following code snippet shows how to create such image from the first page of a document:
int pageNumber = 0;
RadFixedPage page = this.pdfViewer.Document.Pages[pageNumber];
  
var tf = new ThumbnailFactory();
ImageSource imageSource = tf.CreateThumbnail(page, new Size(page.ActualWidth, page.ActualHeight));
  
Image image = new Image();
image.Width = page.ActualWidth;
image.Height = page.ActualHeight;
image.Source = imageSource;
  
Grid container = new Grid();
container.Background = new SolidColorBrush(Colors.White);
container.Children.Add(image);
container.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
container.Arrange(new Rect(new Point(0, 0), container.DesiredSize));
  
RenderTargetBitmap bitmap = new RenderTargetBitmap((int)page.ActualWidth, (int)page.ActualHeight, 96, 96, PixelFormats.Default);
bitmap.Render(container);
  
var encoder = new PngBitmapEncoder();
using (var fs = new FileStream(@"C:\test.png", FileMode.Create))
{
    encoder.Frames.Add(BitmapFrame.Create(bitmap));
    encoder.Save(fs);
}

I hope this is helpful. If you have any other questions or concerns please do not hesitate to contact us again.

Regards,
Deyan
the Telerik team
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Chris
Top achievements
Rank 1
Veteran
Iron
answered on 23 Feb 2016, 08:51 AM

HI

 

 

>Grid container = new Grid();

What is the namespace/assembly of Grid ?

--

Maybe the code should list the namespace briefly (for novices):

using System.Windows.Media;
using System.Windows.Media.Imaging;
using Telerik.Windows.Documents.Fixed.FormatProviders;
using Telerik.Windows.Documents.Fixed.FormatProviders.Pdf;
using Telerik.Windows.Documents.Fixed.Model;
using Telerik.Windows.Documents.Fixed.Model.Objects;
using Telerik.Windows.Documents.UI;

 

Best regards

 

Chris

 

 

Grid container = new Grid();
Grid container = new Grid();
Grid container = new Grid();
Grid container = new Grid();
0
Tanya
Telerik team
answered on 25 Feb 2016, 10:56 AM
Hello Chris,

The Grid class resides in the System.Windows.Controls namespace. In order to use it, you will need to add a reference to the PresentationFramework assembly.

The next snippet shows all the required namespaces for using the snippet from post.
using System.IO;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Telerik.Windows.Documents.Fixed.Model;
using Telerik.Windows.Documents.UI;

Hope this helps.

Regards,
Tanya
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
PdfProcessing
Asked by
Iwhp
Top achievements
Rank 1
Answers by
Deyan
Telerik team
Chris
Top achievements
Rank 1
Veteran
Iron
Tanya
Telerik team
Share this question
or