Title | Convert a picture to blue scale in three ways in VB .NET |
Description | This example shows how to convert a picture to blue scale in three ways in Visual Basic .NET. |
Keywords | blue scale, picture, VB .NET, image processing |
Categories | Graphics, VB.NET |
|
|
The ZeroRedGreen, AverageImage, and KeepBlueBrightness subroutines convert an image into shades of blue in different ways. All use the BitmapBytesRGB24 class to provide fast access to the bitmap's pixel data.
Subroutine ZeroRedGreen sets the green and blue components of each pixel to zero. The result is all blue but darker than the original because the red and green contributions to brightness have been removed.
|
|
' Set Red = 0, Green = 0.
Private Sub ZeroRedGreen(ByVal pic As PictureBox)
' Make a BitmapBytesRGB24 object.
Dim bm As New Bitmap(pic.Image)
Dim bm_bytes As New BitmapBytesRGB24(bm)
' Lock the bitmap.
bm_bytes.LockBitmap()
Dim pix As Integer
For y As Integer = 0 To bm.Height - 1
pix = y * bm_bytes.RowSizeBytes
For x As Integer = 0 To bm.Width - 1
bm_bytes.ImageBytes(pix + 1) = 0
bm_bytes.ImageBytes(pix + 2) = 0
pix += 3
Next x
Next y
' Unlock the bitmap.
bm_bytes.UnlockBitmap()
pic.Image = bm
End Sub
|
|
Subroutine AverageImage sets each pixel's blue component to the average of its red, green, and blue components. It sets the red and green components to zero. The result is darker than the original because each pixel's total color component values is one third its original value.
|
|
Dim pix As Integer
For y As Integer = 0 To bm.Height - 1
pix = y * bm_bytes.RowSizeBytes
For x As Integer = 0 To bm.Width - 1
bm_bytes.ImageBytes(pix) = CByte(( _
CInt(bm_bytes.ImageBytes(pix)) + _
bm_bytes.ImageBytes(pix) + _
bm_bytes.ImageBytes(pix)) \ 3)
bm_bytes.ImageBytes(pix + 1) = 0
bm_bytes.ImageBytes(pix + 2) = 0
pix += 3
Next x
Next y
|
|
Subroutine KeepBlueBrightness sets each pixel's blue component to the sum of its red, green, and blue components. If this sum is greater than 255, it sets the blue component to 255 and splits the remainder between the red and green components. The result is a brighter shade of blue. This version gives the best result.
|
|
Dim red_color As Integer
Dim green_color As Integer
Dim blue_color As Integer
Dim pix As Integer
For y As Integer = 0 To bm.Height - 1
pix = y * bm_bytes.RowSizeBytes
For x As Integer = 0 To bm.Width - 1
blue_color = CInt(bm_bytes.ImageBytes(pix)) + _
bm_bytes.ImageBytes(pix + 1) + _
bm_bytes.ImageBytes(pix + 2)
If blue_color > 255 Then
red_color = (blue_color - 255) \ 2
green_color = red_color
blue_color = 255
Else
red_color = 0
green_color = 0
End If
bm_bytes.ImageBytes(pix) = CType(blue_color, Byte)
bm_bytes.ImageBytes(pix + 1) = CType(green_color, _
Byte)
bm_bytes.ImageBytes(pix + 2) = CType(red_color, _
Byte)
pix += 3
Next x
Next y
|
|
My book Visual Basic Graphics Programming has a lot more information about graphics.
|
|
|
|