如何在VB中直接显示无格式256灰度级图像
2008-02-23 06:52:41来源:互联网 阅读 ()
Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function DeleteDC Lib "gdi32" (ByVal HDC As Long) As Long
Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Declare Function SetDIBitsToDevice Lib "gdi32" (ByVal HDC As Long, ByVal x As Long, ByVal y As Long, ByVal dx As Long, ByVal dy As Long, ByVal SrcX As Long, ByVal SrcY As Long, ByVal Scan As Long, ByVal NumScans As Long, Bits As Any, BitsInfo As BITMAPINFO, ByVal wUsage As Long) As Long
Type rgbquad
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
rgbReserved As Byte
End Type
Type PALETTEENTRY
peRed As Byte
peGreen As Byte
peBlue As Byte
peFlags As Byte
End Type
Type BITMAPFILEHEADER
bfType As Integer
bfSize As Long
bfReserved1 As Integer
bfReserved2 As Integer
bfOffBits As Long
End Type
Type 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
Type BITMAPINFO
bmiHeader As BITMAPINFOHEADER
bmiColors(0 To 255) As rgbquad
End Type
Global Const SRCCOPY = &HCC0020 ' dest=source
Global Const srcand = &H8800C6 ' dest=source and dest
Global Const srcor = &HEE0086 ' dest=source or dest
Public Const COLORONCOLOR = 3
Public Const DIB_RGB_COLORS = 0 ' color table in RGBs
Public Const DIB_PAL_COLORS = 1 '
color table in palette indices
Global Const GMEM_MOVEABLE = &H2
'--------以上为定义部分,可放在一个BAS文件中--------
Dim x As Long, ii As Integer
Dim w1 As Long, h1 As Long
Dim bitmapinfo_h As BITMAPINFOHEADER,
bitmapfile_h As BITMAPFILEHEADER
Dim lpInitInfo As BITMAPINFO
Dim t_rgbquad(0 To 255) As rgbquad
Dim pLogPal As LOGPALETTE
Dim leng As Long
Dim t_buf() As Byte '图像数据buffer
On Error GoTo Error_process
'Set up error handler.
' Open the file
pfile1$ = "c:\fcg\test.d"
' test.d为256X256大小,256灰度级的无格式图像文件
fd% = FreeFile
w1 = 256 '图像宽度
h1 = 256 '图像高度
leng = w1 * h1
ReDim t_buf(leng) As Byte
Open pfile1$ For Binary As #fd%
Get #fd%, , t_buf
Close ' Close the file
leng = w1 * h1
bitmapfile_h.bfType = 19778 '"BM"
bitmapfile_h.bfSize = 1078 h1 * w1
bitmapfile_h.bfReserved1 = 0
bitmapfile_h.bfReserved2 = 0
bitmapfile_h.bfOffBits = 1078
bitmapinfo_h.biSize = 40
bitmapinfo_h.biWidth = w1
bitmapinfo_h.biHeight = h1
bitmapinfo_h.biPlanes = 1
bitmapinfo_h.biBitCount = 8
bitmapinfo_h.biCompression = 0
bitmapinfo_h.biSizeImage = 0
bitmapinfo_h.biXPelsPerMeter = 0
bitmapinfo_h.biYPelsPerMeter = 0
bitmapinfo_h.biClrUsed = 256
For ii = 0 To 255 '设置色表为256灰度
t_rgbquad(ii).rgbBlue = CByte(ii)
t_rgbquad(ii).rgbGreen = CByte(ii)
t_rgbquad(ii).rgbRed = CByte(ii)
' t_rgbquad.rgbReserved = 0
Next ii
lpInitInfo.bmiHeader = bitmapinfo_h
For ii = 0 To 255
lpInitInfo.bmiColors(ii) = t_rgbquad(ii)
Next ii
'picture1为一个picture控件,
用于显示无格式256灰度级图像
x = SetDIBitsToDevice(picture1.HDC, 0, 0,
w1, h1, 0, 0, 0, h1, t_buf(0), lpInitInfo,
0) '显示图像
x = GlobalUnlock(hPal) '释放资源
x = GlobalFree(hPal)
GoTo Normal_exit
Error_process:
Msgbox "程序运行出错!"
Normal_exit:
上一篇: VB图片框在报表设计中屏幕滚动的问题
下一篇: 在Visual Basic 中实现启动程序时的闪烁屏幕
标签:
版权申明:本站文章部分自网络,如有侵权,请联系: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