2024年5月31日发(作者:)

VB代码VB小程序:将彩色图像转变为黑白图像

本程序使用两种方法将一幅彩色图像转变为黑白图像:用 API 方法、用 VB 控件方法。通

过比较两种方法不难发现:

用 VB 控件进行转换,过程直观,代码好理解,对学习和理解 VB 绘图语句很有帮助,但速

度慢。

用 API 方法进行转换,需操作二进制数组,像素点的行列定位较复杂,但转换速度快,几

乎是瞬间就完成了转换。

' '以下是窗体代码,在 VB6 调试通过

'需在窗体放置 5 个控件:Command1、Command2、Command3、Picture1、Text1

'本人原创,转载请注明文章来源:

/100bd/blog/item/

Dim ctExit As Boolean

Private Type BitMap

bmType As Long '图像类型:0 表示是位图

bmWidth As Long '图像宽度(像素)

bmHeight As Long '图像高度(像素)

bmWidthBytes As Long '每一行图像的字节数

bmPlanes As Integer '图像的图层数

bmBitsPixel As Integer '图像的位数

bmBits As Long '位图的内存指针

End Type

Private Declare Function GetObject Lib "gdi32" Alias

"GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As

Any) As Long

Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As

Long, ByVal dwCount As Long, lpBits As Any) As Long

Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As

Long, ByVal dwCount As Long, lpBits As Any) As Long

Private Sub Form_Load()

n = "转变为黑白图片"