跳到内容
  • 福昕首页
  • 开发中心
  • SDK文档资料
  • 福昕首页
  • 开发中心
  • SDK文档资料
申请试用
  • 企业自动化
    • Compressor
  • 福昕CloudAPI
  • 福昕PDF SDK 软件开发工具包
    • 福昕PDF SDK(ActiveX)
    • 福昕PDF SDK(桌面/服务器)
    • 福昕PDF SDK(Plug-in)
    • 福昕 PDF SDK(安卓)
    • 福昕PDF SDK(iOS)
    • 福昕PDF SDK(Web)
  • 福昕管理控制台
    • 公有云
    • 私有云
    • 通用情况
  • 福昕阅读器
    • RMS插件
  • 福昕高级编辑器
    • AI助手
    • Mac版本
      • 常规问题
    • windows版本
      • ECM集成
      • 互联PDF
      • 企业管理指南
      • 保护
      • 内容编辑
      • 创建PDF
      • 压缩
      • 图章
      • 安装与卸载
      • 常见问题
      • 打印
      • 注释/评论
      • 福昕插件
      • 翻译助手
      • 翻译助手教程
      • 试用与激活
      • 转换
      • 页面管理
    • 教育用户
      • 论文查重
      • 论文畅
    • 网页版
      • 电子签章
    • 订阅
    • 资源
  • 福昕高级编辑器Linux版本
  • 福船图纸管理系统
  • 福昕PDF SDK 软件开发工具包 > 福昕PDF SDK(桌面/服务器)
  • 标签:

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

  • 福昕知识库
  • 2024-09-04

从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;
  }

相关内容

【校园昕积分】邀请攻略:使用福昕合作高校发放教育专用激活码激活

【校园昕积分】邀请攻略:使用CARSI高校账户或高校EDU邮箱注册

在福昕高级PDF编辑器中如何自定义快速访问工具栏

在福昕高级PDF编辑器中如何设置常用图章

在对文件虚拟打印后如何设置文件不自动打开

通知消息关闭后如何开启

PDF文件奇偶页差异化页码添加

Web SDK 开发实战:动态自定义 PDF 注释右键菜单的 JavaScript 实现方案

无法安装服务“Foxit Document Management”(FoxitAssistantSvcPlus)

将3D文件添加作为信任文件

推荐内容

【校园昕积分】邀请攻略:使用福昕合作高校发放教育专用激活码激活

【校园昕积分】邀请攻略:使用CARSI高校账户或高校EDU邮箱注册

在福昕高级PDF编辑器中如何自定义快速访问工具栏

在福昕高级PDF编辑器中如何设置常用图章

在对文件虚拟打印后如何设置文件不自动打开

通知消息关闭后如何开启

PDF文件奇偶页差异化页码添加

Web SDK 开发实战:动态自定义 PDF 注释右键菜单的 JavaScript 实现方案

无法安装服务“Foxit Document Management”(FoxitAssistantSvcPlus)

将3D文件添加作为信任文件

产品
  • 应用行业
  • 白皮书
开发支持
  • 开发中心
  • SDK文档资料

销售咨询:010-50951668

客服电话:0591-38509808

销售咨询
微信公众号

©2025 福建福昕软件开发股份有限公司 版权所有

隐私策略