2

我正在尝试Bitmap使用Pdfium将 PDF 页面转换为 .NET 。我想直接在 WPF 中使用它,所以不打算使用 WinForm 方法。我尝试使用FPDF_RenderPage. 从我到现在从SDK和它的唯一 .NET 端口 ( PdfViewer ) 中学到的知识,有两种方法可以渲染到位图。

我想知道我是否在第一种方法中正确执行了这些步骤,特别是内存设备上下文,以及在第二种方法中正确地将本机位图带到 .NET 需要什么。

  1. 通过FPDF_RenderPage提供内存设备上下文来使用

    var hMemDC = NativeMethods.CreateCompatibleDC(IntPtr.Zero);
    IntPtr hDC = NativeMethods.GetDC(IntPtr.Zero);
    
    // Create a bitmap for output 
    var hBitmap = NativeMethods.CreateCompatibleBitmap(hDC, width, height);
    
    // Select the bitmap into the memory DC 
    hBitmap = NativeMethods.SelectObject(hMemDC, hBitmap);
    
    IntPtr brush = NativeMethods.CreateSolidBrush((int)ColorTranslator.ToWin32(Color.White));
    NativeMethods.FillRgn(hMemDC, NativeMethods.CreateRectRgn(0, 0, width, height), brush);
    
    // _file is an instance of PdfFile from PdfViewer port
    // Now render the page onto the memory DC, which in turn changes the bitmap                 
    bool success = _file.RenderPDFPageToDC(
                 page,
                 hMemDC,
                 (int)dpiX, (int)dpiY,
                 bounds.X, bounds.Y, bounds.Width, bounds.Height,
                 true /* fitToBounds */,
                 true /* stretchToBounds */,
                 true /* keepAspectRatio */,
                 true /* centerInBounds */,
                 true /* autoRotate */,
                 forPrinting
            );
    
    hBitmap = NativeMethods.SelectObject(hMemDC, hBitmap);
    
    Bitmap myBitmap = Bitmap.FromHbitmap(hBitmap);
    
  2. 使用FPDF_RenderPageBitmap

    IntPtr bitmapHandle = NativeMethods.FPDFBitmap_Create(bounds.Width, bounds.Height, 0);    
    
    // _file is an instance of PdfFile from PdfViewer port
    bool success = _file.RenderPDFPageToBitmap(
       page,
       bitmapHandle,
       (int)dpiX, (int)dpiY,
       bounds.X, bounds.Y, bounds.Width, bounds.Height,
       true /* fitToBounds */,
       true /* stretchToBounds */,
       true /* keepAspectRatio */,
       true /* centerInBounds */,
       true /* autoRotate */,
       forPrinting
    );                
    
    if (!success)
      throw new Win32Exception();
    else
    {
        byte[] bytes = new byte[bounds.Width * bounds.Height * 4];
        Marshal.Copy(bitmapHandle, bytes, 0, bytes.Length);
        bitmap = Convert_BGRA_TO_ARGB(bytes, bounds.Width, bounds.Height);
    }
    

// 将 BGRA 转换为 ARGB

    public Bitmap Convert_BGRA_TO_ARGB(byte[] DATA, int width, int height)
    {
        Bitmap Bm = new Bitmap(width, height, PixelFormat.Format32bppArgb);

        int index;
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                // BGRA TO ARGB
                index = 4 * (x + (y * width));
                Color c = Color.FromArgb(
                     DATA[index + 3],
                    DATA[index + 2],
                    DATA[index + 1],
                    DATA[index + 0]);
                Bm.SetPixel(x, y, c);
            }
        }
        return Bm;
    }

使成为: 在此处输入图像描述

4

0 回答 0