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

Drawing & Animation III

作者:闵涛 文章来源:闵涛的学习笔记 点击数:4668 更新时间:2009/4/23 16:39:28

ReDim ByteArray(1 To bm.bmWidthBytes, 1 To bm.bmHeight)

For I = 1 To bm.bmWidthBytes Step 3
    For J = 1 To bm.bmHeight
       
        TempColor = OriginalBits(I, J)
        TempColor = TempColor + OriginalBits(I + 1, J)
        TempColor = TempColor + OriginalBits(I + 2, J)
        TempColor = TempColor / 3
       
        ByteArray(I, J) = TempColor
        ByteArray(I + 1, J) = TempColor
        ByteArray(I + 2, J) = TempColor
           

    Next J
Next I

SetBitmapBits hbm, bm.bmWidthBytes * bm.bmHeight, ByteArray(1, 1)

BitBlt Me.hdc, 0, 0, BitmapWidth, BitmapHeight, BitmapImage, 0, 0, vbSrcCopy
Me.Refresh

End Sub

--------------------------------------------------------------------------------

As you can see we loop through the local ByteArray() array and set each byte with the appropriate color. The problem with the one byte vs. three bytes pr pixel we discussed above is overcome by using a loop with the Step 3 option. This makes the outer loop able to set the variable-counter I, to first byte of each pixel in the byte array. We then simply add 1 or 2 to this value and we can access the other colors of the same pixel.

We add each of the individual color values of one pixel together into the TempValue variable. Then we will divide the value in the TempValue variable by 3, and the each color in the pixel is set to this color, which makes the pixel gray.

Blue, Red, Green

Making the bitmap one of either color is actually very simple. If you want to make a pixel blue, you simply set the red and green color values to 0 (black), and you have a pixel dominated by the blue color. Of course if the pixel didn抰 have any blue color in it, then it would turn colorless (black). The same thing goes when a pixel red or green.

The code for this procedure (only Blue shown here) is:


Private Sub cmdBlue_Click()

Dim BitmapWidthBytes As Long
Dim ByteArray() As Byte
Dim I As Long, J As Long

ReDim ByteArray(1 To bm.bmWidthBytes, 1 To bm.bmHeight)

For I = 1 To bm.bmWidthBytes Step 3
    For J = 1 To bm.bmHeight
       
        ByteArray(I, J) = 0
        ByteArray(I + 1, J) = 0
        ByteArray(I + 2, J) = OriginalBits(I + 2, J)
       
    Next J
Next I

SetBitmapBits hbm, bm.bmWidthBytes * bm.bmHeight, ByteArray(1, 1)

BitBlt Me.hdc, 0, 0, BitmapWidth, BitmapHeight, BitmapImage, 0, 0, vbSrcCopy
Me.Refresh

End Sub


Notice again that we use the Step 3 option to set the counter-variable to the start of a pixel.

Brightness

To make a pixel brighter we simply add a value to each of the colors in the pixel. To make it darker we subtract a value from each of the colors in the pixel. It is really as simple as that (isn抰 it disappointing?).

The way we implement it here is to first make a so-called LookUp table. Such a beast is actually no more than an array of calculated values. The lookup table we will generate contains values from 0 to 255, just like a typical byte. The values in the lookup table will increase as the index increases, so any given index will always have a value at least equal to or greater than any lower indexes.

The values in the lookup table are calculated by multiplying the current index with a variable lighting factor. This means that a variable lighting factor of less than 1 will darken the picture instead of lighting it up. The code used in the sample project to build a lookup table is like this:


For I = 0 To 255
    TempValue = I * Val(txtBright.Text)
   
    If TempValue > 255 Then
        BrightTable(I) = 255
    Else
        BrightTable(I) = TempValue
    End If
Next I

The lookup table in this sample code is the BrightTable. The variable lighting factor is entered in the text box next to the Brightness button on the form.

Now that we have this lookup table, let take a peek at the actual code, which manipulates the pixels:

For I = 1 To bm.bmWidthBytes Step 3
    For J = 1 To bm.bmHeight
       
        ByteArray(I, J) = BrightTable(OriginalBits(I, J))
        ByteArray(I + 1, J) = BrightTable(OriginalBits(I + 1, J))
        ByteArray(I + 2, J) = BrightTable(OriginalBits(I + 2, J))
       
    Next J
Next I


We set each of the bytes to the corresponding value in the lookup table, and thus either makes it lighter or darker, depending on the variable lighting factor.

Ripple

The ripple effect is a bit different than any of the other effect we have applied. This is because the rippling does not directly change the color of a specific pixel, but instead set the color of a given pixel equal to the color of another pixel.

As with the Brightness effect, we build a lookup table to store intermediate values in. This lookup table must not be longer than the width of the bitmap, since the index of the table is considered an X-position into the bitmap. So by defining the actual index in the lookup table as a position in the bitmap, the actual value in the index is the distortion pixel, i.e. the pixel that the index X position will be moved to.

So how do we calculate these distortion pixel values? Since we want a ripple (wave like) effect, so why not use the Sin function which define values in waves (I know this is not correct, just accept it in our usage).

So by using a formula with a Sine calculated value we can distort (move) the pixel in the lookup table by adding the index from the table to result of the formula, and thus get a wave like result. The building of the lookup table looks like this:


 

For I = 1 To BitmapWidth
    TempValue = I + Sin(I / 5) * Val(txtRipple.Text)
    If TempValue > BitmapWidth Then
        RippleTable(I) = BitmapWidth
    ElseIf TempValue < 1 Then
        RippleTable(I) = 1
    Else
        RippleTable(I) = TempValue
    End If
Next I

 

We still have to keep things from moving out of bounds, so we make sure that any values over 200 and below 1 are correct to either the least (1) or the greatest (200) value.

So the actual manipulation is then quite simple and looks like this:


For I = 1 To bm.bmWidthBytes Step 3
    For J = 1 To bm.bmHeight
        ByteArray(I, J) = OriginalBits(I, RippleTable(J))
        ByteArray(I + 1, J) = OriginalBits(I + 1, RippleTable(J))
        ByteArray(I + 2, J) = OriginalBits(I + 2, RippleTable(J))
    Next J
Next I


We simply replace the horizontal pixel with the color of the distorted pixel, which is stored in the lookup table, and we have a wave like ripple on the bitmap.

Please note that there are many different algorithms and implementations to achieve each these different effects, the ones shown in this section is just our way of demonstrating pixel manipulation in VB. So do not complain if you discover a nicer or better algorithm, we were just demonstrating pixel manipulation and not algorithm effectiveness and implement.

8-Bit bitmaps

So how do we apply the same effects to 8-bit bitmaps? Well, the actual algorithms are the same, but the implement is not. As stated pr

上一页  [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……
    咸宁网络警察报警平台