Q: A quick question regarding the rasterizer in the PDFNet , are there
any plans to allow direct rasterization to WPF bitmaps? At the present
I’m having to take the GDI+ bitmap, create a WPF bitmap of the same
dimensions and then copy the pixel data from the GDI+ bitmap to the
WPF bitmap.
The following is the code I’m currently using:
private void DisplayCurrentPage()
{
using (PDF.PDFDraw draw = new pdftron.PDF.PDFDraw())
{
draw.SetRasterizerType(PDF.PDFRasterizer.Type.e_BuiltIn);
draw.SetPageBox(PDF.Page.Box.e_media);
draw.SetDrawAnnotations(true);
draw.SetImageSmoothing(true);
draw.SetAntiAliasing(true);
draw.SetDPI(200);
using (System.Drawing.Bitmap gdiBitmap = draw.GetBitmap
(m_CurrentPdfPage))
{
System.Drawing.Imaging.PixelFormat srcPixelFormat =
gdiBitmap.PixelFormat;
Debug.WriteLine(srcPixelFormat);
WriteableBitmap wpfBitmap = new WriteableBitmap
(gdiBitmap.Width, gdiBitmap.Height, gdiBitmap.HorizontalResolution,
gdiBitmap.VerticalResolution, PixelFormats.Bgr24, null);
Debug.WriteLine(wpfBitmap);
System.Drawing.Imaging.BitmapData bitmapData =
gdiBitmap.LockBits(new System.Drawing.Rectangle
(0, 0, gdiBitmap.Width, gdiBitmap.Height),
System.Drawing.Imaging.ImageLockMode.ReadOnly, srcPixelFormat);
wpfBitmap.WritePixels(new Int32Rect(0, 0,
gdiBitmap.Width, gdiBitmap.Height), bitmapData.Scan0,
bitmapData.Stride *
bitmapData.Height, bitmapData.Stride);
gdiBitmap.UnlockBits(bitmapData);
imagePdfPage.Source = wpfBitmap;
}
}
}
A: An extended version of PDFNet with WPF extensions will be released
in the near future. In the meantime you could convert GDI+ bitmaps to
WPF bitmaps as shown in the above code snippet. You may want to make
sure that the pixel format for the WPF bitmap matches the pixel format
of input GDI+ bitmap. Another approach (though probably a bit less
efficient) is to export PDF to a PNG (using pdfdraw.Export(…)) and
then load the image in a WPF bitmap.