Helper method to create the transformation matrix used to redirect the rendering of the ImageViewer.
public void CreateTransform(
ImageViewer imageViewer,
LeadRect destinationRect,
LeadRectD sourceRect,
ControlSizeMode sizeMode,
ControlAlignment horizontalAlignment,
ControlAlignment verticalAlignment
)
public:
void CreateTransform(
ImageViewer^ imageViewer,
LeadRect^ destinationRect,
LeadRectD^ sourceRect,
ControlSizeMode^ sizeMode,
ControlAlignment^ horizontalAlignment,
ControlAlignment^ verticalAlignment
)
imageViewer
The image viewer instance that will be used as the source of RenderRedirect. This value
cannot be null.
destinationRect
Destination rectangle to use. For example, the client area of the external control.
sourceRect
The source rectangle into the current view of imageViewer. Pass Empty to render
the whole view.
sizeMode
Controls how the source rectangle is positioned and scaled inside the destination rectangle.
horizontalAlignment
Controls how the source rectangle is aligned horizontally inside the destination rectangle.
verticalAlignment
Controls how the source rectangle is aligned vertically inside the destination rectangle.
This method will set the transformation matrix into Transform.
Use this method CreateTransform can be used to easily create a transformation matrix for common scenarios such as rendering a thumbnail of the content of the viewer.
For more information, refer to Image Viewer Rendering.
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 image
using (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 control
Form redirectRenderForm = new Form();
redirectRenderControl = new PictureBox();
redirectRenderControl.Width = 400;
redirectRenderControl.Dock = DockStyle.Fill;
redirectRenderForm.Controls.Add(redirectRenderControl);
redirectRenderForm.Show();
// Invalidate on double-click
bool renderView = false;
redirectRenderControl.DoubleClick += (sender, e) =>
{
renderView = !renderView;
redirectRenderControl.Invalidate();
};
redirectRenderControl.Paint += (sender, e) =>
{
if (_imageViewer.Image == null)
return;
// Get PictureBox Graphics object
Graphics graphics = e.Graphics;
// Destination rectangle in PictureBox
int margin = 20;
LeadRect destRect = LeadRect.Create(margin, margin, redirectRenderControl.ClientSize.Width - margin * 2, redirectRenderControl.ClientSize.Height - margin * 2);
LeadRect clipRect = destRect;
// Source rectangle
Debug.WriteLine(_imageViewer.ViewSize);
// RenderRedirect Options
ImageViewerRenderRedirectOptions options = new ImageViewerRenderRedirectOptions();
options.BackgroundColor = Color.Transparent;
// Try to get item from the viewer
ImageViewerItem 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 view
LeadRectD rect;
if (item == null)
rect = _imageViewer.GetViewBounds(true, true);
else
rect = _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 view
LeadRectD 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";
}
Help Collections
Raster .NET | C API | C++ Class Library | HTML5 JavaScript
Document .NET | C API | C++ Class Library | HTML5 JavaScript
Medical .NET | C API | C++ Class Library | HTML5 JavaScript
Medical Web Viewer .NET
Multimedia
Direct Show .NET | C API | Filters
Media Foundation .NET | C API | Transforms
Supported Platforms
.NET, Java, Android, and iOS/macOS Assemblies
Imaging, Medical, and Document
C API/C++ Class Libraries
Imaging, Medical, and Document
HTML5 JavaScript Libraries
Imaging, Medical, and Document
Your email has been sent to support! Someone should be in touch! If your matter is urgent please come back into chat.
Chat Hours:
Monday - Friday, 8:30am to 6pm ET
Thank you for your feedback!
Please fill out the form again to start a new chat.
All agents are currently offline.
Chat Hours:
Monday - Friday
8:30AM - 6PM EST
To contact us please fill out this form and we will contact you via email.