/// <summary>
/// Displays grid over image in image viewer.
/// </summary>
/// <param name="viewer">The viewer.</param>
public static void DrawGridOverImageInImageViewer(Vintasoft.Imaging.UI.ImageViewer viewer)
{
// create visual tool that can display static graphic objects in an image viewer
Vintasoft.Imaging.UI.VisualTools.GraphicObjects.GraphicObjectTool tool =
new Vintasoft.Imaging.UI.VisualTools.GraphicObjects.GraphicObjectTool();
// create graphic object that displays image grid
GridGraphicObject grid = new GridGraphicObject(tool);
// add graphic object to the visual tool
tool.GraphicObjectCollection.Add(grid);
// set visual tool as current visual tool of image viewer
viewer.VisualTool = tool;
}
/// <summary>
/// Represents an image grid which can be displayed in image viewer.
/// </summary>
public class GridGraphicObject :
Vintasoft.Imaging.UI.VisualTools.GraphicObjects.GraphicObject
{
#region Fields
Vintasoft.Imaging.UI.VisualTools.VisualTool _tool;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="GridGraphicObject"/> class.
/// </summary>
/// <param name="tool">The visual tool.</param>
public GridGraphicObject(Vintasoft.Imaging.UI.VisualTools.VisualTool tool)
{
if (tool == null)
throw new System.ArgumentNullException();
_tool = tool;
Pen = new System.Drawing.Pen(
System.Drawing.Color.FromArgb(128, System.Drawing.Color.Lime), 1);
PointTransform =
new Vintasoft.Imaging.UI.VisualTools.PixelsToImageViewerPointFTransform();
}
#endregion
#region Properties
System.Drawing.PointF _location = System.Drawing.PointF.Empty;
/// <summary>
/// Gets or sets the location of grid.
/// </summary>
/// <value>
/// Default value is <b>(0,0)</b>.
/// </value>
public System.Drawing.PointF Location
{
get
{
return _location;
}
set
{
_location = value;
}
}
System.Drawing.SizeF _cellSize = new System.Drawing.SizeF(1, 1);
/// <summary>
/// Gets or sets the size of the grid cell.
/// </summary>
/// <value>
/// Default value is <b>(1,1)</b>.
/// </value>
public System.Drawing.SizeF CellSize
{
get
{
return _cellSize;
}
set
{
_cellSize = value;
}
}
Vintasoft.Imaging.UnitOfMeasure _cellUnitOfMeasure =
Vintasoft.Imaging.UnitOfMeasure.Centimeters;
/// <summary>
/// Gets or sets the unit of measure of the grid cell.
/// </summary>
/// <value>
/// Default value is <b>UnitOfMeasure.Centimeters</b>.
/// </value>
public Vintasoft.Imaging.UnitOfMeasure CellUnitOfMeasure
{
get
{
return _cellUnitOfMeasure;
}
set
{
_cellUnitOfMeasure = value;
}
}
#endregion
#region Methods
/// <summary>
/// Returns a value indicating whether point belongs the object.
/// </summary>
/// <param name="x">X coordinate of point in object space.</param>
/// <param name="y">Y coordinate of point in object space.</param>
/// <returns>
/// <b>true</b> if point belongs the object;
/// otherwise, <b>false</b>.
/// </returns>
public override bool IsPointOnObject(float x, float y)
{
return IsPointOnObject(x, y, false);
}
/// <summary>
/// Returns a value indicating whether point belongs the object.
/// </summary>
/// <param name="x">X coordinate of point in object space.</param>
/// <param name="y">Y coordinate of point in object space.</param>
/// <param name="ignoreContainmentCheckDistance">A value indicating whether the point must be checked on the object only (ignore
/// the "containment" region around object that is used for object selection).</param>
/// <returns>
/// <b>true</b> if point belongs the object;
/// otherwise, <b>false</b>.
/// </returns>
public override bool IsPointOnObject(float x, float y, bool ignoreContainmentCheckDistance)
{
if (_tool.ImageViewer.Image != null &&
(Pen != null || Brush != null))
{
System.Drawing.RectangleF rect = GetRectangle(_location, _tool.ImageViewer.Image);
return rect.Contains(x, y);
}
else
return false;
}
/// <summary>
/// Returns a bounding box of object, in object space.
/// </summary>
/// <returns>
/// Bounding box of object, in object space.
/// </returns>
public override System.Drawing.RectangleF GetBoundingBox()
{
if (_tool.ImageViewer.Image != null)
return GetRectangle(_location, _tool.ImageViewer.Image);
else
return System.Drawing.RectangleF.Empty;
}
/// <summary>
/// Returns a drawing rectangle of object, in object space.
/// </summary>
/// <param name="viewer">An image viewer.</param>
/// <returns>
/// Drawing rectangle of object, in object space.
/// </returns>
public override System.Drawing.RectangleF GetDrawingBox(Vintasoft.Imaging.UI.ImageViewer viewer)
{
if (viewer.Image == null)
return System.Drawing.RectangleF.Empty;
System.Drawing.RectangleF rect = GetRectangle(_location, viewer.Image);
if (Pen != null)
{
float penWidth = Pen.Width / 2.0f;
rect.Inflate(penWidth, penWidth);
}
return rect;
}
/// <summary>
/// Draws the object on specified <see cref="System.Drawing.Graphics" /> in the object space.
/// </summary>
/// <param name="viewer">An image viewer.</param>
/// <param name="g">A graphics where the object must be drawn.</param>
/// <remarks>
/// This method draws object after the PointTransform is applied to
/// the Graphics, specified by <i>g</i> parameter.<br /><br />
/// By default this method does not do anything.
/// </remarks>
public override void DrawInObjectSpace(Vintasoft.Imaging.UI.ImageViewer viewer, System.Drawing.Graphics g)
{
if (viewer.Image == null)
return;
System.Drawing.RectangleF rect = GetRectangle(_location, viewer.Image);
if (Brush != null)
g.FillRectangle(Brush, rect);
if (Pen != null && !CellSize.IsEmpty)
{
float cellHeight =
(float)Vintasoft.Imaging.Utils.UnitOfMeasureConverter.ConvertToPixels(CellSize.Height, CellUnitOfMeasure);
int countRow = (int)System.Math.Ceiling(rect.Height / cellHeight);
float cellWidth =
(float)Vintasoft.Imaging.Utils.UnitOfMeasureConverter.ConvertToPixels(CellSize.Width, CellUnitOfMeasure);
int countColumn = (int)System.Math.Ceiling(rect.Width / cellWidth);
if (countRow > 0 && countColumn > 0)
{
float x1 = rect.X;
float y1 = rect.Y + cellHeight;
float x2 = rect.Right;
int i;
for (i = 1; i < countRow; i++, y1 += cellHeight)
g.DrawLine(Pen, x1, y1, x2, y1);
x1 += cellWidth;
y1 = rect.Y;
float y2 = rect.Bottom;
for (i = 1; i < countColumn; i++, x1 += cellWidth)
g.DrawLine(Pen, x1, y1, x1, y2);
}
}
}
/// <summary>
/// Creates a new WpfGridGraphicObject that is a copy of the current instance.
/// </summary>
/// <returns>
/// A new WpfGridGraphicObject that is a copy of this instance.
/// </returns>
public override object Clone()
{
GridGraphicObject obj = new GridGraphicObject(_tool);
CopyTo(obj);
return obj;
}
/// <summary>
/// Copies current WpfGridGraphicObject to the target WpfGridGraphicObject.
/// </summary>
/// <param name="obj">WpfGridGraphicObject to copy.</param>
public override void CopyTo(Vintasoft.Imaging.UI.VisualTools.GraphicObjects.GraphicObject obj)
{
base.CopyTo(obj);
GridGraphicObject gridObj = obj as GridGraphicObject;
if (gridObj != null)
{
gridObj.Location = Location;
gridObj.CellSize = CellSize;
gridObj.CellUnitOfMeasure = CellUnitOfMeasure;
}
}
/// <summary>
/// Gets the rectangle of grid.
/// </summary>
/// <param name="location">The location.</param>
/// <param name="image">The image.</param>
private System.Drawing.RectangleF GetRectangle(
System.Drawing.PointF location,
Vintasoft.Imaging.VintasoftImage image)
{
System.Drawing.RectangleF rect = new System.Drawing.RectangleF(
_location.X, _location.Y,
image.Width - _location.X, image.Height - _location.Y);
return rect;
}
#endregion
}