登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

miaozk2006

点滴积累与收藏:关于技术,关于生活

 
 
 

日志

 
 

在VB6中保存单色(1Bit)位图(VB6代码)  

2011-11-24 20:24:08|  分类: 编程-VB |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

程序代码:  

Option Explicit

Private Const DIB_RGB_COLORS As Long = 0
Private Const SRCCOPY As Long = &HCC0020
Private Const BI_RGB As Long = 0&

Private 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

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

Private 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

Private Declare Function CreateCompatibleDC Lib "gdi32.dll" ( _
     ByVal hdc As Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32.dll" ( _
     ByVal hdc As Long, _
     ByVal nWidth As Long, _
     ByVal nHeight As Long) As Long
Private Declare Function SelectObject Lib "gdi32.dll" ( _
     ByVal hdc As Long, _
     ByVal hObject As Long) As Long
Private Declare Function BitBlt Lib "gdi32.dll" ( _
     ByVal hDestDC As Long, _
     ByVal x As Long, _
     ByVal y As Long, _
     ByVal nWidth As Long, _
     ByVal nHeight As Long, _
     ByVal hSrcDC As Long, _
     ByVal xSrc As Long, _
     ByVal ySrc As Long, _
     ByVal dwRop As Long) As Long
Private Declare Function GetDIBits Lib "gdi32.dll" ( _
     ByVal aHDC As Long, _
     ByVal hBitmap As Long, _
     ByVal nStartScan As Long, _
     ByVal nNumScans As Long, _
     ByRef lpBits As Any, _
     ByRef lpBI As BITMAPINFO, _
     ByVal wUsage As Long) As Long
Private Declare Function DeleteDC Lib "gdi32.dll" ( _
     ByVal hdc As Long) As Long
Private Declare Function DeleteObject Lib "gdi32.dll" ( _
     ByVal hObject As Long) As Long
Private Declare Function GetBitmapObject Lib "gdi32" Alias "GetObjectA" ( _
    ByVal hBitmap As Long, _
    ByVal cbBuffer As Long, _
    ByRef destBmp As Any) As Long

Private Sub SavePictureBW(ByVal ctrl As PictureBox, ByVal destfile As String)
    Dim hdcMono As Long, hbmpMono As Long, hbmpOld As Long, dxBlt As Long, dyBlt As Long, success As Long
    Dim numscans As Long, byteswide As Long, totalbytes As Long, lfilesize As Long
    Dim bmpsrc As BITMAP, bmpdst As BITMAP
    Dim bInfo As BITMAPINFO
    Dim bitmaparray() As Byte, fileheader() As Byte
    Dim ff As Integer, by8
    
    'Object's scalemode must be Pixel.
    dxBlt = ctrl.ScaleWidth
    dyBlt = ctrl.ScaleHeight
    
    'Create monochrome bitmap from control.
    hdcMono = CreateCompatibleDC(0)
    hbmpMono = CreateCompatibleBitmap(hdcMono, dxBlt, dyBlt)
    success = GetBitmapObject(hbmpMono, Len(bmpsrc), bmpsrc)
    hbmpOld = SelectObject(hdcMono, hbmpMono)
    success = BitBlt(hdcMono, 0, 0, dxBlt, dyBlt, ctrl.hdc, 0, 0, SRCCOPY)
    
    'Calculate array size needed for bitmap bits (dword aligned)
    numscans = dyBlt
    by8 = dxBlt / 8
    If (dxBlt Mod 8) = 0 And (by8 Mod 4) = 0 Then
       byteswide = by8
    Else
       byteswide = (Int(by8) + 4) - (Int(by8) Mod 4)
    End If
    totalbytes = numscans * byteswide
    ReDim bitmaparray(1 To totalbytes)
    
    'Set BITMAPINFO values to pass to GetDIBits function.
    With bInfo
       .bmiHeader.biSize = Len(.bmiHeader)
       .bmiHeader.biWidth = bmpsrc.bmWidth
       .bmiHeader.biHeight = bmpsrc.bmHeight
       .bmiHeader.biPlanes = bmpsrc.bmPlanes
       .bmiHeader.biBitCount = bmpsrc.bmBitsPixel
       .bmiHeader.biCompression = BI_RGB
    End With
    
    success = GetDIBits(hdcMono, ctrl.Image, 0, numscans, bitmaparray(1), bInfo, DIB_RGB_COLORS)
    
    'bitmaparray should now contain bitmap bit data. Now create bitmap file header.
    ReDim fileheader(1 To &H3E)
    fileheader(1) = &H42 'B
    fileheader(2) = &H4D 'M
    lfilesize = UBound(fileheader) + UBound(bitmaparray)
    fileheader(3) = lfilesize And 255
    fileheader(4) = (lfilesize \ 256) And 255
    fileheader(5) = (lfilesize \ 65536) And 255
    fileheader(6) = (lfilesize \ 16777216) And 255
    fileheader(11) = &H3E 'offset
    fileheader(15) = &H28 'size of bitmapinfoheader
    fileheader(19) = dxBlt And 255
    fileheader(20) = (dxBlt \ 256) And 255
    fileheader(21) = (dxBlt \ 65536) And 255
    fileheader(22) = (dxBlt \ 16777216) And 255
    fileheader(23) = dyBlt And 255
    fileheader(24) = (dyBlt \ 256) And 255
    fileheader(25) = (dyBlt \ 65536) And 255
    fileheader(26) = (dyBlt \ 16777216) And 255
    fileheader(27) = 1
    fileheader(29) = 1
    fileheader(35) = UBound(bitmaparray) And 255
    fileheader(36) = (UBound(bitmaparray) \ 256) And 255
    fileheader(37) = (UBound(bitmaparray) \ 65536) And 255
    fileheader(38) = (UBound(bitmaparray) \ 16777216) And 255
    fileheader(47) = 2
    fileheader(51) = 2
    fileheader(59) = &HFF
    fileheader(60) = &HFF
    fileheader(61) = &HFF
    
    ff = FreeFile
    Open destfile For Binary Access Write As #ff
       Put #ff, , fileheader
       Put #ff, , bitmaparray
    Close #ff
    
    ' Clean up
    Call SelectObject(hdcMono, hbmpOld)
    Call DeleteDC(hdcMono)
    Call DeleteObject(hbmpMono)
End Sub

Private Sub Command1_Click()
    Call SavePictureBW(Picture1, "d:\123.bmp")

摘自:http://blog.m5home.com/article.asp?id=504


VB部分相关推荐


VB快速读取 TextBox N 行的资料

VB禁止使用 Alt-Tab Ctrl-Alt-Del

生成迷宫的程序

另一方法转换大小写

VB控件注册 - 利用资源文件将dllocx打包进exe文件

VB利用资源文件进行工作

[]vb高效编程(优化)

VB阳历转阴历

VB代码取得硬盘的物理序列号

VB获得磁盘的文件系统

VB的,经常注册和反注册OCX控件和DLL

VB从程序中生成Exe文件

VB6监视/操作剪贴板示例(VB6.0代码)

VB6里自动提交/自动填表的一种相对通用的方案

VB移动没有标题的窗体

VB随机字母的函数

VB删除带子文件夹和文件的文件夹

VB怎样屏蔽 Alt+F4

VB 隐藏进程

vb屏蔽文本框点右键时的弹出菜单

VB手控Combobox的打开或收起

VBINI文件的读写、删除(对中文支持很好)

vb全局热键的写法(占很少的资源)

vb取消文本框的粘贴功能

VB常用文件操作类

VB获取特殊文件夹

VB获取windows各常用目录的函数(模块)

VB生成太极图

VB:常用内部函数大全,你会了几个呢?

vbSendMessage函数

精简VB程序的代码

VB:将数字转换为大写中文

VB:设定 MsgBox 在若干时间之后若无回应则自动关闭

VB:读取及设定NumLock/CapsLock/ScrollLock的值

VB:您知道 Mid$ 函量可以放在 '=' 的左方吗

VB后台获得按键,并执行自己的函数(非钩子及热键)

VB:将短文件名格式转成长文件名

vb中使用Iphlpapi.dll获取网络信息(上)

vb中使用Iphlpapi.dll获取网络信息(下) 


更多精彩>>>
  评论这张
 
阅读(1157)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018