转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
Drawing & Animation III         ★★★★

Drawing & Animation III

作者:闵涛 文章来源:闵涛的学习笔记 点击数:4671 更新时间:2009/4/23 16:39:28
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
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台