Displays an outline of the image region in the given
Graphics object.
Syntax
Parameters
- graphics
- Graphics object where the image is displayed and where the frame is to appear.
- xform
- RasterRegionXForm object that LEADTOOLS uses to translate between display coordinates and image coordinates.
- frameIndex
- Zero-index of the frame to display. Possible values are from 0 to MaxRegionFrameIndex. You can animate the region frame by cycling between these values.
Example
Visual Basic |
Copy Code |
Public Sub FrameRegionExample() Dim f As FrameRegionForm = New FrameRegionForm() f.ShowDialog() End Sub
Private Class FrameRegionForm : Inherits Form Private frameIndex As Integer Private image As RasterImage Private timer As System.Windows.Forms.Timer Private fillRegion As Boolean
Public Sub New() RasterCodecs.Startup() Dim codecs As RasterCodecs = New RasterCodecs()
Dim srcFileName As String = LeadtoolsExamples.Common.ImagesPath.Path + "Image1.cmp" image = codecs.Load(srcFileName)
Dim rc As Rectangle = New Rectangle(image.Width \ 3, image.Height \ 3, image.Width \ 3, image.Height \ 3) Dim xform As RasterRegionXForm = New RasterRegionXForm() xform.ViewPerspective = RasterViewPerspective.TopLeft image.AddEllipseToRegion(xform, rc, RasterRegionCombineMode.Set)
frameIndex = 0
fillRegion = True
Text = "Double click to enable/disable filling the region"
timer = New System.Windows.Forms.Timer() timer.Interval = 100 AddHandler timer.Tick, AddressOf timer_Tick timer.Start() End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then timer.Dispose() image.Dispose() RasterCodecs.Shutdown() End If
MyBase.Dispose(disposing) End Sub
Protected Overrides Sub OnDoubleClick(ByVal e As EventArgs) fillRegion = Not fillRegion Invalidate()
MyBase.OnDoubleClick(e) End Sub
Private Function GetXForm(ByVal destRect As Rectangle) As RasterRegionXForm Dim xform As RasterRegionXForm = New RasterRegionXForm() xform.ViewPerspective = RasterViewPerspective.TopLeft xform.XOffset = destRect.Left xform.YOffset = destRect.Top xform.XScalarDenominator = image.Width xform.XScalarNumerator = destRect.Width xform.YScalarDenominator = image.Height xform.YScalarNumerator = destRect.Height
Return xform End Function
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim destRect As Rectangle = RasterImage.CalculatePaintModeRectangle(image.ImageWidth, image.ImageHeight, ClientRectangle, RasterPaintSizeMode.Fit, RasterPaintAlignMode.Center, RasterPaintAlignMode.Center) image.Paint(e.Graphics, Rectangle.Empty, Rectangle.Empty, destRect, e.ClipRectangle, RasterPaintProperties.Default)
If fillRegion Then Dim xform As RasterRegionXForm = GetXForm(destRect) image.FillRegion(e.Graphics, xform, New RasterColor(255, 0, 255)) End If
MyBase.OnPaint(e) End Sub
Private Sub timer_Tick(ByVal sender As Object, ByVal e As EventArgs) Dim destRect As Rectangle = RasterImage.CalculatePaintModeRectangle(image.ImageWidth, image.ImageHeight, ClientRectangle, RasterPaintSizeMode.Fit, RasterPaintAlignMode.Center, RasterPaintAlignMode.Center)
Dim xform As RasterRegionXForm = GetXForm(destRect)
Dim g As Graphics = CreateGraphics() Try image.FrameRegion(g, xform, frameIndex) Finally CType(g, IDisposable).Dispose() End Try
frameIndex += 1 If frameIndex > RasterImage.MaxRegionFrameIndex Then frameIndex = 0 End If End Sub End Class |
C# |
Copy Code |
public void FrameRegionExample() { FrameRegionForm f = new FrameRegionForm(); f.ShowDialog(); } class FrameRegionForm : Form { int frameIndex; RasterImage image; System.Windows.Forms.Timer timer; bool fillRegion; public FrameRegionForm() { // Load the image RasterCodecs.Startup(); RasterCodecs codecs = new RasterCodecs(); string srcFileName = LeadtoolsExamples.Common.ImagesPath.Path + "Image1.cmp"; 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); RasterRegionXForm xform = new RasterRegionXForm(); xform.ViewPerspective = RasterViewPerspective.TopLeft; image.AddEllipseToRegion(xform, rc, RasterRegionCombineMode.Set); // initialize the frame index frameIndex = 0; fillRegion = true; Text = "Double click to enable/disable filling the region"; // Create the timer timer = new System.Windows.Forms.Timer(); timer.Interval = 100; timer.Tick += new EventHandler(timer_Tick); timer.Start(); } protected override void Dispose(bool disposing) { // Clean up if(disposing) { timer.Dispose(); image.Dispose(); RasterCodecs.Shutdown(); } base.Dispose(disposing); } protected override void OnDoubleClick(EventArgs e) { fillRegion = !fillRegion; Invalidate(); base.OnDoubleClick(e); } RasterRegionXForm GetXForm(Rectangle destRect) { // Calculate xform when the image is painted into 'destRect' RasterRegionXForm xform = new RasterRegionXForm(); xform.ViewPerspective = RasterViewPerspective.TopLeft; xform.XOffset = destRect.Left; xform.YOffset = destRect.Top; xform.XScalarDenominator = image.Width; xform.XScalarNumerator = destRect.Width; xform.YScalarDenominator = image.Height; xform.YScalarNumerator = destRect.Height; return xform; } protected override void OnPaint(PaintEventArgs e) { // Draw the image fit and center on this form Rectangle destRect = RasterImage.CalculatePaintModeRectangle( image.ImageWidth, image.ImageHeight, ClientRectangle, RasterPaintSizeMode.Fit, RasterPaintAlignMode.Center, RasterPaintAlignMode.Center); image.Paint(e.Graphics, Rectangle.Empty, Rectangle.Empty, destRect, e.ClipRectangle, RasterPaintProperties.Default); if(fillRegion) { RasterRegionXForm xform = GetXForm(destRect); image.FillRegion(e.Graphics, xform, new RasterColor(255, 0, 255)); } base.OnPaint(e); } void timer_Tick(object sender, EventArgs e) { // Frame the image region Rectangle destRect = RasterImage.CalculatePaintModeRectangle( image.ImageWidth, image.ImageHeight, ClientRectangle, RasterPaintSizeMode.Fit, RasterPaintAlignMode.Center, RasterPaintAlignMode.Center); RasterRegionXForm xform = GetXForm(destRect); using(Graphics g = CreateGraphics()) image.FrameRegion(g, xform, frameIndex); // advance to next frame frameIndex++; if(frameIndex > RasterImage.MaxRegionFrameIndex) frameIndex = 0; } } |
Remarks
Requirements
Target Platforms: Microsoft .NET Framework 3.0, Windows XP, Windows Server 2003 family, Windows Server 2008 family
See Also