RedirectPaint Method

Summary

Displays the contents of the RasterImageViewer in the given Graphics object.

Syntax

C#
C++/CLI
C++
public virtual void RedirectPaint( 
   Graphics graphics, 
   Rectangle src, 
   Rectangle dest, 
   Rectangle destClip, 
   Matrix transform 
) 
public: 
virtual void RedirectPaint(  
   Graphics^ graphics, 
   Rectangle src, 
   Rectangle dest, 
   Rectangle destClip, 
   Matrix^ transform 
)  
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

Remarks

Use this function to paint the contents of the RasterImageViewer onto any System.Drawing.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 System.Drawing.Rectangle as dst. Passing null for the transform argument means that src and dest are used to create a transform.

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.

C#
using Leadtools.WinForms; 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing.Color; 
using Leadtools.Drawing; 
 
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 codecs = new RasterCodecs(); 
      viewer.Image = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "Sample1.cmp")); 
      codecs.Dispose(); 
 
      // 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(); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS22\Resources\Images"; 
} 

Requirements

Target Platforms

See Also

Reference

RasterImageViewer Class

RasterImageViewer Members

Help Version 22.0.2023.2.9
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.WinForms Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.