PhotoShop的自动色阶功能的实现
2009-06-01 15:25:04 来源:WEB开发网为了大家理解方便,这里我们设置图像的数据为一二维的RGBQUAD结构体,该结构体由四个分量组成,分别表示了颜色空间的Red,Green,Blue,Alpha分量(有的地方最后一个变量名为Reserved,但这不妨碍我们)。其中的Alpha因为VB的StdPicture对象不支持,我们不予以关心。
图片看不清楚?请点击这里查看原图(大图)。
好了,下面给出获取PictureBox控件中图像数据的函数,以及怎么样把图像数据绘制到PictureBox中去。
模块中的代码:
Option Explicit
'***************************模仿PS的自动色阶功能*********************************
'** 作者 : laviewpbt
'** 开发时间 : 2008.7.1
'** 最后修改时间 : 2008.8.28
'** 联系方式 : QQ:33184777
'** E-MAIL : laviewpbt@sina.com
'** All Rights Resered
'***********************************************************************
Public Type BITMAPFILEHEADER
bfType As Integer
bfSize As Long
bfReserved1 As Integer
bfReserved2 As Integer
bfOffBits As Long
End Type
Public Type Bitmap
bmType As Long
bmWidth As Long
bmHeight As Long
bmWidthBytes As Long
bmPlanes As Integer
bmBitsPixel As Integer
bmBits As Long
End Type
Public Type BITMAPINFOHEADER '40 bytes
biSize As Long 'BITMAPINFOHEADER结构的大小
biWidth As Long
biHeight As Long
biPlanes As Integer '设备的为平面数,现在都是1
biBitCount As Integer '图像的颜色位图
biCompression As Long '压缩方式
biSizeImage As Long '实际的位图数据所占字节
biXPelsPerMeter As Long '目标设备的水平分辨率
biYPelsPerMeter As Long '目标设备的垂直分辨率
biClrUsed As Long '使用的颜色数
biClrImportant As Long '重要的颜色数。如果该项为0,表示所有颜色都是重要的
End Type
Public Type RGBQUAD '只有bibitcount为1,2,4时才有调色板
Blue As Byte '蓝色分量
Green As Byte '绿色分量
Red As Byte '红色分量
Reserved As Byte '保留值
End Type
Public Type BITMAPINFO
bmiHeader As BITMAPINFOHEADER
bmiColors As RGBQUAD
End Type
Public Const BI_RGB = 0&
Public Const DIB_RGB_COLORS = 0&
Public Const IMAGE_BITMAP = 0&
Public 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
Public Declare Function GetGDIObject Lib "gdi32.dll" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, ByRef lpObject As Any) As Long
Public Declare Function SetDIBits Lib "gdi32" (ByVal Hdc 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
Public Declare Function GetTickCount Lib "kernel32" () As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpDst As Any, lpSrc As Any, ByVal ByteLength As Long)
Public Declare Function VarPtrArray Lib "msvbvm60.dll" Alias "VarPtr" (ByRef Ptr() As Any) As Long
'得到图像数据
Public Function GetPicData(Pic As PictureBox, PicData() As RGBQUAD) As Boolean '读取位图数据
Dim Bmp As Bitmap, BmpInfo As BITMAPINFO
GetGDIObject Pic.Picture.Handle, Len(Bmp), Bmp '取得对指定对象进行说明的一个结构,
'hobject为位图,刷子等的句柄,
'count欲取回的字节数。通常是由lpObject定义的那个结构的长度
With BmpInfo.bmiHeader
.biSize = Len(BmpInfo.bmiHeader)
.biWidth = Bmp.bmWidth
.biHeight = -Bmp.bmHeight 'BMP位图默认的扫描方式是从下到上,
'这与GDI的坐标系统相反,为方便,这里
'改为-值,则取得数据就是以左上角为起点
.biPlanes = 1
.biBitCount = 32 '32位位图,默认情况下Windows不会处理最高8位
'可以将它作为自己的Alpha通道,取为32位位图,
'虽然多占用了点内存,但是避免了扫描行宽度问题,代码易于理解
.biCompression = BI_RGB '无压缩
.biSizeImage = Bmp.bmWidth * Bmp.bmHeight * 4
End With
ReDim PicData(0 To Bmp.bmWidth - 1, 0 To Bmp.bmHeight - 1) As RGBQUAD
GetDIBits Pic.Hdc, Pic.Image, 0, Bmp.bmHeight, PicData(0, 0), BmpInfo, DIB_RGB_COLORS
End Function
'根据数组显示图像
Public Sub SetPicData(Pic As PictureBox, PicData() As RGBQUAD) '显示位图
Dim Bmp As Bitmap, BmpInfo As BITMAPINFO
GetGDIObject Pic.Picture.Handle, Len(Bmp), Bmp '取得对指定对象进行说明的一个结构,hobject为位图,刷子等的句柄,count欲取回的字节数。通常是由lpObject定义的那个结构的长度
With BmpInfo.bmiHeader
.biSize = Len(BmpInfo.bmiHeader)
.biWidth = Bmp.bmWidth
.biHeight = -Bmp.bmHeight 'BMP位图默认的扫描方式是从下到上,
'这与GDI的坐标系统相反,为方便,这里
'改为-值,则取得数据就是以左上角为起点
.biPlanes = 1
.biBitCount = 32 '32位位图,默认情况下Windows不会处理最高8位
'可以将它作为自己的Alpha通道,取为32位位图,
'虽然多占用了点内存,但是避免了扫描行宽度问题,代码易于理解
.biCompression = BI_RGB '无压缩
.biSizeImage = Bmp.bmWidth * Bmp.bmHeight * 4
End With
SetDIBits Pic.Hdc, Pic.Image, 0, Bmp.bmHeight, PicData(0, 0), BmpInfo, DIB_RGB_COLORS
Pic.Refresh
End Sub
更多精彩
赞助商链接