Set Pixel

The SetPixel() method in the AnyBitmap class is used to apply colors to specific pixels on an image. This is useful for drawing or simply creating a line on an image using a specified color.

The SetPixel() method is found under the AnyBitmap class in IronSoftware.Drawing. It accepts three parameters: the x coordinate, the y coordinate, and an IronSoftware.Drawing.Color object. The method can be called on an AnyBitmap object to apply the color to the AnyBitmap image.

In the provided code snippet, a sample image was loaded using the AnyBitmap.FromFile() method, which returns an AnyBitmap object. A for loop was used to create a colored horizontal line by changing the x coordinate. The bitmap with the applied pixels is then saved to the machine with a different name.

// Load an existing image file into an AnyBitmap object
AnyBitmap bitmap = AnyBitmap.FromFile("sample_image.jpg");

// Define the color to use for the horizontal line
IronSoftware.Drawing.Color lineColor = IronSoftware.Drawing.Color.Red;

// Specify the y coordinate for the horizontal line
int yCoordinate = 50;

// Draw a horizontal line by iterating over the x coordinates
for (int x = 0; x < bitmap.Width; x++)
{
    // Set the specified pixel at (x, yCoordinate) to the line color
    bitmap.SetPixel(x, yCoordinate, lineColor);
}

// Save the modified image to a new file
bitmap.Save("sample_image_with_line.jpg");
// Load an existing image file into an AnyBitmap object
AnyBitmap bitmap = AnyBitmap.FromFile("sample_image.jpg");

// Define the color to use for the horizontal line
IronSoftware.Drawing.Color lineColor = IronSoftware.Drawing.Color.Red;

// Specify the y coordinate for the horizontal line
int yCoordinate = 50;

// Draw a horizontal line by iterating over the x coordinates
for (int x = 0; x < bitmap.Width; x++)
{
    // Set the specified pixel at (x, yCoordinate) to the line color
    bitmap.SetPixel(x, yCoordinate, lineColor);
}

// Save the modified image to a new file
bitmap.Save("sample_image_with_line.jpg");
' Load an existing image file into an AnyBitmap object
Dim bitmap As AnyBitmap = AnyBitmap.FromFile("sample_image.jpg")

' Define the color to use for the horizontal line
Dim lineColor As IronSoftware.Drawing.Color = IronSoftware.Drawing.Color.Red

' Specify the y coordinate for the horizontal line
Dim yCoordinate As Integer = 50

' Draw a horizontal line by iterating over the x coordinates
For x As Integer = 0 To bitmap.Width - 1
	' Set the specified pixel at (x, yCoordinate) to the line color
	bitmap.SetPixel(x, yCoordinate, lineColor)
Next x

' Save the modified image to a new file
bitmap.Save("sample_image_with_line.jpg")
$vbLabelText   $csharpLabel

Explanation

  1. Loading the Image: The AnyBitmap.FromFile() method is used to load an image from the specified file path and create an AnyBitmap object.
  2. Defining the Color: An IronSoftware.Drawing.Color object is created to specify the color of the line. In this snippet, a red color is used.
  3. Setting the Y Coordinate: The y coordinate determines the vertical position of the horizontal line to be drawn on the image.
  4. Drawing the Line: The for loop iterates over the width of the bitmap (bitmap.Width), applying the selected color to each pixel at the specified y coordinate using SetPixel().
  5. Saving the Modified Image: After the line is drawn, the modified bitmap is saved to a new file with a different name to preserve the original image.