C#SDK中的foxit.common.Bitmap如何与System.Drawing.Bitmap互转?

从SDK 9.2版本及以后的版本中,SDK各个API中常用的位图类从之前的System.Drawing.Bitmap 转变成 foxit.common.Bitmap。如果在项目里升级SDK,可能会涉及这两个类的类型转换。转换示例代码如下:

  1. System.Drawing.Bitmap 转 foxit.common.Bitmap:
 static public System.Drawing.Bitmap FoxitBitamp2SystemBitmap(Bitmap _bitmap)
 {

     System.Drawing.Imaging.PixelFormat pixel_format = System.Drawing.Imaging.PixelFormat.Format24bppRgb;
     switch (_bitmap.GetFormat())
     {
         case Bitmap.DIBFormat.e_DIBRgb :
             pixel_format = PixelFormat.Format24bppRgb;
             break;
         case Bitmap.DIBFormat.e_DIBArgb:
             pixel_format = PixelFormat.Format32bppArgb;
             break;
         case Bitmap.DIBFormat.e_DIB8bpp :
             pixel_format = PixelFormat.Format8bppIndexed;
             break;
         case Bitmap.DIBFormat.e_DIBRgb32:
             pixel_format =PixelFormat.Format32bppRgb;
             break;
         // 根据需要添加其他PixelFormat的case  
         default:
             throw new NotSupportedException("Unsupported pixel format.");
     }
     System.Drawing.Bitmap  bitmap = new System.Drawing.Bitmap(_bitmap.GetWidth(), _bitmap.GetHeight(), pixel_format);
     Graphics bitmap_graphics = Graphics.FromImage(bitmap);
     bitmap_graphics.Clear(System.Drawing.Color.White);
     bitmap_graphics.Dispose();
     System.Drawing.Imaging.BitmapData bitmap_data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
                                                                                                                     System.Drawing.Imaging.ImageLockMode.ReadWrite, pixel_format);
     int buffer_size = _bitmap.GetPitch() * _bitmap.GetHeight();
     byte[] byte_string = new byte[buffer_size];
     System.Runtime.InteropServices.Marshal.Copy(_bitmap.GetBuffer(), byte_string, 0, buffer_size);
     System.Runtime.InteropServices.Marshal.Copy(byte_string, 0, bitmap_data.Scan0, buffer_size);
     bitmap.UnlockBits(bitmap_data);
     
     return bitmap;
 }

2. System.Drawing.Bitmap 转 foxit.common.Bitmap:

  static public Bitmap FoxitBitamp2SystemBitmap(System.Drawing.Bitmap bitmap)
    {
        
        int buffer_size = CalculateStride(bitmap) * bitmap.Height;

        Bitmap.DIBFormat dIBFormat = Bitmap.DIBFormat.e_DIBArgb;
        switch (bitmap.PixelFormat )
        {
            case PixelFormat.Format24bppRgb:
                dIBFormat = Bitmap.DIBFormat.e_DIBRgb;
                break;
            case PixelFormat.Format32bppArgb:
                dIBFormat = Bitmap.DIBFormat.e_DIBArgb;
                break;
            case PixelFormat.Format8bppIndexed:
                dIBFormat = Bitmap.DIBFormat.e_DIB8bpp;
                break;
            case PixelFormat.Format32bppRgb:
                dIBFormat = Bitmap.DIBFormat.e_DIBRgb32;
                break;
            // 根据需要添加其他PixelFormat的case  
            default:
                throw new NotSupportedException("Unsupported pixel format.");
        }
        byte[] byte_string = new byte[buffer_size];
        System.Drawing.Imaging.BitmapData bitmap_data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
                                                                                                                       System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);
        System.Runtime.InteropServices.Marshal.Copy(bitmap_data.Scan0, byte_string, 0, buffer_size);
      
        Bitmap _bitmap = new Bitmap(bitmap.Width, bitmap.Height, dIBFormat, bitmap_data.Scan0, CalculateStride(bitmap));
 bitmap.UnlockBits(bitmap_data);
        return _bitmap;
    }

  public static int CalculateStride(System.Drawing.Bitmap bitmap)
  {
      int width = bitmap.Width;
      PixelFormat format = bitmap.PixelFormat;

      // 计算每像素的字节数  
      int bytesPerPixel;
      switch (format)
      {
          case PixelFormat.Format24bppRgb:
              bytesPerPixel = 3;
              break;
          case PixelFormat.Format32bppArgb:
              bytesPerPixel = 4;
              break;
          // 根据需要添加其他PixelFormat的case  
          default:
              throw new NotSupportedException("Unsupported pixel format.");
      }
      int stride = width * bytesPerPixel;
 
      return stride;
  }