static private Image SaveStreamContents(PdfImageInfo imageInfo, byte[] byteArray)
    {
      Image result = null;

      Int32 BitsPerComponent = imageInfo.BitsPerComponent;
      Int32 ImageWidth = imageInfo.Width;
      Int32 ImageHeight = imageInfo.Height;

      if(BitsPerComponent == 8)
      {
        ColorSpaceEnum ColorSpace = imageInfo.ColorSpace;

        if((ColorSpace == ColorSpaceEnum.RGB) || (ColorSpace == ColorSpaceEnum.Gray))
        {
          // create empty bitmap
          Bitmap BM = new Bitmap(ImageWidth, ImageHeight, PixelFormat.Format24bppRgb);

          // create a new contents array with bmp width
          Byte[] PixelBuf = new Byte[((3 * ImageWidth + 3) & ~3) * ImageHeight];

          // copy row by row
          Int32 IPtr = 0;
          Int32 BPtr = 0;

          if(ColorSpace == ColorSpaceEnum.RGB)
          {
            for(Int32 Row = 0; Row < ImageHeight; Row++)
            {
              // copy column by column
              for(Int32 Col = 0; Col < ImageWidth; Col++)
              {
                PixelBuf[BPtr + 2] = byteArray[IPtr++];
                PixelBuf[BPtr + 1] = byteArray[IPtr++];
                PixelBuf[BPtr] = byteArray[IPtr++];
                BPtr += 3;
              }
              BPtr = (BPtr + 3) & ~3;
            }
          }
          else
          {
            for(Int32 Row = 0; Row < ImageHeight; Row++)
            {
              // copy column by column
              for(Int32 Col = 0; Col < ImageWidth; Col++)
              {
                PixelBuf[BPtr + 2] = byteArray[IPtr];
                PixelBuf[BPtr + 1] = byteArray[IPtr];
                PixelBuf[BPtr] = byteArray[IPtr++]; // Increment only after the last one
                BPtr += 3;
              }
              BPtr = (BPtr + 3) & ~3;
            }
          }

          // Lock the bitmap's bits.  
          Rectangle LockRect = new Rectangle(0, 0, ImageWidth, ImageHeight);
          BitmapData BmpData = BM.LockBits(LockRect, ImageLockMode.WriteOnly, BM.PixelFormat);

          // Get the address of the first line.
          IntPtr ImagePtr = BmpData.Scan0;

          // Copy contents into the bitmap
          Marshal.Copy(PixelBuf, 0, ImagePtr, PixelBuf.Length);

          // unlock the bitmap
          BM.UnlockBits(BmpData);

          result = BM;
        }
      }
      else if(BitsPerComponent == 1)
      {
        // create empty bitmap
        Bitmap BM = new Bitmap(ImageWidth, ImageHeight, PixelFormat.Format1bppIndexed);

        // Lock the bitmap's bits.  
        Rectangle LockRect = new Rectangle(0, 0, ImageWidth, ImageHeight);
        BitmapData BmpData = BM.LockBits(LockRect, ImageLockMode.WriteOnly, PixelFormat.Format1bppIndexed);

        Byte[] PixelBuf = new Byte[BmpData.Stride * BmpData.Height];

        int instride = (ImageWidth + 7) / 8;
        int outstride = BmpData.Stride;
        int inpos = 0;
        int outpos = 0;
        for(int y = 0; y < BmpData.Height; y++)
        {
          Array.Copy(byteArray, inpos, PixelBuf, outpos, instride);
          inpos += instride;
          outpos += outstride;
        }

        // Get the address of the first line.
        IntPtr ImagePtr = BmpData.Scan0;

        // Copy contents into the bitmap
        Marshal.Copy(PixelBuf, 0, ImagePtr, PixelBuf.Length);

        // unlock the bitmap
        BM.UnlockBits(BmpData);

        result = BM;
      }
      else
      { }

      return result;
    }
    private static PdfImageInfo GetImageInfoFromObject(IntPtr pageObject)
    {
      PdfImageInfo info = new PdfImageInfo();

      unsafe
      {
        Pdfium.GetImageFromPageObject(pageObject, &info);
      }
      return info;
    }