SelectionRegion Свойство (TextRegionLine)
Свойство SelectionRegion возвращает одну область.
Свойство SelectionRegions возвращает несколько областей. Каждая область представляет собой группу символов. Символы группируются, если они имеют одинаковый размер и расположены очень близко.
Этот C#/VB.NET код показывает, как нарисовать область выделения на изображении.
''' <summary>
''' Draws the selection region for text region line on specified image.
''' </summary>
''' <param name="image">An image that contains text and where selection region must be drawn.</param>
''' <returns>
''' A clone of image that contains text and with drawn selection region.
''' </returns>
Public Shared Function DrawTextRegionSelection(image As Vintasoft.Imaging.VintasoftImage) As Vintasoft.Imaging.VintasoftImage
' create result image
Dim result As Vintasoft.Imaging.VintasoftImage = DirectCast(image.Clone(), Vintasoft.Imaging.VintasoftImage)
' get the text region lines
Dim textRegionLines As Vintasoft.Imaging.Text.TextRegionLine() = image.Metadata.TextRegion.Lines
' if image has text regions
If textRegionLines.Length > 0 Then
' create the drawing engine
Using darawingEngine As Vintasoft.Imaging.Drawing.DrawingEngine = result.CreateDrawingEngine()
' create transform from text to an image space
Dim textToImageTransform As Vintasoft.Imaging.AffineMatrix = image.Metadata.TextRegion.GetTransformFromTextToImageSpace(image.Resolution)
' create brushes
Using lineBrush As Vintasoft.Imaging.Drawing.IDrawingBrush = darawingEngine.DrawingFactory.CreateSolidBrush(New Vintasoft.Imaging.ImageColors.Argb32Color(128, 0, 128, 0))
Using symbolPen As Vintasoft.Imaging.Drawing.IDrawingPen = darawingEngine.DrawingFactory.CreatePen(New Vintasoft.Imaging.ImageColors.Argb32Color(255, 0, 0, 255), 1)
For Each textRegionLine As Vintasoft.Imaging.Text.TextRegionLine In textRegionLines
' get points of text region line
Dim linePoints As System.Drawing.PointF() = textRegionLine.SelectionRegion.ToPolygon()
' transform points
Vintasoft.Imaging.PointFAffineTransform.TransformPoints(textToImageTransform, linePoints)
' fill line polygon
darawingEngine.FillPolygon(lineBrush, linePoints)
' for each symbol in text region line
For Each textRegionSymbol As Vintasoft.Imaging.Text.TextRegionSymbol In textRegionLine.Symbols
' get points of text symbol
Dim symbolPoints As System.Drawing.PointF() = textRegionSymbol.SelectionRegion.ToPolygon()
' transform points
Vintasoft.Imaging.PointFAffineTransform.TransformPoints(textToImageTransform, symbolPoints)
' fill symbol polygon
darawingEngine.DrawPolygon(symbolPen, symbolPoints)
Next
Next
End Using
End Using
End Using
End If
Return result
End Function
/// <summary>
/// Draws the selection region for text region line on specified image.
/// </summary>
/// <param name="image">An image that contains text and where selection region must be drawn.</param>
/// <returns>
/// A clone of image that contains text and with drawn selection region.
/// </returns>
public static Vintasoft.Imaging.VintasoftImage DrawTextRegionSelection(Vintasoft.Imaging.VintasoftImage image)
{
// create result image
Vintasoft.Imaging.VintasoftImage result = (Vintasoft.Imaging.VintasoftImage)image.Clone();
// get the text region lines
Vintasoft.Imaging.Text.TextRegionLine[] textRegionLines = image.Metadata.TextRegion.Lines;
// if image has text regions
if (textRegionLines.Length > 0)
{
// create the drawing engine
using (Vintasoft.Imaging.Drawing.DrawingEngine darawingEngine = result.CreateDrawingEngine())
{
// create transform from text to an image space
Vintasoft.Imaging.AffineMatrix textToImageTransform = image.Metadata.TextRegion.GetTransformFromTextToImageSpace(image.Resolution);
// create brushes
using (Vintasoft.Imaging.Drawing.IDrawingBrush lineBrush = darawingEngine.DrawingFactory.CreateSolidBrush(new Vintasoft.Imaging.ImageColors.Argb32Color(128, 0, 128, 0)))
{
using (Vintasoft.Imaging.Drawing.IDrawingPen symbolPen = darawingEngine.DrawingFactory.CreatePen(new Vintasoft.Imaging.ImageColors.Argb32Color(255, 0, 0, 255), 1))
{
foreach (Vintasoft.Imaging.Text.TextRegionLine textRegionLine in textRegionLines)
{
// get points of text region line
System.Drawing.PointF[] linePoints = textRegionLine.SelectionRegion.ToPolygon();
// transform points
Vintasoft.Imaging.PointFAffineTransform.TransformPoints(textToImageTransform, linePoints);
// fill line polygon
darawingEngine.FillPolygon(lineBrush, linePoints);
// for each symbol in text region line
foreach (Vintasoft.Imaging.Text.TextRegionSymbol textRegionSymbol in textRegionLine.Symbols)
{
// get points of text symbol
System.Drawing.PointF[] symbolPoints = textRegionSymbol.SelectionRegion.ToPolygon();
// transform points
Vintasoft.Imaging.PointFAffineTransform.TransformPoints(textToImageTransform, symbolPoints);
// fill symbol polygon
darawingEngine.DrawPolygon(symbolPen, symbolPoints);
}
}
}
}
}
}
return result;
}
Целевые платформы: .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5