Class

RGBSurface


Description

Used for direct-color pixel manipulations. The RGBSurface property of a Picture object allows you to manipulate the picture at the pixel level. Can be used only for pictures created by the Picture constructor with no pixel depth or a depth of only 16 or 32.

Methods

Name

Parameters

Returns

Shared

FloodFill

x As Double, y As Double, FillColor As Color

Pixel

x As Double, y As Double

Color

Transform

map() As Integer

Method descriptions


RGBSurface.FloodFill

FloodFill(x As Double, y As Double, FillColor As Color)

Performs a "floodfill" (the action performed by a PaintBucket tool in an image editing program) on an RGBSurface.

The pixel at x, y and all adjacent pixels of the same color are changed to the passed FillColor.

The following code uses the FloodFill method to paint an RGBSurface object in a random color. It is in the MouseDown event of a DesktopCanvas control. The variable p is a Picture object.

Var randomColor As Color
randomColor = Color.RGB(Rnd * 255, Rnd * 255, Rnd * 255) // create random color
p.RGBSurface.FloodFill(x, y, randomColor) // fill the area with the color
Me.Graphics.DrawPicture(p, 0, 0) // repaint the canvas control

RGBSurface.Pixel

Pixel(x As Double, y As Double) As Color

Returns the color of the object at location specified by the x and y parameters.

This example replaces all the black pixels in a Picture, somePicture, with white:

Var surf As RGBSurface = somePicture.RGBSurface
Var lastX As Integer = somePicture.Width - 1
Var lastY As Integer = somePicture.Height - 1
For y As Integer = 0 To lastY
  For x As Integer = 0 To lastX
    If surf.Pixel(x, y) = &c000000 Then
      surf.Pixel(x, y) = &cFFFFFF
    End if
  Next
Next

RGBSurface.Transform

Transform(map() As Integer)

Applies a one-to-one pixel transformation to all pixels of an RGBSurface.

Transform is overloaded. You can specify the transformation via either of three lookup tables, redMap, greenMap, and blueMap, or one lookup table to apply to all three channels.

Each map parameter is an 256 element array of integers. The transformation works as follows: For each pixel, the pixel's RGB value is used as an index into the map arrays and the value found becomes the new R, G, or B value for the pixel.

For example, if you set up a map such that map(i)=255-i, then calling Transform(Map) will invert the image.

The following example uses the Transform method to invert an image:

Const kMaxMapOffset = 255
Var map(kMaxMapOffset) As Integer
For i As Integer = 0 To kMaxMapOffset
  map(i) = kMaxMapOffset - i
Next
somePicture.RGBSurface.Transform(map)

Notes

Pixel manipulations using the RGBSurface property are faster than the same manipulations using the Graphics class methods.

32-bit RGBSurface objects are mapped to 24-bit objects on Windows.

RGBSurface supports alpha channels by taking them into account when rendering objects.

Sample code

This example replaces all the black pixels in a Picture, somePicture, with white. Put it in the Paint event handler of a Canvas and reference a picture you have added to the project.

Var surf As RGBSurface = somePicture.RGBSurface
Var lastX As Integer = somePicture.Width - 1
Var lastY As Integer = somePicture.Height - 1
For y As Integer = 0 To lastY
  For x As Integer = 0 To lastX
    If surf.Pixel(x, y) = &c000000 Then
      surf.Pixel(x, y) = &cFFFFFF
    End if
  Next
Next

g.DrawPicture(somePicture, 0, 0, somePicture.Width, somePicture.Height)

The following example uses the FloodFill method to paint an RGBSurface object in a random color. You can put it in the Paint event handle of a Canvas.

// Create a random color
Var randomColor As Color
randomColor = Color.RGB(Rnd * 255, Rnd * 255, Rnd * 255)  //create random color

// Starting in the top left, fill the picture with the color
somePicture.RGBSurface.FloodFill(0, 0, randomColor)

// Display the picture
g.DrawPicture(somePicture, 0, 0)

The following example uses the Transform method to invert an image. You can put it in the Paint event handle of a Canvas.

Const kMaxMapOffset = 255
Var map(kMaxMapOffset) As Integer
For i As Integer = 0 To kMaxMapOffset
  map(i) = kMaxMapOffset - i
Next
somePicture.RGBSurface.Transform(map)

// Display the picture
g.DrawPicture(somePicture, 0, 0)

Compatibility

All project types on all supported operating systems.

See also

Object parent class; RGBSurface property of the Picture object.