当前位置:编程学习 > VB >>

VB的图像处理

VB中使用API或GDI将上图转换为下图(背景和前景色反转,前景图形只要边框),要求速度快,使用VB自带PSET的方式不要来了,速度太慢了。最好能给详细程序,谢谢了。

补充:我需要的是实现如上面图所述的转换功能,需要具体程序呀,函数如下格式:

Function 图像转换(Picture1,Picture2) '将图片1转换后显示在图片2。

需要的API函数等都可以不要声明,我自己可以查(当然有就更好了)

麻烦各位高手帮帮忙呀。

追问:我需要的是实现转换的代码。谢谢
答案:过程一:获得一个在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 '用于记录输入图像的宽度
Dim bi24BitInfo As BitMapInfo '定义BMP信息

可以看出,我在输入和输出中使用了两个不同的动态数组ColVal()和ColOut()
这么做是有道理的,因为我们不只是为了输入和输出图像,中间还要对像素进行处理。
包括图像缩放、色彩调整、锐化、柔化等等处理,使用两个不同的数组来分别存放数据更有利于程序的实现。

有些性急的朋友说不定已经把程序贴到工程里试用了,可是会发现根本不能输出图像。
这是因为当你用DIBGET获得的图像还在ColVal() 中呢,需要把它们放到ColOut()这个数组中去,DIBPUT这个过程才能起作用。

这里再给出一个用于数组整体移动数据的过程:
Public Sub CopyData(ByVal W As Long, ByVal H As Long)
Dim Length As Long
Dim I As Long
Dim L As Long
I = Bits \ 8
L = I - 1
Length = (W + 1&) * (H + 1&) * I
ReDim ColOut(L, W, H)
CopyMemory ColOut(0, 0, 0), ColVal(0, 0, 0), Length
End sub

API定义:
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSrc As Any, ByVal ByteLen As Long)

这时,我们就可以来试一下效果了:
把你的显示器调到32位色。
将前面的所有API和变量定义全部贴到一个新建的模块里
新建一个窗体,加两个PICTURE控件:pictrue1 ,picture2 一个按钮command1
在pictrue1中加载一个图片
在command1中写如下代码:
sub command1_click()
With picture1
.ScaleMode=3
.BorderStyle=0
DibGet .hdc,0,0,.scalewidth,.scaleheight
End With
CopyData InPutHei ,InPutWid
picture2.AutoRedraw=True
DibPut picture2.hdc
picture2.refresh
end sub


运行一下,按钮按下,pictreu1中的图片就立刻显示到了picture2中。

这时,你可能会说,弄了这么半天就贴个图?用PaintPicture不是就可以了吗?
不错,如果只是要贴个图,确实不用这么麻烦,可是,我们后面要说的图像处理部分将会用到前门得到的像素值。所以,这只是一个开始,我真正要讲的东西还在后面呢。请大家继续关注。

本文来自CSDN博客,转载请标明出处: http://blog.csdn.net/wallescai/archive/2004/12/31/235270.aspx

上一个:计算机2级VB
下一个:怎样在VB中添加工具条

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,