Render the content of the viewer to an external context.
public virtual void RenderRedirect(Graphics graphics,ImageViewerRenderRedirectOptions options,LeadRect clipRectangle)
graphics
Target device context to render the content of the viewer to.
options
Rendering options
clipRectangle
Clipping rectangle to use.
This method can be used to render the content of the viewer to an external context. ImageViewerPanControl uses this method to show a smaller version of the viewer in a separate external control.
To take a "snapshot" of the viewer surface to perform such operations as screen capture or printing, use RenderRedirect passing it the target context (of the printer or a bitmap).
To perform live updates for operations such as pan window, subscribe to the RedirectRender event to get notified whenever the viewer content is changed. Then call RenderRedirect to render the content of the viewer into the target device.
During the render cycle, the value of IsRenderRedirected will be set to true if this particular rendering operation is targeting an external device other than the viewer itself. If you are performing custom rendering, you can check the value of this property to apply any modification needed.
This example will mirror the content of the viewer into an external control with live updates whenever the viewer content changes.
Start with the ImageViewer example, remove all the code inside the example function (search for the "// TODO: add example code here" comment) and insert the following code:
using Leadtools;using Leadtools.Controls;using Leadtools.Codecs;using Leadtools.Drawing;using Leadtools.ImageProcessing;using Leadtools.ImageProcessing.Color;public ImageViewerForm _form = new ImageViewerForm();public ImageViewer _imageViewer;public PictureBox redirectRenderControl;public void ImageViewerRedirectRenderExample(){// Get the ImageViewer control from the form_imageViewer = _form.ImageViewer;// Load an imageusing (var codecs = new RasterCodecs())_imageViewer.Image = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "cannon.jpg"));// PanZoom Interactive Mode_imageViewer.DefaultInteractiveMode = new ImageViewerPanZoomInteractiveMode();// Separate Form with PictureBox controlForm redirectRenderForm = new Form();redirectRenderControl = new PictureBox();redirectRenderControl.Width = 400;redirectRenderControl.Dock = DockStyle.Fill;redirectRenderForm.Controls.Add(redirectRenderControl);redirectRenderForm.Show();// Invalidate on double-clickbool renderView = false;redirectRenderControl.DoubleClick += (sender, e) =>{renderView = !renderView;redirectRenderControl.Invalidate();};redirectRenderControl.Paint += (sender, e) =>{if (_imageViewer.Image == null)return;// Get PictureBox Graphics objectGraphics graphics = e.Graphics;// Destination rectangle in PictureBoxint margin = 20;LeadRect destRect = LeadRect.Create(margin, margin, redirectRenderControl.ClientSize.Width - margin * 2, redirectRenderControl.ClientSize.Height - margin * 2);LeadRect clipRect = destRect;// Source rectangleDebug.WriteLine(_imageViewer.ViewSize);// RenderRedirect OptionsImageViewerRenderRedirectOptions options = new ImageViewerRenderRedirectOptions();options.BackgroundColor = Color.Transparent;// Try to get item from the viewerImageViewerItem item = null;if (!renderView)item = _imageViewer.Items[0];LeadRectD sourceRect = LeadRectD.Empty;if (item == null)sourceRect = _imageViewer.GetViewBounds(true, false);else{sourceRect = _imageViewer.GetItemViewBounds(item, ImageViewerItemPart.Image, false);options.RenderBackgrounds = false;options.RenderBorders = false;options.RenderItemStates = false;options.RenderShadows = false;options.RenderText = false;}// Transformation Matrix (represents rotation, zooming...etc)Debug.WriteLine(_imageViewer.ViewTransform);options.CreateTransform(_imageViewer, destRect, sourceRect, ControlSizeMode.FitAlways, ControlAlignment.Center, ControlAlignment.Center);clipRect = options.Transform.TransformRect(sourceRect).ToLeadRect();// Redirect rendering to the PictureBox Graphics object (Outlined with a black rectangle)_imageViewer.RenderRedirect(graphics, options, clipRect);graphics.DrawRectangle(Pens.Black, destRect.X, destRect.Y, destRect.Width + 1, destRect.Height + 1);// Calculate visible viewLeadRectD rect;if (item == null)rect = _imageViewer.GetViewBounds(true, true);elserect = _imageViewer.GetItemViewBounds(item, ImageViewerItemPart.Image, true);LeadPointD[] points ={LeadPointD.Create(rect.Left, rect.Top),LeadPointD.Create(rect.Right, rect.Bottom)};options.Transform.TransformPoints(points);double xmin = points[0].X;double ymin = points[0].Y;double xmax = xmin;double ymax = ymin;for (int i = 1; i < points.Length; i++){if (points[i].X < xmin) xmin = points[i].X;if (points[i].X > xmax) xmax = points[i].X;if (points[i].Y < ymin) ymin = points[i].Y;if (points[i].Y > ymax) ymax = points[i].Y;}// Draw yellow rectangle showing visible part of the viewLeadRectD bounds = LeadRectD.FromLTRB(xmin, ymin, xmax, ymax);RectangleF rc = new RectangleF((float)bounds.X, (float)bounds.Y, (float)bounds.Width, (float)bounds.Height);graphics.DrawRectangle(Pens.Yellow, rc.X, rc.Y, rc.Width - 1, rc.Height - 1);};_imageViewer.RedirectRender += (sender, e) =>{redirectRenderControl.Invalidate();};}static class LEAD_VARS{public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images";}