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



graphics
The Graphics object used to paint.
src
Rectangle which determines the portion of the image to paint.
dest
Rectangle which determines where the image is placed, and how it is scaled.
destClip
Rectangle which clips the image display.
transform
The matrix is used to transform from physical to logical coordinates
Displays the contents of the RasterImageViewer in the given Graphics object.

Syntax

Visual Basic (Declaration) 
Public Overridable Sub RedirectPaint( _
   ByVal graphics As Graphics, _
   ByVal src As Rectangle, _
   ByVal dest As Rectangle, _
   ByVal destClip As Rectangle, _
   ByVal transform As Matrix _
) 
Visual Basic (Usage)Copy Code
Dim instance As RasterImageViewer
Dim graphics As Graphics
Dim src As Rectangle
Dim dest As Rectangle
Dim destClip As Rectangle
Dim transform As Matrix
 
instance.RedirectPaint(graphics, src, dest, destClip, transform)
C# 
public virtual void RedirectPaint( 
   Graphics graphics,
   Rectangle src,
   Rectangle dest,
   Rectangle destClip,
   Matrix transform
)
C++/CLI 
public:
virtual void RedirectPaint( 
   Graphics graphics,
   Rectangle src,
   Rectangle dest,
   Rectangle destClip,
   Matrix transform
) 

Parameters

graphics
The Graphics object used to paint.
src
Rectangle which determines the portion of the image to paint.
dest
Rectangle which determines where the image is placed, and how it is scaled.
destClip
Rectangle which clips the image display.
transform
The matrix is used to transform from physical to logical coordinates

Example

This example creates a Form with a RasterImageViewer and a panel. An image is loaded into the RasterImageViewer, and stationary and moving text are drawn on top of the image. A mirror image of RasterImageViewer contents is then painted on the panel.

Visual BasicCopy Code
Private Class MyForm2 : Inherits Form
    Private Class MyPanel : Inherits Panel
        Protected Overrides Sub OnPaintBackground(ByVal pe As PaintEventArgs)
            ' do nothing
        End Sub
    End Class

    Private viewer As RasterImageViewer
    Private panel As MyPanel

    Public Sub New(ByVal title As String)
        Text = title
        Size = New Size(750, 450)

        ' Create the raster viewer
        viewer = New RasterImageViewer()
        viewer.DoubleBuffer = True
        viewer.Dock = DockStyle.Fill

        ' Load an image into the viewer
        RasterCodecs.Startup()
        Dim codecs As RasterCodecs = New RasterCodecs()
        viewer.Image = codecs.Load(LeadtoolsExamples.Common.ImagesPath.Path + "Sample1.cmp")
        codecs.Dispose()
        RasterCodecs.Shutdown()

        ' Create the panel
        panel = New MyPanel()
        panel.Parent = Me
        panel.Width = 400
        panel.Height = 400
        panel.Dock = DockStyle.Right
        panel.BackColor = Color.Beige
        panel.BorderStyle = BorderStyle.Fixed3D

        Controls.Add(panel)
        panel.BringToFront()

        Controls.Add(viewer)
        viewer.BringToFront()

        AddHandler panel.Paint, AddressOf panel_Paint
        AddHandler viewer.PostImagePaint, AddressOf viewer_PostImagePaint
        AddHandler viewer.RedirectImagePaint, AddressOf viewer_RedirectImagePaint
    End Sub

    Private Sub viewer_RedirectImagePaint(ByVal sender As Object, ByVal e As EventArgs)
        panel.Invalidate()
    End Sub

    Private Sub panel_Paint(ByVal sender As Object, ByVal e As PaintEventArgs)
        If Not viewer.Image Is Nothing Then
            Dim dx As Integer = viewer.Image.Width \ 2
            Dim dy As Integer = viewer.Image.Height \ 2
            Dim dest As Rectangle = viewer.SourceRectangle

            ' move center of image to origin
            Dim m1 As Matrix = New Matrix(1, 0, 0, 1, -dx, -dy)

            ' mirror the image
            Dim m2 As Matrix = New Matrix(-1, 0, 0, 1, 0, 0)
            m1.Multiply(m2, MatrixOrder.Append)

            ' move back to original location
            Dim m3 As Matrix = New Matrix(1, 0, 0, 1, dx, dy)
            m1.Multiply(m3, MatrixOrder.Append)

            ' scale image to fit on panel
            Dim scaleX As Single = CSng(panel.Width) / CSng(viewer.Image.Width)
            Dim scaleY As Single = CSng(panel.Height) / CSng(viewer.Image.Height)
            Dim m4 As Matrix = New Matrix(scaleX, 0, 0, scaleY, 0, 0)
            m1.Multiply(m4, MatrixOrder.Append)

            ' display contents of RaserImageViewer on panel
            viewer.RedirectPaint(e.Graphics, Rectangle.Empty, Rectangle.Empty, Rectangle.Empty, m1)
        End If
    End Sub

    Private Sub viewer_PostImagePaint(ByVal sender As Object, ByVal e As PaintEventArgs)
        Dim graphics As Graphics = e.Graphics
        Dim graphicsState As GraphicsState = graphics.Save()

        Dim font As Font = New Font("Arial", 16)
        e.Graphics.DrawString("This text stays with the window", font, Brushes.Yellow, New PointF(100, 100))

        graphics.MultiplyTransform(viewer.Transform)
        e.Graphics.DrawString("This text stays with the image.", font, Brushes.White, New PointF(100, 200))

        graphics.Restore(graphicsState)
        graphics.Flush()
    End Sub
End Class

Public Sub RasterImageViewer_RedirectPaint(ByVal title As String)
    Dim form As MyForm2 = New MyForm2(title)
    form.ShowDialog()
End Sub
C#Copy Code
class MyForm2 : Form 

   class MyPanel : Panel 
   { 
      protected override void  OnPaintBackground(PaintEventArgs pe) 
      { 
         // do nothing 
      } 
   } 
 
   RasterImageViewer viewer; 
   MyPanel panel; 
 
   public MyForm2(string title) 
   { 
      Text = title; 
      Size = new Size(750, 450); 
 
      // Create the raster viewer 
      viewer = new RasterImageViewer(); 
      viewer.DoubleBuffer = true; 
      viewer.Dock = DockStyle.Fill; 
 
      // Load an image into the viewer 
      RasterCodecs.Startup(); 
      RasterCodecs codecs = new RasterCodecs(); 
      viewer.Image = codecs.Load(LeadtoolsExamples.Common.ImagesPath.Path + "Sample1.cmp"); 
      codecs.Dispose(); 
      RasterCodecs.Shutdown(); 
 
      // Create the panel 
      panel = new MyPanel(); 
      panel.Parent = this; 
      panel.Width = 400; 
      panel.Height = 400; 
      panel.Dock = DockStyle.Right; 
      panel.BackColor = Color.Beige; 
      panel.BorderStyle = BorderStyle.Fixed3D; 
 
      Controls.Add(panel); 
      panel.BringToFront(); 
 
      Controls.Add(viewer); 
      viewer.BringToFront(); 
 
      panel.Paint += new PaintEventHandler(panel_Paint); 
      viewer.PostImagePaint += new PaintEventHandler(viewer_PostImagePaint); 
      viewer.RedirectImagePaint += new EventHandler(viewer_RedirectImagePaint); 
   } 
 
   void viewer_RedirectImagePaint(object sender, EventArgs e) 
   { 
      panel.Invalidate(); 
   } 
 
   void panel_Paint(object sender, PaintEventArgs e) 
   { 
      if(viewer.Image != null) 
      { 
         int dx = viewer.Image.Width / 2; 
         int dy = viewer.Image.Height / 2; 
         Rectangle dest = viewer.SourceRectangle; 
 
         // move center of image to origin 
         Matrix m1 = new Matrix(1,0,0,1,-dx,-dy); 
 
         // mirror the image 
         Matrix m2 = new Matrix(-1, 0, 0, 1, 0, 0); 
         m1.Multiply(m2, MatrixOrder.Append); 
 
         // move back to original location 
         Matrix m3 = new Matrix(1, 0, 0, 1, dx, dy); 
         m1.Multiply(m3, MatrixOrder.Append); 
 
         // scale image to fit on panel 
         float scaleX = (float)panel.Width / (float)viewer.Image.Width; 
         float scaleY = (float)panel.Height / (float)viewer.Image.Height; 
         Matrix m4 = new Matrix(scaleX, 0, 0, scaleY, 0, 0); 
         m1.Multiply(m4, MatrixOrder.Append); 
 
         // display contents of RaserImageViewer on panel 
         viewer.RedirectPaint( 
            e.Graphics, 
            Rectangle.Empty, //_viewer.SourceRectangle, 
            Rectangle.Empty, 
            Rectangle.Empty, //e.ClipRectangle,  
            m1); 
      } 
   } 
 
   private void viewer_PostImagePaint(object sender, PaintEventArgs e) 
   { 
      Graphics graphics = e.Graphics; 
      GraphicsState graphicsState = graphics.Save(); 
 
      Font font = new Font("Arial", 16); 
      e.Graphics.DrawString("This text stays with the window", font, Brushes.Yellow, new PointF(100,100)); 
 
      graphics.MultiplyTransform(viewer.Transform); 
      e.Graphics.DrawString("This text stays with the image.", font, Brushes.White, new PointF(100,200)); 
 
      graphics.Restore(graphicsState); 
      graphics.Flush(); 
   } 

 
public void RasterImageViewer_RedirectPaint(string title) 

   MyForm2 form = new MyForm2(title); 
   form.ShowDialog(); 
}

Remarks

Use this function to paint the contents of the RasterImageViewer onto any Graphics object. The contents of the RasterImageViewer includes the Image and anything painted in the PostImagePaint event handler. When using this function, pass either a valid transform or a dest rectangle, but NOT BOTH. It is an error to pass both a transform and a dest rectangle.

If you pass a transformmatrix, it is applied to the graphics object before any painting takes place. If you pass a dest rectangle, internally a transform is constructed from src and dest and it is applied to the graphics object before any painting takes place. Passing Rectangle.Empty for the src rectangle gives the default, which is equivalent to the size of the Image. Passing Rectangle.Empty for the destClip rectangle is equivalent to passing same Rectangle as dst. Passing null for the transform argument means that src and dest are used to create a transform.

Requirements

Target Platforms: Microsoft .NET Framework 2.0, Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also