public void InitCanvas(Size size) { if (_canvasBitmap == null) { _canvasBitmap = new PdfBitmap(size.Width, size.Height, true); } if (_formsBitmap != null) { _formsBitmap.Dispose(); } _formsBitmap = new PdfBitmap(size.Width, size.Height, true); _waitTime = 70; _prevTicks = DateTime.Now.Ticks; }
public void Dispose() { if (Bitmap != null) { Bitmap.Dispose(); } Bitmap = null; }
public void ReleaseCanvas() { foreach (var i in this) { ReleasePage(i.Key); } this.Clear(); if (_canvasBitmap != null) { _canvasBitmap.Dispose(); } _canvasBitmap = null; }
private void RenderPage(int pageNumber, DrawingVisual visual) { int dpiX = PrinterTicket.PageResolution.X ?? 96; int dpiY = PrinterTicket.PageResolution.Y ?? 96; //Calculate the size of the printable area in inches //The printable area represents a DIPs (Device independed points) (DIPs = pixels/(DPI/96) ) var fitSize = new Size() { Width = PageSize.Width / 96.0, Height = PageSize.Height / 96.0 }; //Get page's size in inches //The page size represents a points (1pt = 1/72 inch) var pdfSize = new Size() { Width = _doc.Pages[pageNumber].Width / 72.0f, Height = _doc.Pages[pageNumber].Height / 72.0f }; //If page was rotated in original file, then we need to "rotate the paper in printer". //For that just swap the width and height of the paper. if (_doc.Pages[pageNumber].OriginalRotation == PageRotate.Rotate270 || _doc.Pages[pageNumber].OriginalRotation == PageRotate.Rotate90) { fitSize = new Size(fitSize.Height, fitSize.Width); } //Calculate the render size (in inches) fitted to the paper's size. var rSize = GetRenderSize(pdfSize, fitSize); int pixelWidth = (int)(rSize.Width * dpiX); int pixelHeight = (int)(rSize.Height * dpiY); using (PdfBitmap bmp = new PdfBitmap(pixelWidth, pixelHeight, true)) { //Render to PdfBitmap using page's Render method with FPDF_PRINTING flag. _doc.Pages[pageNumber].RenderEx( bmp, 0, 0, pixelWidth, pixelHeight, PageRotate.Normal, RenderFlags.FPDF_PRINTING | RenderFlags.FPDF_ANNOT); //Rotates the PdfBitmap image depending on the orientation of the page PdfBitmap b2 = null; if (PageRotation(_doc.Pages[pageNumber]) == PageRotate.Rotate270) { b2 = bmp.SwapXY(false, true); } else if (PageRotation(_doc.Pages[pageNumber]) == PageRotate.Rotate180) { b2 = bmp.FlipXY(true, true); } else if (PageRotation(_doc.Pages[pageNumber]) == PageRotate.Rotate90) { b2 = bmp.SwapXY(true, false); } int stride = b2 == null ? bmp.Stride : b2.Stride; int width = b2 == null ? bmp.Width : b2.Width; int height = b2 == null ? bmp.Height : b2.Height; IntPtr buffer = b2 == null ? bmp.Buffer : b2.Buffer; var imgsrc = CreateImageSource(b2 ?? bmp); if (b2 != null) { b2.Dispose(); } var dc = visual.RenderOpen(); dc.DrawImage(imgsrc, new Rect(0, 0, imgsrc.PixelWidth / (dpiX / 96.0), imgsrc.Height / (dpiY / 90.0))); dc.Close(); imgsrc = null; } }