Return the transform of the viewer taking into account the DPI of the image.
public Matrix GetTransformWithDpi()
A System.Drawing.Drawing2D.Matrix object containing the values of the transform of the viewer taking into account the DPI of the image
Use this instead of the Transform property when the value of the UseDpi property is set to true.
If the value of the UseDpi property is false, then the return value of this method is an exact copy of the Transform matrix.
Note, since this method returns a copy of the matrix used inside this RasterImageViewer object, you need to call Matrix.Dispose on the return object.
This example use the GetTransformWithDpi method to perform rubber band selection on a RasterImageViewer.
using Leadtools.WinForms;
using Leadtools;
using Leadtools.Codecs;
using Leadtools.ImageProcessing.Color;
using Leadtools.Drawing;
class MyForm4 : Form
{
private RasterImageViewer _viewer;
// Are we currently rubber banding
private bool isRubberBanding;
// The current rubberband rectangle
private Rectangle rubberBandingRectangle;
// Flag to indicates if the rubber band rectangle is drawn
private bool isRubberBandingRectangleDrawn;
// Did we clicp the cursor?
private bool isCursorClipped;
private Rectangle saveClipRectangle;
public MyForm4(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();
Controls.Add(_viewer);
_viewer.BringToFront();
StartRubberBanding();
}
// Call this to start rubber banding on the viewer
private void StartRubberBanding()
{
// Subclass to the event we need
_viewer.MouseDown += new MouseEventHandler(_viewer_MouseDown);
_viewer.MouseMove += new MouseEventHandler(_viewer_MouseMove);
_viewer.MouseUp += new MouseEventHandler(_viewer_MouseUp);
_viewer.LostFocus += new EventHandler(_viewer_LostFocus);
}
// Call this to stop rubber banding on the viewer
private void StopRubberBanding()
{
// Un-subclass to the events
_viewer.MouseDown -= new MouseEventHandler(_viewer_MouseDown);
_viewer.MouseMove -= new MouseEventHandler(_viewer_MouseMove);
_viewer.MouseUp -= new MouseEventHandler(_viewer_MouseUp);
_viewer.LostFocus -= new EventHandler(_viewer_LostFocus);
isRubberBanding = false;
rubberBandingRectangle = Rectangle.Empty;
isRubberBandingRectangleDrawn = false;
isCursorClipped = false;
saveClipRectangle = Rectangle.Empty;
}
// Begins the rubber banding operation
private void BeginRubberBanding(int x, int y)
{
rubberBandingRectangle = Rectangle.FromLTRB(x, y, x, y);
isRubberBanding = true;
_viewer.Capture = true;
// Clip the cursor
ClipCursor(true);
DrawRubberBandRectangle();
}
// Ends the rubber banding
private void EndRubberBanding()
{
_viewer.Capture = false;
isRubberBanding = false;
if (isRubberBandingRectangleDrawn)
DrawRubberBandRectangle();
if (isCursorClipped)
ClipCursor(false);
}
private void ClipCursor(bool clip)
{
if (clip)
{
Rectangle rect = Rectangle.Intersect(FixRectangle(_viewer.PhysicalViewRectangle), _viewer.ClientRectangle);
rect = _viewer.RectangleToScreen(rect);
Control parent = _viewer.Parent;
while (parent != null)
{
rect = Rectangle.Intersect(rect, _viewer.Parent.RectangleToScreen(_viewer.Parent.ClientRectangle));
if (parent is Form)
{
Form form = parent as Form;
if (form.IsMdiChild)
{
if (form.Owner != null)
rect = Rectangle.Intersect(rect, form.Owner.RectangleToScreen(form.Owner.ClientRectangle));
else if (form.Parent != null)
rect = Rectangle.Intersect(rect, form.Parent.RectangleToScreen(form.Parent.ClientRectangle));
}
}
parent = parent.Parent;
}
rect.Height += 1;
rect.Width += 1;
saveClipRectangle = Cursor.Clip;
Cursor.Clip = rect;
isCursorClipped = true;
}
else
{
Cursor.Clip = saveClipRectangle;
isCursorClipped = false;
saveClipRectangle = Rectangle.Empty;
}
}
private static Rectangle FixRectangle(Rectangle rect)
{
if (rect.Left > rect.Right)
rect = Rectangle.FromLTRB(rect.Right, rect.Top, rect.Left, rect.Bottom);
if (rect.Top > rect.Bottom)
rect = Rectangle.FromLTRB(rect.Left, rect.Bottom, rect.Right, rect.Top);
return rect;
}
// Draws the rubberband rectangle on the viewer
private void DrawRubberBandRectangle()
{
Rectangle rect = FixRectangle(rubberBandingRectangle);
rect.Width++;
rect.Height++;
rect = _viewer.RectangleToScreen(rect);
ControlPaint.DrawReversibleFrame(rect, Color.Transparent, FrameStyle.Thick);
isRubberBandingRectangleDrawn = !isRubberBandingRectangleDrawn;
}
private void _viewer_MouseDown(object sender, MouseEventArgs e)
{
_viewer.Focus();
// Cancel rubber banding if it is on
if (isRubberBanding)
EndRubberBanding();
else
{
if (_viewer.IsImageAvailable && e.Button == MouseButtons.Left)
{
// See if we click is on the image
Rectangle rect = _viewer.PhysicalViewRectangle;
if (rect.Contains(e.X, e.Y))
{
// Start the rubber banding
BeginRubberBanding(e.X, e.Y);
}
}
}
}
private void _viewer_MouseMove(object sender, MouseEventArgs e)
{
if (isRubberBanding)
{
DrawRubberBandRectangle();
rubberBandingRectangle = Rectangle.FromLTRB(
rubberBandingRectangle.Left,
rubberBandingRectangle.Top,
e.X,
e.Y);
DrawRubberBandRectangle();
}
}
private void _viewer_MouseUp(object sender, MouseEventArgs e)
{
if (isRubberBanding)
{
// Save the rubberband rectangle
Rectangle rect = rubberBandingRectangle;
EndRubberBanding();
// First, convert the rectangle to image coordinates
rect = FixRectangle(rect);
// Must be at least 1 pixel in size
if (rect.Width > 1 && rect.Height > 1)
{
// Get the transform matrix
// This works even if the UseDpi property of the viewer is set to true
using (Matrix transform = _viewer.GetTransformWithDpi())
{
Transformer t = new Transformer(transform);
rect = Rectangle.Round(t.RectangleToLogical(rect));
LeadRect lrect = new LeadRect(rect.Left, rect.Top, rect.Width, rect.Height);
lrect = _viewer.Image.RectangleToImage(RasterViewPerspective.TopLeft, lrect);
// Add this rectangle as a region
// Note: no conversion or xform is needed
_viewer.Image.AddRectangleToRegion(null, lrect, RasterRegionCombineMode.Set);
}
}
}
}
private void _viewer_LostFocus(object sender, EventArgs e)
{
if (isRubberBanding)
{
EndRubberBanding();
}
}
}
static class LEAD_VARS
{
public const string ImagesDir = @"C:\LEADTOOLS22\Resources\Images";
}