VB图像处理之像素的获取和输出
2008-02-23 06:48:15来源:互联网 阅读 ()
在VB中要快速获取一幅在PICTURE中打开的图像比较快速的方法是使用DIB方法,当然还有DDB方法,不过使用DDB方法还需要考虑不同颜色深度的图像的分别处理,在程序的实现上要相对复杂,而使用DIB方法则不必,并且在处理速度上比DDB方法也慢的有限。
过程一:获得一个在PICTURE控件中打开的图像的所有像素。
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 Long
On Error GoTo ErrLine
Done = False
TimeGet = timeGetTime
InPutWid = XEnd - XBegin
InPutHei = YEnd - YBegin
W = InPutWid 1
H = InPutHei 1
I = (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 With
iBitmap = GetCurrentObject(IdSource, 7&)
GetDIBits IdSource, iBitmap, 0&, H, ColVal(0, 0, 0), bi24BitInfo, 0& DeleteObject iBitmap
Done = True
TimeGet = timeGetTime - TimeGetExit Sub
ErrLine:
MsgBox "错误号:" & Err.Number & ":" & Err.Description
End Sub
在这个过程中所用到的只是一些参数的设定和API的调用,不涉及算法。
过程二:图像输出的过程:
Public Sub DIBPut(ByVal IdDestination As Long)
Dim W As Long
Dim H As Long
On Error GoTo ErrLine
Done = False
TimePut = timeGetTime
W = OutPutWid 1
H = OutPutHei 1
With bi24BitInfo.bmiHeader
.biWidth = W
.biHeight = H
LineBytes = ((W * Bits 31) And &HFFFFFFE0) \ 8
.biSizeImage = LineBytes * H
End With
SetDIBitsToDevice IdDestination, 0, 0, W, H, 0, 0, 0, H, ColOut(0, 0, 0), bi24BitInfo.bmiHeader, 0
Done = True
TimePut = timeGetTime - TimePut
Exit Sub
ErrLine:
MsgBox Err.Description
End Sub
下面解释一下在过程中到的全局变量和数据结构,以及API的定义。
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 Declare Function timeGetTime Lib "winmm.dll" () 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 Type
Private Type RGBQuad
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
'rgbReserved As Byte
End Type
Private Type BitMapInfo
bmiHeader As BitMapInfoHeader
bmiColors As RGBQuad
End Type
这三个数据结构都是在DIB中不可缺少的。我们不必深究,只是按照顺序复制粘贴直接使用就是了。
过程中用到的全局变量:
Private Const Bits As Long = 32 '颜色深度,这里把所有图像都按照32位来处理
Public Done As Boolean '用于标记一个过程是否结束
Public TimeGet As Long '用于记录输入过程处理所花费的时间
Public TimePut As Long '用于记录输出过程处理所花费的时间
Dim ColVal() As Byte '用于存放从DIB输入的像素值
Dim ColOut() As Byte '用于存放向DIB输出的像素值
Dim InPutHei As Long '用于记录输入图像的高度
Dim InPutWid As Long '用于记录输入图像的宽度
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:用VB写“多线程”程序
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash