首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

怎么创建24位位图

2012-04-09 
如何创建24位位图?如何创建24位位图?我想创建一个指定大小的24位位图文件主要想获取指定大小的这个位图的B

如何创建24位位图?
如何创建24位位图?

我想创建一个指定大小的24位位图文件
主要想获取指定大小的这个位图的Byte信息
不用picturebox控件!

[解决办法]
研究下bmp的文件格式 根据文件格式写即可
[解决办法]
用GDI+就可以了
[解决办法]
Private Type BITMAPFILEHEADER
bfType As Integer
bfSize As Long
bfReserved1 As Integer
bfReserved2 As Integer
bfOffBits As Long
End Type

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 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 Type RGBQUAD
Blue As Byte
Green As Byte
Red As Byte
Reserved As Byte
End Type


Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Type POINTAPI
x As Long
y As Long
End Type

Private Const PI = 3.14159265238 / 180


Private Const DIB_RGB_COLORS As Long = 0
Private Const OBJ_BITMAP As Long = 7
Private Const SRCCOPY As Long = &HCC0020
Private Const COLORONCOLOR As Long = 3
Private Const CF_BITMAP As Long = 2
Private Const CLR_INVALID = &HFFFFFFFF '如指定的点位于设备场景的剪切区之外,则返回CLR_INVALID
Private Const STRETCH_ANDSCANS = 1 '默认设置。剔除的线段与剩下的线段进行AND运算。这个模式通常应用于采用了白色背景的单色位图
Private Const STRETCH_ORSCANS = 2 '剔除的线段被简单的清除。这个模式通常用于彩色位图
Private Const STRETCH_DELETESCANS = 3 '剔除的线段与剩下的线段进行OR运算。这个模式通常应用于采用了白色背景的单色位图
Private Const STRETCH_HALFTONE = 4 '目标位图上的像素块被设为源位图上大致近似的块。这个模式要明显慢于其他模式
Private Const IMAGE_BITMAP As Long = 0
Private Const LR_LOADFROMFILE As Long = &H10
Private Const LR_CREATEDIBSECTION As Long = &H2000


Private Declare Function CreateDIBSection Lib "gdi32 " (ByVal hDC As Long, lpBitsInfo As BITMAPINFOHEADER, ByVal wUsage As Long, lpBits As Long, ByVal handle As Long, ByVal dw As Long) As Long
Private Declare Function CreateCompatibleDC Lib "gdi32 " (ByVal hDC As Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32 " (ByVal hDC As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function DeleteDC Lib "gdi32 " (ByVal hDC As Long) As Long
Private Declare Function GetObject Lib "gdi32 " Alias "GetObjectA " (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Private Declare Function GetObjectType Lib "gdi32 " (ByVal hgdiobj As Long) As Long
Private Declare Function SelectObject Lib "gdi32 " (ByVal hDC As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteObject Lib "gdi32 " (ByVal hObject As Long) As Long
Private Declare Function BitBlt Lib "gdi32 " (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 SetRect Lib "user32 " (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long


Private Declare Function FillRect Lib "user32 " (ByVal hDC As Long, lpRect As RECT, ByVal hBrush As Long) As Long
Private Declare Function CreateSolidBrush Lib "gdi32 " (ByVal crColor As Long) As Long
Private Declare Function VarPtrArray Lib "msvbvm50 " Alias "VarPtr " (Ptr() As Any) As Long
Private Declare Function GetDC Lib "user32 " (ByVal hWnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32 " (ByVal hWnd As Long, ByVal hDC As Long) As Long
Private Declare Function PlgBlt Lib "gdi32 " (ByVal hdcDest As Long, lpPoint As POINTAPI, ByVal hdcSrc As Long, ByVal nXSrc As Long, ByVal nYSrc As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hbmMask As Long, ByVal xMask As Long, ByVal yMask As Long) As Long
Private Declare Sub ZeroMemory Lib "kernel32 " Alias "RtlZeroMemory " (Destination As Any, ByVal Length As Long)
Private Declare Function LoadImage Lib "user32 " Alias "LoadImageA " (ByVal hInst As Long, ByVal lpsz As String, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32 " Alias "RtlMoveMemory " (lpDst As Any, lpSrc As Any, ByVal ByteLength As Long)


Private mBmpInfoHeader As BITMAPINFOHEADER
Private mhDC As Long
Private mhDib As Long
Private mhOldDib As Long
Private mPtr As Long
Private mWidthBytes As Long

Public Function Create(ByVal NewWidth As Long, ByVal NewHeight As Long, Optional ByVal Bits As Long = 24, Optional ByVal BackColor As Long = vbBlack) As Boolean
Dim Rct As RECT
Dim hBrush As Long



Destroy '销毁以前的DIB
mhDC = CreateCompatibleDC(0) '创建DIB设备场景
If (mhDC <> 0) Then '创建成功
With mBmpInfoHeader '位图信息头
.biSize = Len(mBmpInfoHeader)
.biPlanes = 1
.biBitCount = Bits
.biWidth = NewWidth
.biHeight = NewHeight
Select Case Bits
Case 1
mWidthBytes = (((.biWidth + 7) \ 8 + 3) And &HFFFFFFFC)
Case 4
mWidthBytes = (((.biWidth + 1) \ 2 + 3) And &HFFFFFFFC)
Case 8
mWidthBytes = ((.biWidth + 3) And &HFFFFFFFC)
Case 16
mWidthBytes = ((.biWidth * 2 + 3) And &HFFFFFFFC)
Case 24
mWidthBytes = ((.biWidth * 3 + 3) And &HFFFFFFFC)
Case 32
mWidthBytes = .biWidth * 4
Case Else
Exit Function
End Select
.biSizeImage = mWidthBytes * .biHeight
End With
mhDib = CreateDIBSection(mhDC, mBmpInfoHeader, DIB_RGB_COLORS, mPtr, 0, 0) '创建DIB
If (mhDib <> 0) Then
mhOldDib = SelectObject(mhDC, mhDib) '选入设备场景
Else
Destroy '如果DIB创建失败,需销毁DIB设备场景
End If
End If
Create = (mhDib <> 0)
End Function


Public Sub Destroy()
If mhDC <> 0 Then
If mhDib <> 0 Then
SelectObject mhDC, mhOldDib
DeleteObject mhDib
End If
DeleteObject mhDC
mBmpInfoHeader.biBitCount = 0
mBmpInfoHeader.biWidth = 0
mBmpInfoHeader.biHeight = 0
mBmpInfoHeader.biSizeImage = 0
End If
mhDC = 0: mhDib = 0: mhOldDib = 0: mPtr = 0: mIsGreyMode = False


End Sub


读成BYTE我也在研究,还没研究成,上面的定义有可能用的到.
[解决办法]
好像我在VBGOOD里面帖过这个代码
[解决办法]
呵,laviewpbt,我也忘了从哪里下的了,如果是你写的,可要说一声高手了.
[解决办法]
网上这样的代码很多。找找例子看看。
Good Luck!
[解决办法]
看你这帖子挂这么多天了,还是我贴给你一个例子吧。

http://blog.csdn.net/KiteGirl/archive/2007/07/11/1684894.aspx

这是我做的BitMap程序中的一个,类似程序很多。其中有个BitMapGetBySpace函数就是产生一个空的BitMap文件头。
这个程序原本是用来模拟下雨效果的,在一张BMP图片上动态地显现出许多透明的雨点。这些函数里,还有画点和取点颜色的函数以及Alpha混合函数。由于具体的函数太烦琐,无法详细给你解释。
如果你想知道更多,应该去参考一下BMP文件的结构,以及相关API函数。

如果你想运行这个程序看看效果,应该在程序的目录下放一个Test.bmp文件,这个BMP最好是640*480的,必须是24Bit位图。
如果在VB下解释运行速度不怎么样,但是在编译成EXE之后,速度会比较快。

[解决办法]
顺便说一下:
我给你的代码测试时候有点风险,弄不好可能会导致系统非法操作或者出现不可预料的后果,你不得不重新启动计算机。
因此测试时候不要运行其他程序做对你有用的工作。

热点排行