|
hBitmap = LoadImage(0, FileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE Or LR_CREATEDIBSECTION)
If hBitmap = 0 Then ''''Failure in loading bitmap DeleteDC DC GenerateDC = 0 ''''Raise error Err.Raise vbObjectError + 2 Exit Function End If
''''Throw the Bitmap into the Device Context SelectObject DC, hBitmap
''''Return the device context and handle BitmapHandle = hBitmap GenerateDC = DC
End Function
So now we have a handle to the loaded bitmap, we need to get the next parameter in the GetBitmapBits function. This parameter is a dwCount, which is the to number of bytes the function should return. In order to get this, we have to extract some info from the bitmap and into a special BITMAP structure, using the GetObjectAPI function . The bitmap structure is declared as such:
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
Members of BITMAP structure:
bmType: The type of the bitmap. Must be 0 for logical bitmaps bmWidth: The width of the bitmap in pixels. Must be greater than 0 bmHeight: The height of the bitmap. Must be greater than 0 bmWidthBytes: The width of the bitmap in bytes. This number must be even bmPlanes: The number of color planes bmBitsPixel: The number of bits per pixel bmBits: A pointer to the bits.
The important members of this structure, that concern us here, are the bmWidth, bmHeight and bmWidthBytes members. To calculate how many bytes we need we simply multiply the bmHeight with bmWidthBytes. The bmWidth can not be used in this instance, since it only contains width values in pixels, and not in bytes.
The last parameter in the GetBitmapBits function lpBits is a pointer to the buffer, which will hold the bits. Since we are not eligible to work with pointers directly, we have to 慶heat?the system a bit. The lpBits parameter is passed By Reference, which means that the actual variable will not be passed, but instead a reference (pointer) to the variable is made and passed to the function. So the trick is to pass the first index in a byte array to the function. This address of this first index will be passed to the function, and it will take it from there. So the only thing we have to do, is to initialize a Byte array with at least the same amount of space as the value passed in the dwCount. This way the function will copy all the bits into our buffer (byte array), and we will be able to work with them as any normal array.
So in order to get the bits and bytes of a given bitmap, we could do the following:
Dim BitmapImage As Long ''''The DC of the bitmap Dim bm As BITMAP ''''The bitmap structure Dim hbm As Long ''''The bitmap handle Dim OriginalBits() As Byte
''''Load the bitmap and get the handle on it BitmapImage = GenerateDC(App.Path & "\bitmap.bmp", hbm)
''''Get the information into the BITMAP structure GetObjectAPI hbm, Len(bm), bm
''''Redimension the byte array to fit into the size of the bitmap ReDim OriginalBits(1 To bm.bmWidthBytes, 1 To bm.bmHeight)
''''Retrieve the bits into the byte array GetBitmapBits hbm, bm.bmWidthBytes * bm.bmHeight, OriginalBits(1, 1)
Setting the Bits
After manipulating with the bits and bytes of a bitmap, you have to set them back into position before any updates are shown. This is done with the SetBitmapBits function. It is declared as such:
Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, _ ByVal dwCount As Long, lpBits As Any) As Long
It is almost identical with the GetBitmapBits, and is also used in the same way. The hBitmap parameter is the handle of the given bitmap. The dwCount parameter is the size of the byte array, which contains the new bytes, and the lpBits is the first index into the byte array with the pixel information.
24-bit bitmaps
So now that we know all about getting and setting the bits of a bitmap, let us take a look at what can actually be done with it. The sample project in BITMAPS.ZIP , demonstrates some simple image processing algorithms. Before actually showing you how these are implemented we should address the problem with using 24-bit bitmaps and manipulating these in an array of bytes. There is no data structure in Visual Basic, which is a 24-bit variable, so we cannot directly make a value and set it. Instead we have to deal with 24-bit in pieces of 8 bit (1 byte).

Take a look at the following illustration, which shows the byte layout of small part of a 24-bit bitmap:

As you can see each pixel is one byte 慼igh?and 3 bytes 憌ide? which is equal to (3*8 bits + 1*8 bits) = 24 bits. This is relatively simple. The problem occurs when you think about how our byte array is organized. It just represents each byte with a separate index and not each pixel as a separate index. So an index of (3,1) into the byte array will not produce the third pixel in the first row, but instead produce the color value of blue of the first pixel. Keep this mind.
Our sample project is built around some effects applied to a picture. The picture is loaded in the Form_Load event. In the same event we extract the information we need to do the pixel manipulations:
Private Sub Form_Load()
''''Load the image BitmapImage = GenerateDC(App.Path & "\bitmap.bmp", hbm)
''''Get the bitmap structure GetObjectAPI hbm, Len(bm), bm
''''Preinitialize the byte array ReDim OriginalBits(1 To bm.bmWidthBytes, 1 To bm.bmHeight)
BitmapWidth = bm.bmWidth BitmapHeight = bm.bmHeight
''''Get the bits GetBitmapBits hbm, bm.bmWidthBytes * bm.bmHeight, OriginalBits(1, 1) ''''Draw the bitmap BitBlt Me.hdc, 0, 0, BitmapWidth, BitmapWidth, BitmapImage, 0, 0, vbSrcCopy
End Sub
We use the same methods as described above to extract the pixels from the bitmap. Lastly we display the image with the BitBlt function. Note that the OriginalBits() array is declared at global level, and is used as the reference to the bits in the image processing functions.
Graying
Graying an image is very simple, you simply add the value of each color together and divide this value with 3. This will get you the medium value of the three colors. The three colors are then set to this medium value, and you have a gray pixel.
The code looks like this:
Private Sub cmdGrey_Click()
Dim BitmapWidthBytes As Long Dim ByteArray() As Byte Dim I As Long, J As Long Dim TempColor As Long 上一页 [1] [2] [3] [4] [5] [6] 下一页 [Sql Server]Sql精妙语句--各种求值函数 [网页制作]网页表格之---多个表格纵向排列 [网页制作]JavaScript另类用法--读取和写入cookie [网页制作]号称非常安全的上网工具---360安全浏览器介绍 [办公软件]信息技术教学篇---Word工具栏的显示、隐藏及四种菜… [操作系统]开始菜单---运行命令大总结 [操作系统]网络转载---64位操作系统与32位的区别 [操作系统]ldap:///(没有响应)Windows无法访问指定设备、路径… [网络技术]安全篇---交换机设置方法介绍 [聊天工具]Real10 & Xpdf installation on Linux Box
|