要解决或说确定 3 个问题:1 在文本文件中如何显示图片?用点,还是用其他字符?也就是说一个像素对于一个文本字符还是 X * X 的像素阵对应一个字符?其区别是转换算法不同,后者复杂一些。2 文本文件中应该是黑白的吧?要考虑彩色图片的二值化转换问题。实际上,只要设置一个灰度阈值,将彩色像素换算的灰度值与之比较,浅的 = 0(白),深的 = 1(黑)。灰度换算的公式: GrayScale = (77 * R + 150 * G + 28 * B) / 255如果文本图片是有灰度的,例如用不同的字符代表不同的灰度区域,只要算出一个像素阵的平均灰度,查找最接近的字符就可以了。3 较快地获取 PictureBox 中的像素 要处理一个图像,首先要获得该图像的像素值,而VB本身提供的PICTURE控件虽然可以打开很多类型的图片,但是它提供的那个POINT方法读取像素实在是太慢。而使用GetPixel这个API的速度也快不到哪里去,因为PIONT方法本身就是对于GetPixel的一个包装。 在VB中要快速获取一幅在PICTURE中打开的图像比较快速的方法是使用DIB方法,当然还有DDB方法,不过使用DDB方法还需要考虑不同颜色深度的图像的分别处理,在程序的实现上要相对复杂,而使用DIB方法则不必,并且在处理速度上比DDB方法也慢的有限。API定义: 删除一个DC Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long 删除一个对象 Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long 选择当前对象 Private Declare Function GetCurrentObject Lib "gdi32" (ByVal hdc As Long, ByVal uObjectType As Long) As Long 获取DIB Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BitMapInfo, ByVal wUsage As Long) As Long数据结构定义: Private Type BitMapInfoHeader '文件信息头——BITMAPINFOHEADER biSize As Long biWidth As Long biHeight As Long biPlanes As Integer biBitCount As Integer biCompression As Long biSizeImage As Long biXPelsPerMeter As Long biYPelsPerMeter As Long biClrUsed As Long biClrImportant As Long End TypePrivate Type RGBQuad rgbBlue As Byte rgbGreen As Byte rgbRed As Byte 'rgbReserved As Byte End TypePrivate Type BitMapInfo bmiHeader As BitMapInfoHeader bmiColors As RGBQuad End Type过程中用到的全局变量: Private Const Bits As Long = 32 '颜色深度,这里把所有图像都按照32位来处理 Public Done As Boolean '用于标记一个过程是否结束 Dim ColVal() As Byte '用于存放从DIB输入的像素值 Dim ColOut() As Byte '用于存放向DIB输出的像素值 Dim InPutHei As Long '用于记录输入图像的高度 Dim InPutWid As Long '用于记录输入图像的宽度 Dim bi24BitInfo As BitMapInfo '定义BMP信息Public Sub DibGet(ByVal IdSource As Long, XBegin As Long, ByVal YBegin As Long, ByVal XEnd As Long, ByVal YEnd As Long) Dim iBitmap As Long Dim iDC As Long Dim I As LongDim Dim W As Long Dim H As LongOn Error GoTo ErrLine Done = False InPutWid = XEnd - XBegin InPutHei = YEnd - YBegin W = InPutWid + 1 H = InPutHei + 1I = (Bits \ 8) - 1 ReDim ColVal(I, InPutWid, InPutHei) With bi24BitInfo.bmiHeader .biBitCount = Bits .biCompression = 0& .biPlanes = 1 .biSize = Len(bi24BitInfo.bmiHeader) .biWidth = W .biHeight = H End WithiBitmap = GetCurrentObject(IdSource, 7&) GetDIBits IdSource, iBitmap, 0&, H, ColVal(0, 0, 0), bi24BitInfo, 0& DeleteObject iBitmap Done = True ErrLine: MsgBox "错误号:" & Err.Number & ":" & Err.Description End Sub处理像素时: For i = 0 To InPutWid R = ColVal(0, i, 0) G = ColVal(1, i, 0) B = ColVal(2, i, 0) ...... Next i
本来我是没有看懂题的,不知楼主有没有时间解释一下?
GrayScale = (77 * R + 150 * G + 28 * B) / 255如果文本图片是有灰度的,例如用不同的字符代表不同的灰度区域,只要算出一个像素阵的平均灰度,查找最接近的字符就可以了。3 较快地获取 PictureBox 中的像素
要处理一个图像,首先要获得该图像的像素值,而VB本身提供的PICTURE控件虽然可以打开很多类型的图片,但是它提供的那个POINT方法读取像素实在是太慢。而使用GetPixel这个API的速度也快不到哪里去,因为PIONT方法本身就是对于GetPixel的一个包装。
在VB中要快速获取一幅在PICTURE中打开的图像比较快速的方法是使用DIB方法,当然还有DDB方法,不过使用DDB方法还需要考虑不同颜色深度的图像的分别处理,在程序的实现上要相对复杂,而使用DIB方法则不必,并且在处理速度上比DDB方法也慢的有限。API定义:
删除一个DC
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
删除一个对象
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
选择当前对象
Private Declare Function GetCurrentObject Lib "gdi32" (ByVal hdc As Long, ByVal uObjectType As Long) As Long
获取DIB
Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BitMapInfo, ByVal wUsage As Long) As Long数据结构定义:
Private Type BitMapInfoHeader '文件信息头——BITMAPINFOHEADER
biSize As Long
biWidth As Long
biHeight As Long
biPlanes As Integer
biBitCount As Integer
biCompression As Long
biSizeImage As Long
biXPelsPerMeter As Long
biYPelsPerMeter As Long
biClrUsed As Long
biClrImportant As Long
End TypePrivate Type RGBQuad
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
'rgbReserved As Byte
End TypePrivate Type BitMapInfo
bmiHeader As BitMapInfoHeader
bmiColors As RGBQuad
End Type过程中用到的全局变量:
Private Const Bits As Long = 32 '颜色深度,这里把所有图像都按照32位来处理
Public Done As Boolean '用于标记一个过程是否结束
Dim ColVal() As Byte '用于存放从DIB输入的像素值
Dim ColOut() As Byte '用于存放向DIB输出的像素值
Dim InPutHei As Long '用于记录输入图像的高度
Dim InPutWid As Long '用于记录输入图像的宽度
Dim bi24BitInfo As BitMapInfo '定义BMP信息Public Sub DibGet(ByVal IdSource As Long, XBegin As Long, ByVal YBegin As Long, ByVal XEnd As Long, ByVal YEnd As Long)
Dim iBitmap As Long
Dim iDC As Long
Dim I As LongDim
Dim W As Long
Dim H As LongOn Error GoTo ErrLine
Done = False
InPutWid = XEnd - XBegin
InPutHei = YEnd - YBegin
W = InPutWid + 1
H = InPutHei + 1I = (Bits \ 8) - 1
ReDim ColVal(I, InPutWid, InPutHei)
With bi24BitInfo.bmiHeader
.biBitCount = Bits
.biCompression = 0&
.biPlanes = 1
.biSize = Len(bi24BitInfo.bmiHeader)
.biWidth = W
.biHeight = H
End WithiBitmap = GetCurrentObject(IdSource, 7&)
GetDIBits IdSource, iBitmap, 0&, H, ColVal(0, 0, 0), bi24BitInfo, 0&
DeleteObject iBitmap
Done = True
ErrLine:
MsgBox "错误号:" & Err.Number & ":" & Err.Description
End Sub处理像素时:
For i = 0 To InPutWid
R = ColVal(0, i, 0)
G = ColVal(1, i, 0)
B = ColVal(2, i, 0)
......
Next i
注意像素数据的参数是:
ColVal(RGB, X, Y)