public class Transformer
The example shows how to use the Transformer
members to convert physical to logical coordinates and vice versa.
The converted coordinates are then used to zoom in the image while at the center point.
Before | After |
---|---|
using Leadtools.Codecs;
using Leadtools.Drawing;
using Leadtools.WinForms;
public void CenterAtPoint(RasterImageViewer viewer)
{
// Minimum and maximum scale factors allowed (change if you have to)
const double minimumScaleFactor = 0.05;
const double maximumScaleFactor = 11;
// Normalize the scale factor based on min and max
scaleFactor = Math.Max(minimumScaleFactor, Math.Min(maximumScaleFactor, scaleFactor));
// Check if we need to change the scale factor for the viewer
if (viewer.ScaleFactor != scaleFactor)
{
viewer.BeginUpdate();
// Get the current center in logical units
// We will use this point later to re-center the viewer
// Get what you see in physical coordinates
Rectangle rc = Rectangle.Intersect(viewer.PhysicalViewRectangle, viewer.ClientRectangle);
// Get the center of what you see in physical coordinates
PointF center = new PointF(rc.Left + rc.Width / 2, rc.Top + rc.Height / 2);
Transformer t = new Transformer(viewer.Transform);
// Get the center of what you see in logical coordinates
center = t.PointToLogical(center);
// Set the new scale factor
viewer.ScaleFactor = scaleFactor;
// Bring the original center into the view center
t = new Transformer(viewer.Transform);
// Get the center of what you saw before the zoom in physical coordinates
center = t.PointToPhysical(center);
// Bring the old center into the center of the view
viewer.CenterAtPoint(Point.Round(center));
viewer.EndUpdate();
//Code below is informational and only provides values for Transformer members
// and does not impact the image in this demo:
// Get the logical coordinates of the Rectangle
RectangleF recLogical = t.RectangleToLogical(rc);
// Get the physical coordinates of the Rectangle
RectangleF recPhysical = t.RectangleToPhysical(rc);
// Get the logical length
float xlengthLogical = t.LengthToLogical(center.X);
// Get the logical length
float xlengthPhysical = t.LengthToPhysical(center.X);
Console.WriteLine($"recLogical: {recLogical}\nrecPhysical{recPhysical}\nxlengthLogical :{xlengthLogical}\nxlengthPhysical:{xlengthPhysical}");
// Convert input logical points to physical points
// Define logical points
PointF[] points = new PointF[2];
points[0].X = 10.00F;
points[0].Y = 10.00F;
points[0].X = 20.00F;
points[0].Y = 20.00F;
// Convert to physical points
PointF[] physicalPoints = Transformer.TransformPoints(points, viewer.Transform);
for (int i = 0; i < physicalPoints.Length; i++)
{
Console.WriteLine($"Logical Points: {physicalPoints[i].X}, {physicalPoints[i].Y}");
Console.WriteLine($"Physical Points:{physicalPoints[i].X}, {physicalPoints[i].Y}");
}
}
}