使用 GDI+ 将 24 bpp 转换为 4 bpp

2023-12-28

我的程序当前采用 4 bpp(每像素位数)TIFF 作为位图,将其转换为图形,添加一些文本字符串,然后再次将其保存为 TIFF 文件。默认情况下,输出 Bitmap.Save() TIFF 文件似乎为 24 bpp(无论输入如何),并且比原始 TIFF 大很多。

是否可以保留与输出输入相同的 4 bpp 调色板编码,如果不能,如何将位图像素格式从 24bpp 转换为 4 bpp 索引?

我在以下位置看到了将 24 bpp 转换为 1 bpp 的示例鲍勃·鲍威尔:锁定比特 https://web.archive.org/web/20140906075741/http://bobpowell.net/onebit.aspx但不知道如何做到 4 bpp。


这是我发布的课程的修改版本here https://stackoverflow.com/questions/2501104/outofmemoryexception-with-image-clone-only-on-windows-2003/2502605#2502605。它使用原始源站点评论中的 4bpp 逻辑。

Public Class BitmapEncoder
    ''' <summary>
    ''' Copies a bitmap into a 1bpp/4bpp/8bpp bitmap of the same dimensions, fast
    ''' </summary>
    ''' <param name="b">original bitmap</param>
    ''' <param name="bpp">1 or 8, target bpp</param>
    ''' <returns>a 1bpp copy of the bitmap</returns>
    Public Shared Function ConvertBitmapToSpecified(ByVal b As System.Drawing.Bitmap, ByVal bpp As Integer) As System.Drawing.Bitmap
        Select Case bpp
            Case 1
            Case 4
            Case 8
            Case Else
                Throw New ArgumentException("bpp must be 1, 4 or 8")
        End Select


        ' Plan: built into Windows GDI is the ability to convert
        ' bitmaps from one format to another. Most of the time, this
        ' job is actually done by the graphics hardware accelerator card
        ' and so is extremely fast. The rest of the time, the job is done by
        ' very fast native code.
        ' We will call into this GDI functionality from C#. Our plan:
        ' (1) Convert our Bitmap into a GDI hbitmap (ie. copy unmanaged->managed)
        ' (2) Create a GDI monochrome hbitmap
        ' (3) Use GDI "BitBlt" function to copy from hbitmap into monochrome (as above)
        ' (4) Convert the monochrone hbitmap into a Bitmap (ie. copy unmanaged->managed)

        Dim w As Integer = b.Width, h As Integer = b.Height
        Dim hbm As IntPtr = b.GetHbitmap()
        ' this is step (1)
        '
        ' Step (2): create the monochrome bitmap.
        ' "BITMAPINFO" is an interop-struct which we define below.
        ' In GDI terms, it's a BITMAPHEADERINFO followed by an array of two RGBQUADs
        Dim bmi As New BITMAPINFO()
        bmi.biSize = 40
        ' the size of the BITMAPHEADERINFO struct
        bmi.biWidth = w
        bmi.biHeight = h
        bmi.biPlanes = 1
        ' "planes" are confusing. We always use just 1. Read MSDN for more info.
        bmi.biBitCount = CShort(bpp)
        ' ie. 1bpp or 8bpp
        bmi.biCompression = BI_RGB
        ' ie. the pixels in our RGBQUAD table are stored as RGBs, not palette indexes
        bmi.biSizeImage = CUInt((((w + 7) And &HFFFFFFF8) * h / 8))
        bmi.biXPelsPerMeter = 1000000
        ' not really important
        bmi.biYPelsPerMeter = 1000000
        ' not really important
        ' Now for the colour table.
        Dim ncols As UInteger = CUInt(1) << bpp
        ' 2 colours for 1bpp; 256 colours for 8bpp
        bmi.biClrUsed = ncols
        bmi.biClrImportant = ncols
        bmi.cols = New UInteger(255) {}
        ' The structure always has fixed size 256, even if we end up using fewer colours
        If bpp = 1 Then
            bmi.cols(0) = MAKERGB(0, 0, 0)
            bmi.cols(1) = MAKERGB(255, 255, 255)
        ElseIf bpp = 4 Then
            bmi.biClrUsed = 16
            bmi.biClrImportant = 16
            Dim colv1 As Integer() = New Integer(15) {8, 24, 38, 56, 72, 88, 104, 120, 136, 152, 168, 184, 210, 216, 232, 248}

            For i As Integer = 0 To 15
                bmi.cols(i) = MAKERGB(colv1(i), colv1(i), colv1(i))
            Next
        ElseIf bpp = 8 Then
            For i As Integer = 0 To ncols - 1
                bmi.cols(i) = MAKERGB(i, i, i)
            Next
        End If
        ' For 8bpp we've created an palette with just greyscale colours.
        ' You can set up any palette you want here. Here are some possibilities:
        ' greyscale: for (int i=0; i<256; i++) bmi.cols[i]=MAKERGB(i,i,i);
        ' rainbow: bmi.biClrUsed=216; bmi.biClrImportant=216; int[] colv=new int[6]{0,51,102,153,204,255};
        '          for (int i=0; i<216; i++) bmi.cols[i]=MAKERGB(colv[i/36],colv[(i/6)%6],colv[i%6]);
        ' optimal: a difficult topic: http://en.wikipedia.org/wiki/Color_quantization
        ' 
        ' Now create the indexed bitmap "hbm0"
        Dim bits0 As IntPtr
        ' not used for our purposes. It returns a pointer to the raw bits that make up the bitmap.
        Dim hbm0 As IntPtr = CreateDIBSection(IntPtr.Zero, bmi, DIB_RGB_COLORS, bits0, IntPtr.Zero, 0)
        '
        ' Step (3): use GDI's BitBlt function to copy from original hbitmap into monocrhome bitmap
        ' GDI programming is kind of confusing... nb. The GDI equivalent of "Graphics" is called a "DC".
        Dim sdc As IntPtr = GetDC(IntPtr.Zero)
        ' First we obtain the DC for the screen
        ' Next, create a DC for the original hbitmap
        Dim hdc As IntPtr = CreateCompatibleDC(sdc)
        SelectObject(hdc, hbm)
        ' and create a DC for the monochrome hbitmap
        Dim hdc0 As IntPtr = CreateCompatibleDC(sdc)
        SelectObject(hdc0, hbm0)
        ' Now we can do the BitBlt:
        BitBlt(hdc0, 0, 0, w, h, hdc, _
         0, 0, SRCCOPY)
        ' Step (4): convert this monochrome hbitmap back into a Bitmap:
        Dim b0 As System.Drawing.Bitmap = System.Drawing.Bitmap.FromHbitmap(hbm0)
        '
        ' Finally some cleanup.
        DeleteDC(hdc)
        DeleteDC(hdc0)
        ReleaseDC(IntPtr.Zero, sdc)
        DeleteObject(hbm)
        DeleteObject(hbm0)
        '
        Return b0
    End Function


    Private Shared SRCCOPY As Integer = &HCC0020
    Private Shared BI_RGB As UInteger = 0
    Private Shared DIB_RGB_COLORS As UInteger = 0
    <System.Runtime.InteropServices.DllImport("gdi32.dll")> _
    Private Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean
    End Function

    <System.Runtime.InteropServices.DllImport("user32.dll")> _
    Private Shared Function GetDC(ByVal hwnd As IntPtr) As IntPtr
    End Function

    <System.Runtime.InteropServices.DllImport("gdi32.dll")> _
    Private Shared Function CreateCompatibleDC(ByVal hdc As IntPtr) As IntPtr
    End Function

    <System.Runtime.InteropServices.DllImport("user32.dll")> _
    Private Shared Function ReleaseDC(ByVal hwnd As IntPtr, ByVal hdc As IntPtr) As Integer
    End Function

    <System.Runtime.InteropServices.DllImport("gdi32.dll")> _
    Private Shared Function DeleteDC(ByVal hdc As IntPtr) As Integer
    End Function

    <System.Runtime.InteropServices.DllImport("gdi32.dll")> _
    Private Shared Function SelectObject(ByVal hdc As IntPtr, ByVal hgdiobj As IntPtr) As IntPtr
    End Function

    <System.Runtime.InteropServices.DllImport("gdi32.dll")> _
    Private Shared Function BitBlt(ByVal hdcDst As IntPtr, ByVal xDst As Integer, ByVal yDst As Integer, ByVal w As Integer, ByVal h As Integer, ByVal hdcSrc As IntPtr, _
     ByVal xSrc As Integer, ByVal ySrc As Integer, ByVal rop As Integer) As Integer
    End Function


    <System.Runtime.InteropServices.DllImport("gdi32.dll")> _
    Private Shared Function CreateDIBSection(ByVal hdc As IntPtr, ByRef bmi As BITMAPINFO, ByVal Usage As UInteger, ByRef bits As IntPtr, ByVal hSection As IntPtr, ByVal dwOffset As UInteger) As IntPtr
    End Function

    <System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)> _
    Private Structure BITMAPINFO
        Public biSize As UInteger
        Public biWidth As Integer, biHeight As Integer
        Public biPlanes As Short, biBitCount As Short
        Public biCompression As UInteger, biSizeImage As UInteger
        Public biXPelsPerMeter As Integer, biYPelsPerMeter As Integer
        Public biClrUsed As UInteger, biClrImportant As UInteger
        <System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=256)> _
        Public cols As UInteger()
    End Structure

    Private Shared Function MAKERGB(ByVal r As Integer, ByVal g As Integer, ByVal b As Integer) As UInteger
        Return CUInt((b And 255)) Or CUInt(((r And 255) << 8)) Or CUInt(((g And 255) << 16))
    End Function
    Private Sub New()

    End Sub

End Class

使用方法:

    'Load your image
    Using B As New Bitmap("c:\test.tiff")
        'Do a bunch of stuff to it
        '...'

        'Convert it to 4BPP
        Using I = BitmapEncoder.ConvertBitmapToSpecified(B, 4)
            'Save to disk
            I.Save("c:\test2.tiff")
        End Using
    End Using
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 GDI+ 将 24 bpp 转换为 4 bpp 的相关文章

  • 装配和产品版本不匹配

    我正在尝试在 asp net 网站中使用 Ajax 控件工具包 我从之前的一个示例项目中复制了 dll 它有以下详细信息 Assembly Version 3 5 40412 0 File Version 3 5 40412 2 Inter
  • view.getDrawingCache() 在 Android API 28 中已弃用

    在 Android API 28 中view getDrawingCache 已被弃用 是否有任何更新的解决方案来生成 android 中特定视图的位图 获取视图位图的两种方法 使用画布 使用像素 API Canvas Java Bitma
  • 使用 Rijndael 解密文本文件

    我用过本指南 https www codeproject com Articles 12092 Encrypt Decrypt Files in VB NET Using Rijndael加密的值txtCode Text到一个文本文件中 然
  • 如何将字节数组转换为图像 [kotlin]

    我想将字节数组转换为图像并在图像视图中显示它 但不知道该怎么做 有人可以指导我吗 我使用这个函数将位图转换为字节数组 private fun BitmapToByteArray ByteArray val stream ByteArrayO
  • 多选DataGridView没有CTRL键没有闪烁?

    我在表单 Form1 vb 上有一个 DataGridView 控件 需要允许用户在不使用 CTRL 键的情况下多选行 没有可用的键盘 他们使用触摸屏 我已启用 mutliselect 属性 并在 Form 类中包含以下代码 我的 Data
  • 需要帮助将 C# 转换为 VB

    我正在看this https web archive org web 20200815114302 http geekswithblogs net NewThingsILearned archive 2008 01 16 listcolle
  • 绘制大位图时 nSyncAndDrawFrame 速度极慢

    我想用多个大位图优化视差滚动视图 在我的 Nexus 5 上 一切都很顺利 Traceview 转储如下所示 doFrame 方法大约需要 18 毫秒才能完成 但是 当使用我的 Nexus 7 或 Android 6 模拟器 Genymot
  • 使用 GDI+ 和 C++ 减少闪烁

    我在 C MFC 应用程序中使用 GDI 每当调整窗口大小时 我似乎都无法避免闪烁 我已经尝试过以下步骤 返回 TRUEOnEraseBkGnd 返回 NULLOnCtlColor 根据此代码使用双缓冲 void vwView OnDraw
  • 使用 VB.NET 覆盖文本文件中的特定行

    我需要执行以下操作 更改文本文件中的行 Path c this certain path 用这条线 Path c that other newer path 这些路径的长度肯定会不同 因此我需要替换引号中的内容 或者完全擦除该行并输入一个新
  • 通过 Tab 键浏览 XML 文档字段

    In VB NET you can move through the fields in the XML member documentation with the Tab key 这在 C 中不起作用 还有其他方法吗 除了用鼠标将光标放在
  • 使用 .NET 将二进制转换器转换为十进制

    我已经开始掌握一些编程基础知识 但我仍然很新并且缺乏经验 我正在编写的新程序遇到问题 我想要一个程序 将 8 位二进制数放入文本框中 按下按钮 然后显示二进制数的十进制值 下面是我尝试过的代码 Public Class Form1 Priv
  • 获取 Blob 图像并将该图像转换为 Bitmap 图像

    我正在从数据库中获取 blob 格式的图像 我想将其转换为位图图像 我用来将位图转换为 Blob 的代码放在下面 但是请告诉我如何反转它 ByteArrayOutputStream boas new ByteArrayOutputStrea
  • 为 winforms ComboBox 中的单个项目着色?

    我遇到了一个困境 我有一个表单 其中包含许多组合框 其中包含在某些情况下可能无效 过时的信息 选项 项目 我不能简单地从项目中删除过时的信息 但我确实想在选项无效时为用户提供视觉线索 我正在考虑对项目进行着色 可能是红色 来指示它们是否无效
  • 用户完成后关闭 Excel

    任务非常简单 我想从 VB net GUI 打开 Excel 文档 xls 用户将处理 Excel 文件 用户完成后关闭 Excel 文件 我想要VB net代码释放Excel对象 问题是 当用户关闭文件时 Excel 对象仍然可以在任务管
  • 通过 VB.NET 和 C# 中的 Ref 参数

    我有与传递参数 byRef 相关的问题 我有基于 VB NET 的类库 其中一些函数是使用 byref 参数类型定义的 这些参数是父类对象 当我尝试调用此函数并在 byref 参数中传递子类对象时 它在 VB NET 中工作 但我无法在 C
  • 比较两个字符串ArrayList

    我有两个数组列表 dim Colors1 New ArrayList Colors1 Add Blue Colors1 Add Red Colors1 Add Yellow Colors1 Add Green Colors1 Add Pur
  • 正则表达式基于组的不同替换?

    所以我对正则表达式比较陌生 并且做了一些练习 我正在玩一个简单的 混淆器 它只是寻找 dot or dot or at or at 不区分大小写 并且在匹配项之前或之后有或没有任意数量的空格 这是针对通常情况的 someemail AT d
  • vb.net中如何读取串口数据?

    我创建了一个类 有一个名为 SendUSSD 的子类 当调用它时 它会向连接 gsm 手机的 COM 端口发送一个 ussd 代码 如 123 此 usd 应该返回移动余额 If IsOpen True Then checks if the
  • 自动加载 linq2entities 中的关系

    当我的模型中的两个实体之间存在关系时 组成员 1 用户 并尝试使用 LINQ 从该关系中选择项目 从 user GroupMember 中的实体选择实体 除非我首先使用以下语句加载关系 否则我总是得到空结果 user GroupMember
  • Nothing = String.Empty (为什么它们相等?)

    为什么第一个 if 语句的计算结果为 true 我知道如果我使用 is 而不是 那么它的计算结果不会为 true 如果我将 String Empty 替换为 Foo 它的计算结果不会为 true String Empty 和 Foo 都具有

随机推荐

  • 如何在生成 PDF 文件下载时显示进度条,而不出现 IE“下载”警告

    我已经在客户端和服务器之间进行了通信 过程很简单 我正在向控制器发出 AJAX POST 请求 该控制器在文件系统中生成 pdf 上述请求成功后 将发出表单 GET 请求以取回 pdf 并打开标准浏览器 保存或打开 对话框 我之所以采用两步
  • C - fwrite 大于 4GB 的二进制文件

    我基本上是 C 新手 我有一个 64 位 Windows 7 配备 64GB RAM 和 240GB SSD 我使用的采集板将采集的数据存储在 2 个内部 FIFO 中 然后将数据传递到 RAM 这样我就有可能采集 60 GB 的数据 我无
  • 部署时字体未加载

    我创建了一个有角度的应用程序 它在本地环境中加载得非常好 所有样式和字体都被应用 但是当我部署它时 字体没有加载 无法弄清楚为什么 它没有在控制台中显示任何错误或警告消息 我正在导入所有 css 样式angular json文件如下 dem
  • Factorial 函数在 Python 中工作,对于 Julia 返回 0

    我在Python中定义了一个阶乘函数 如下所示 def fact n if n 1 return n else return n fact n 1 print fact 100 在 Julia 中如下 function fact n if
  • 更改日期格式javascript

    我正在从两个不同的 API 中提取一些数据 稍后我想要获取这些对象 但是 我得到两种不同的日期格式 这种格式 1427457730 和这种格式 2015 04 10T09 12 22Z 如何更改其中之一的格式 以便我可以使用相同的格式 ea
  • MSSQLSM 2008 奇怪的登录在 select 语句上失败

    我的SQL服务器上有多种用户 所有用户都可以登录服务器 然而 一个用户一旦登录就无法在特定视图上运行任何操作 选择 更改 编辑 但可以在所有其余视图上运行 用户设置与具有完全访问权限的另一个帐户相同 说实话 它们是相同的 对于无法从他的视图
  • Apollo 的 MockedProvider 不为 withApollo 中包装的组件提供客户端

    在测试 withApollo 中包装的组件时 我使用 Apollo 文档中指定的 Apollo 的 MockedProvider 但是当酶尝试渲染该组件时 渲染器找不到clientMockedProvider 应该已经提供了 我有一个使用的
  • 如何使用jquery检查某个元素是否在用户视图中

    我有一个很大的可拖动对象div在我的窗户里 这div有一个较小的窗口 div style width 500px height 500px div style width 100 height 5000px ul li li li li l
  • WCF netTCPBinding - 传输加密是否足够?

    我有一个处理一些敏感数据的 WCF 服务 我想确保不暴露这些数据 所以我正在考虑 netTCPBinding 主要是因为我可以控制它运行的网络 并且性能是重中之重 我认识到有两个领域可以加密 传输级别和消息级别 我打算使用证书在传输级别进行
  • 获取每个字符出现的次数

    给定字符串 a dqdwqfwqfggqwq 如何获取每个字符出现的次数 在2 7和3 1中有一个叫做Counter的工具 gt gt gt import collections gt gt gt results collections C
  • Winform 消息框中可点击的 URL?

    我想在消息框中显示帮助链接 默认情况下 文本显示为不可选择的字符串 一种选择是在消息框中显示 url 以及一条消息 并提供帮助按钮 将您带到该 url MessageBox Show test message caption Message
  • 如何使用 CSS 将跨度更改为看起来像 pre?

    是否可以更改 span 标签 或 div 将其内容预格式化为 pre 标签只使用 CSS 吗 看着那 这W3C CSS2 1 默认样式表 https www w3 org TR CSS21 sample html or the CSS2 2
  • AJAX向WebApi传递多个参数

    AJAX 请求 ajax url url dataType json type Post data token 4 feed id 0 message Hello World userId 4 服务器端 Web API HttpPost p
  • 使用外部框架构建 Objective-C 应用程序

    我正在整合growl http growl info documentation developer implementing growl php lang cocoa进入我的 Objective C 应用程序 但是 如果我构建并运行我的应
  • C、硬件抽象层中“extern”类型的变量

    我正在研究硬件抽象层 该 HAL 的目的是在 Linux 驱动程序和 MCU 驱动程序之间轻松切换 我正在研究SPI接口 下面是 打开 SPI接口的HAL函数的签名 哈尔 spi h spi handle t spi open spi po
  • 将多个通用接口传递给一个方法

    我试图将多个通用接口作为参数传递给我的类之一的构造函数 以下代码无法编译 public interface IPosterGenerator
  • 如何使用Gradle将Android配置注入到每个子项目中?

    而不是在每个子项目中复制 android 配置块 android compileSdkVersion 19 buildToolsVersion 19 0 0 defaultConfig minSdkVersion 9 targetSdkVe
  • 为什么使用“set var = value”获取脚本会破坏 $@?

    我正在尝试在 Centos 虚拟机上配置自动注销 我注意到 如果我在 etc profile d autologout sh 创建一个文件 仅set autologout 30在文件中 然后它会中断为任何源自 etc profile 的脚本
  • 如何通过拖动来调整 PyQt 小部件的大小?

    我有一个 QScrollArea 其中包含一个带有 QVBoxLayout 的小部件 该布局内还有其他几个小部件 我希望用户能够拖动这些小部件的下边框以在垂直方向上调整它们的大小 当它们调整大小时 我不希望它们从滚动区域中的其他小部件 窃取
  • 使用 GDI+ 将 24 bpp 转换为 4 bpp

    我的程序当前采用 4 bpp 每像素位数 TIFF 作为位图 将其转换为图形 添加一些文本字符串 然后再次将其保存为 TIFF 文件 默认情况下 输出 Bitmap Save TIFF 文件似乎为 24 bpp 无论输入如何 并且比原始 T