/// <inheritdoc /> public byte[] GetImage() { lock (DocLib.Lock) { var width = GetPageWidth(); var height = GetPageHeight(); var bitmap = fpdf_view.FPDFBitmapCreate(width, height, 1); if (bitmap == null) { throw new DocnetException("failed to create a bitmap object"); } var stride = fpdf_view.FPDFBitmapGetStride(bitmap); var result = new byte[stride * height]; try { // | | a b 0 | // | matrix = | c d 0 | // | | e f 1 | using (var matrix = new FS_MATRIX_()) using (var clipping = new FS_RECTF_()) { matrix.A = (float)_scaling; matrix.B = 0; matrix.C = 0; matrix.D = (float)_scaling; matrix.E = 0; matrix.F = 0; clipping.Left = 0; clipping.Right = width; clipping.Bottom = 0; clipping.Top = height; fpdf_view.FPDF_RenderPageBitmapWithMatrix(bitmap, _page, matrix, clipping, 0); var buffer = fpdf_view.FPDFBitmapGetBuffer(bitmap); Marshal.Copy(buffer, result, 0, result.Length); } } catch (Exception ex) { throw new DocnetException("error rendering page", ex); } finally { fpdf_view.FPDFBitmapDestroy(bitmap); } return(result); } }
protected override unsafe PdfBitmap OnExecute(CancellationToken cancellationToken) { try { cancellationToken.ThrowIfCancellationRequested(); _bitmap = fpdfview.FPDFBitmapCreateEx( (int)_viewport.Size.Width, (int)_viewport.Size.Height, (int)FPDFBitmapFormat.BGRA, IntPtr.Zero, 0); if (_bitmap == null) { throw new Exception("failed to create a bitmap object"); } cancellationToken.ThrowIfCancellationRequested(); if (_backgroundColor.HasValue) { fpdfview.FPDFBitmapFillRect( _bitmap, 0, 0, (int)_viewport.Size.Width, (int)_viewport.Size.Height, _backgroundColor.Value.ToPixel <Argb32>().Argb); cancellationToken.ThrowIfCancellationRequested(); } using var clipping = new FS_RECTF_ { Left = 0, Right = _viewport.Size.Width, Bottom = 0, Top = _viewport.Size.Height }; // | | a b 0 | // | matrix = | c d 0 | // | | e f 1 | using var matrix = new FS_MATRIX_ { A = _scale, B = 0, C = 0, D = _scale, E = -_viewport.X, F = -_viewport.Y }; fpdfview.FPDF_RenderPageBitmapWithMatrix(_bitmap, _pageInstance, matrix, clipping, (int)_flags); cancellationToken.ThrowIfCancellationRequested(); var scan0 = fpdfview.FPDFBitmapGetBuffer(_bitmap); var image = Image.WrapMemory <Bgra32>( scan0.ToPointer(), (int)_viewport.Size.Width, (int)_viewport.Size.Height); return(new PdfBitmap(_bitmap, image, _dispatcher, _scale, _viewport)); } catch (OperationCanceledException) { if (_bitmap != null) { fpdfview.FPDFBitmapDestroy(_bitmap); } throw; } catch (Exception ex) { if (_bitmap != null) { fpdfview.FPDFBitmapDestroy(_bitmap); } throw new Exception("Error rendering page. Check inner exception.", ex); } }