Leadtools Send comments on this topic. | Back to Introduction - All Topics | Help Version 16.5.9.25
GetRegionClipSegments Method
See Also  Example
Leadtools Namespace > RasterImage Class : GetRegionClipSegments Method



row
The number of the row for which to get the segments. The first row is 0, and the last row is 1 less than the image height.
segmentsBuffer
Buffer to be updated with the segments from row contained in the region.
segmentsBufferOffset
Zero based index into segmentsBuffer.
Gets the segments contained in the region for a particular row.

Syntax

Visual Basic (Declaration)  
Public Function GetRegionClipSegments( _
   ByVal row As Integer, _
   ByVal segmentsBuffer() As Integer, _
   ByVal segmentsBufferOffset As Integer _
) As Integer
Visual Basic (Usage) Copy Code
Dim instance As RasterImage
Dim row As Integer
Dim segmentsBuffer() As Integer
Dim segmentsBufferOffset As Integer
Dim value As Integer
 
value = instance.GetRegionClipSegments(row, segmentsBuffer, segmentsBufferOffset)
C#  
public int GetRegionClipSegments( 
   int row,
   int[] segmentsBuffer,
   int segmentsBufferOffset
)
C++/CLI  
public:
int GetRegionClipSegments( 
   int row,
   array<int>^ segmentsBuffer,
   int segmentsBufferOffset
) 

Parameters

row
The number of the row for which to get the segments. The first row is 0, and the last row is 1 less than the image height.
segmentsBuffer
Buffer to be updated with the segments from row contained in the region.
segmentsBufferOffset
Zero based index into segmentsBuffer.

Return Value

The number of segments from row that are contained in the region.

Example

This example sets the value of pixels inside a region to half intensity.

Visual Basic Copy Code
Public Sub GetRegionClipSegmentsExample()
   RasterCodecs.Startup()
   Dim codecs As RasterCodecs = New RasterCodecs()

   Dim srcFileName As String = LeadtoolsExamples.Common.ImagesPath.Path + "Image1.cmp"
   Dim destFileName As String = LeadtoolsExamples.Common.ImagesPath.Path + "Image1_GetRegionClipSegments.bmp"

   ' Load the image
   Dim image As RasterImage = codecs.Load(srcFileName)

   ' Add a region to the image
   Dim rc As Rectangle = New Rectangle(image.Width \ 3, image.Height \ 3, image.Width \ 3, image.Height \ 3)
   image.AddEllipseToRegion(Nothing, rc, RasterRegionCombineMode.Set)

   ' Set the pixels inside the region to black

   ' Get the maximum number of elements in a row,
   ' so we know how big the array of segments should be
   Dim maxClipSegments As Integer = image.CalculateRegionMaximumClipSegments()

   ' Get the region bounds, so we know which is the first row
   Dim rgnBounds As Rectangle = image.GetRegionBounds(Nothing)

   ' Allocate an array large enough to store the maximum number of segments.
   Dim segmentBuffer As Integer() = New Integer(maxClipSegments - 1) {}

   ' Loop through the region rows
   Dim y As Integer = rgnBounds.Top
   Do While y < rgnBounds.Bottom
      ' Get the segments for the row
      image.GetRegionClipSegments(y, segmentBuffer, 0)

      ' Do something with the pixels
      Dim i As Integer = 0
      Do While i < maxClipSegments / 2
         Dim j As Integer = segmentBuffer(i)
         Do While j < segmentBuffer(i + 1)
            Dim clr As RasterColor = image.GetPixelColor(y, j)
            clr = New RasterColor(clr.R \ 2, clr.G \ 2, clr.B \ 2)
            image.SetPixelColor(y, j, clr)
            j += 1
         Loop
         i += 1
      Loop
      y += 1
   Loop

   ' Save the image back to file
   codecs.Save(image, destFileName, RasterImageFormat.Bmp, 24)

   image.Dispose()
   codecs.Dispose()
   RasterCodecs.Shutdown()
End Sub
C# Copy Code
public void GetRegionClipSegmentsExample() 

   RasterCodecs.Startup(); 
   RasterCodecs codecs = new RasterCodecs(); 
 
   string srcFileName = LeadtoolsExamples.Common.ImagesPath.Path + "Image1.cmp"; 
   string destFileName = LeadtoolsExamples.Common.ImagesPath.Path + "Image1_GetRegionClipSegments.bmp"; 
 
   // Load the image 
   RasterImage image = codecs.Load(srcFileName); 
 
   // Add a region to the image 
   Rectangle rc = new Rectangle(image.Width / 3, image.Height / 3, image.Width / 3, image.Height / 3); 
   image.AddEllipseToRegion(null, rc, RasterRegionCombineMode.Set); 
 
   // Set the pixels inside the region to black 
 
   // Get the maximum number of elements in a row, 
   // so we know how big the array of segments should be 
   int maxClipSegments = image.CalculateRegionMaximumClipSegments(); 
 
   // Get the region bounds, so we know which is the first row 
   Rectangle rgnBounds = image.GetRegionBounds(null); 
 
   // Allocate an array large enough to store the maximum number of segments. 
   int[] segmentBuffer = new int[maxClipSegments]; 
 
   // Loop through the region rows 
   for(int y = rgnBounds.Top; y < rgnBounds.Bottom; y++) 
   { 
      // Get the segments for the row 
      image.GetRegionClipSegments(y, segmentBuffer, 0); 
 
      // Do something with the pixels 
      for(int i = 0; i < maxClipSegments / 2; i++) 
      { 
         for(int j = segmentBuffer[i]; j < segmentBuffer[i + 1]; j++) 
         { 
            RasterColor clr = image.GetPixelColor(y, j); 
            clr = new RasterColor(clr.R / 2, clr.G / 2, clr.B / 2); 
            image.SetPixelColor(y, j, clr); 
         } 
      } 
   } 
 
   // Save the image back to file 
   codecs.Save(image, destFileName, RasterImageFormat.Bmp, 24); 
 
   image.Dispose(); 
   codecs.Dispose(); 
   RasterCodecs.Shutdown(); 
}

Remarks

To use this method, first call GetRegionBounds with xform set to null (Nothing in Visual Basic), to get the image boundaries. The bounding rectangle will indicate which rows are contained in the region. Go through all the rows contained in the region to get the segments contained in the region.

The segments are returned as an array of pairs of horizontal offsets. The first point in the pair is the beginning of the segment (it is contained in the region). The last point in the pair is the end of the segment. To follow the Windows rules, the end of the segment is the first point NOT CONTAINED in the region.

In most regions, there will be one segment per row. However, some regions can have 0, 1, 2 or more segments.

For example, assume that for a particular row there are two segments. segmentBuffer will be filled with 4 values. Let's call them x0, x1, x2, x3. In this case:

  • portion from 0 to x0 - 1 is OUTSIDE the region
  • portion from x0 to x1 - 1 is INSIDE the region
  • portion from x1 to x2 - 1 is OUTSIDE the region
  • portion from x2 to x3 - 1 is INSIDE the region
  • portion from x3 to Width - 1 is OUTSIDE the region

Requirements

Target Platforms: Microsoft .NET Framework 3.0, Windows XP, Windows Server 2003 family, Windows Server 2008 family

See Also