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 maxscaleFactor = Math.Max(minimumScaleFactor, Math.Min(maximumScaleFactor, scaleFactor));// Check if we need to change the scale factor for the viewerif (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 coordinatesRectangle rc = Rectangle.Intersect(viewer.PhysicalViewRectangle, viewer.ClientRectangle);// Get the center of what you see in physical coordinatesPointF 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 coordinatescenter = t.PointToLogical(center);// Set the new scale factorviewer.ScaleFactor = scaleFactor;// Bring the original center into the view centert = new Transformer(viewer.Transform);// Get the center of what you saw before the zoom in physical coordinatescenter = t.PointToPhysical(center);// Bring the old center into the center of the viewviewer.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 RectangleRectangleF recLogical = t.RectangleToLogical(rc);// Get the physical coordinates of the RectangleRectangleF recPhysical = t.RectangleToPhysical(rc);// Get the logical lengthfloat xlengthLogical = t.LengthToLogical(center.X);// Get the logical lengthfloat xlengthPhysical = t.LengthToPhysical(center.X);Console.WriteLine($"recLogical: {recLogical}\nrecPhysical{recPhysical}\nxlengthLogical :{xlengthLogical}\nxlengthPhysical:{xlengthPhysical}");// Convert input logical points to physical points// Define logical pointsPointF[] 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 pointsPointF[] 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}");}}}