Example Application: The Color Selector |
|
Setting the Color of, or Painting, a Pixel |
The screen of a computer monitor uses a series of horizontal and vertical lines, the intersection of two perpendicular lines is a pixel. Each pixel holds one color. Of course, two adjacent pixels can hold the same color or each can hold a different color. A bitmap is a series of colored adjacent pixels. Put another way, for a group of pixels to be considered a bitmap, these pixels must constitute a group. A bitmap is made by specifying the color of each pixel. This means that the pictures we have used so far were simply made of pixels and each pixel held an appropriate color. |
If you decide to create or design a picture using the tool resources in Microsoft Visual Studio, you would experiment, on a large scale the ability to specify the color of each individual pixel, using (a limited list of) colors: Actually, when you have a bitmap, you can access any pixel of the picture and then you can either specify its color or get its current color. To allow you to specify the color of a pixel, the Bitmap class provides a method named SetPixel and its syntax is: public void SetPixel(int x, int y, Color color) The x and y arguments represent the left and top values of the location of the pixel. The 3rd argument specifies the new color that the pixel will hold. Here is an example: Imports System.Drawing Imports System.Windows.Forms Module Exercise Public Class Starter Inherits Form Dim components As System.ComponentModel.Container Public Sub New() InitializeComponent() End Sub Public Sub InitializeComponent() BackColor = Color.Black End Sub Private Sub FormPaint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint Dim i As Integer Dim j As Integer Dim bgDrawingArea As Bitmap = New Bitmap(Width, Height) e.Graphics.DrawImage(bgDrawingArea, 0, 0) For i = 0 To Width For j = 0 To Height bgDrawingArea.SetPixel(i, j, Color.White) Dim painter As Graphics = Graphics.FromHwnd(Handle) e.Graphics.DrawImage(bgDrawingArea, 0, 0) j += 20 Next i += 20 Next End Sub End Class Function Main() As Integer Dim frmStart As Starter = New Starter Application.Run(frmStart) Return 0 End Function End Module
We previously mentioned that a picture was a series of pixels with each pixel holding a color. As opposed to specifying the color of a pixel, you can retrieve its color. To support this, the Bitmap class is equipped with a method named GetPixel. Its syntax is: public Color GetPixel(int x, int y) The x and y arguments represent the left and top values of the location of the pixel whose color you want to get. This method returns the color of that pixel.
|
|
||
Home | Copyright © 2008-2016, FunctionX, Inc. | |
|