C# 使用Pdfium将PDF页面呈现为位图

C# 使用Pdfium将PDF页面呈现为位图,c#,wpf,bitmap,pdfium,C#,Wpf,Bitmap,Pdfium,我正在尝试使用将PDF页面转换为.NET位图。我想在WPF中直接使用它,所以不打算使用WinForm方法。在我试图使用FPDF_RenderPage时,我已经问了这个问题(基于我最初的理解)。从我到现在所学到的以及它唯一的.NET端口()有两种方法可以渲染位图 我想知道我在第一种方法中是否正确地执行了步骤,特别是在内存设备上下文中,以及在第二种方法中如何正确地将本机位图带到.NET中 通过提供内存设备上下文使用FPDF_RenderPage var hMemDC = NativeMethods.

我正在尝试使用将PDF页面转换为.NET
位图
。我想在WPF中直接使用它,所以不打算使用WinForm方法。在我试图使用
FPDF_RenderPage
时,我已经问了这个问题(基于我最初的理解)。从我到现在所学到的以及它唯一的.NET端口()有两种方法可以渲染位图

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

  • 通过提供内存设备上下文使用
    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);
    
  • 使用

  • //将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;
        }
    
    公共位图转换为ARGB(字节[]数据,整数宽度,整数高度)
    {
    位图Bm=新位图(宽度、高度、像素格式.Format32bppArgb);
    整数指数;
    对于(int y=0;y
    呈现:

    看起来您没有考虑每行的步幅(想想DWORD对齐)。配方奶粉应该有帮助。
        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;
        }