/// <summary>
/// Displays grid over image in image viewer.
/// </summary>
/// <param name="viewer">The viewer.</param>
public static void SetGridGraphicObject(Vintasoft.Imaging.Wpf.UI.WpfImageViewer viewer)
{
// create visual tool that can display static graphic objects in an image viewer
Vintasoft.Imaging.Wpf.UI.VisualTools.GraphicObjects.WpfGraphicObjectTool tool =
new Vintasoft.Imaging.Wpf.UI.VisualTools.GraphicObjects.WpfGraphicObjectTool();
// create graphic object that displays image grid
WpfGridGraphicObject grid = new WpfGridGraphicObject(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 WPF image viewer.
/// </summary>
public class WpfGridGraphicObject : Vintasoft.Imaging.Wpf.UI.VisualTools.GraphicObjects.WpfGraphicObject
{
#region Fields
Vintasoft.Imaging.Wpf.UI.VisualTools.WpfVisualTool _tool;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="WpfGridGraphicObject"/> class.
/// </summary>
/// <param name="tool">The visual tool.</param>
public WpfGridGraphicObject(Vintasoft.Imaging.Wpf.UI.VisualTools.WpfVisualTool tool)
{
if (tool == null)
throw new System.ArgumentNullException();
_tool = tool;
Pen = new System.Windows.Media.Pen(
new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(128, 0, 255, 0)), 1);
PointTransform = new Vintasoft.Imaging.Wpf.UI.VisualTools.WpfPixelsToImageViewerPointTransform();
}
#endregion
#region Properties
System.Windows.Point _location = new System.Windows.Point(0, 0);
/// <summary>
/// Gets or sets the location of grid.
/// </summary>
/// <value>
/// Default value is <b>(0,0)</b>.
/// </value>
public System.Windows.Point Location
{
get
{
return _location;
}
set
{
_location = value;
}
}
System.Windows.Size _cellSize = new System.Windows.Size(1, 1);
/// <summary>
/// Gets or sets the size of the grid cell.
/// </summary>
/// <value>
/// Default value is <b>(1,1)</b>.
/// </value>
public System.Windows.Size 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>
/// Determines that point belongs the object.
/// </summary>
/// <param name="p">Point in object space.</param>
/// <returns>
/// <b>true</b> if point belongs the object;
/// otherwise, <b>false</b>.
/// </returns>
public override bool IsPointOnObject(System.Windows.Point p)
{
if (_tool.ImageViewer.Image != null &&
(Pen != null || Brush != null))
{
System.Windows.Rect rect = GetRectangle(_location, _tool.ImageViewer.Image);
return rect.Contains(p);
}
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.Windows.Rect GetBoundingBox()
{
if (_tool.ImageViewer.Image != null)
return GetRectangle(_location, _tool.ImageViewer.Image);
else
return System.Windows.Rect.Empty;
}
/// <summary>
/// Renders the object on specified <see cref="T:System.Windows.Media.DrawingContext" />
/// in the object space.
/// </summary>
/// <param name="viewer">An image viewer.</param>
/// <param name="context">A drawing context where the object must be rendered.</param>
/// <remarks>
/// This method draws object after
/// the <see cref="P:Vintasoft.Imaging.Wpf.UI.VisualTools.GraphicObjects.WpfGraphicObject.PointTransform" />
/// is applied to the DrawingContext, specified by <i>context</i> parameter.<br /><br />
/// By default this method does not do anything.
/// </remarks>
public override void RenderInObjectSpace(
Vintasoft.Imaging.Wpf.UI.WpfImageViewer viewer, System.Windows.Media.DrawingContext context)
{
if (viewer.Image == null)
return;
System.Windows.Rect rect = GetRectangle(_location, viewer.Image);
context.DrawRectangle(Brush, null, rect);
if (Pen != null && !CellSize.IsEmpty)
{
double cellHeight = Vintasoft.Imaging.Utils.UnitOfMeasureConverter.ConvertToPixels(
CellSize.Height, CellUnitOfMeasure);
int countRow = (int)System.Math.Ceiling(rect.Height / cellHeight);
double cellWidth = Vintasoft.Imaging.Utils.UnitOfMeasureConverter.ConvertToPixels(
CellSize.Width, CellUnitOfMeasure);
int countColumn = (int)System.Math.Ceiling(rect.Width / cellWidth);
if (countRow > 0 && countColumn > 0)
{
double x1 = rect.X;
double y1 = rect.Y + cellHeight;
double x2 = rect.Right;
int i;
for (i = 1; i < countRow; i++, y1 += cellHeight)
context.DrawLine(Pen, new System.Windows.Point(x1, y1), new System.Windows.Point(x2, y1));
x1 += cellWidth;
y1 = rect.Y;
double y2 = rect.Bottom;
for (i = 1; i < countColumn; i++, x1 += cellWidth)
context.DrawLine(Pen, new System.Windows.Point(x1, y1), new System.Windows.Point(x1, y2));
}
}
}
/// <summary>
/// Creates a new <see cref="WpfGridGraphicObject"/> that is a copy of the current instance.
/// </summary>
/// <returns>
/// A new <see cref="WpfGridGraphicObject"/> that is a copy of this instance.
/// </returns>
public override object Clone()
{
WpfGridGraphicObject obj = new WpfGridGraphicObject(_tool);
CopyTo(obj);
return obj;
}
/// <summary>
/// Copies current <see cref="WpfGridGraphicObject"/> to
/// the target <see cref="WpfGridGraphicObject"/>
/// </summary>
/// <param name="obj"><see cref="WpfGridGraphicObject"/> to copy.</param>
public override void CopyTo(Vintasoft.Imaging.Wpf.UI.VisualTools.GraphicObjects.WpfGraphicObject obj)
{
base.CopyTo(obj);
WpfGridGraphicObject gridObj = obj as WpfGridGraphicObject;
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.Windows.Rect GetRectangle(System.Windows.Point location, Vintasoft.Imaging.VintasoftImage image)
{
System.Windows.Rect rect = new System.Windows.Rect(
_location.X, _location.Y,
image.Width - _location.X, image.Height - _location.Y);
return rect;
}
#endregion
}