Визуальный инструмент GraphicObjectTool.
В этом разделе
Визуальный инструмент
GraphicObjectTool позволяет отображать статические графические объекты в
ImageViewer.
Графический объект.
Класс
GraphicObject является базовым классом для статических объектов и позволяет:
Вот список стандартных статических объектов:
Вот C#/VB.NET код, который демонстрирует, как отобразить сетку над изображением:
/// <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>
/// Determines that 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)
{
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
}
''' <summary>
''' Displays grid over image in image viewer.
''' </summary>
''' <param name="viewer">The viewer.</param>
Public Shared Sub DrawGridOverImageInImageViewer(viewer As Vintasoft.Imaging.UI.ImageViewer)
' create visual tool that can display static graphic objects in an image viewer
Dim tool As New Vintasoft.Imaging.UI.VisualTools.GraphicObjects.GraphicObjectTool()
' create graphic object that displays image grid
Dim grid As 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
End Sub
''' <summary>
''' Represents an image grid which can be displayed in image viewer.
''' </summary>
Public Class GridGraphicObject
Inherits Vintasoft.Imaging.UI.VisualTools.GraphicObjects.GraphicObject
#Region "Fields"
Private _tool As Vintasoft.Imaging.UI.VisualTools.VisualTool
#End Region
#Region "Constructors"
''' <summary>
''' Initializes a new instance of the <see cref="GridGraphicObject"/> class.
''' </summary>
''' <param name="tool">The visual tool.</param>
Public Sub New(tool As Vintasoft.Imaging.UI.VisualTools.VisualTool)
If tool Is Nothing Then
Throw New System.ArgumentNullException()
End If
_tool = tool
Pen = New System.Drawing.Pen(System.Drawing.Color.FromArgb(128, System.Drawing.Color.Lime), 1)
PointTransform = New Vintasoft.Imaging.UI.VisualTools.PixelsToImageViewerPointFTransform()
End Sub
#End Region
#Region "Properties"
Private _location As System.Drawing.PointF = System.Drawing.PointF.Empty
''' <summary>
''' Gets or sets the location of grid.
''' </summary>
''' <value>
''' Default value is <b>(0,0)</b>.
''' </value>
Public Property Location() As System.Drawing.PointF
Get
Return _location
End Get
Set
_location = value
End Set
End Property
Private _cellSize As 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 Property CellSize() As System.Drawing.SizeF
Get
Return _cellSize
End Get
Set
_cellSize = value
End Set
End Property
Private _cellUnitOfMeasure As Vintasoft.Imaging.UnitOfMeasure = 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 Property CellUnitOfMeasure() As Vintasoft.Imaging.UnitOfMeasure
Get
Return _cellUnitOfMeasure
End Get
Set
_cellUnitOfMeasure = value
End Set
End Property
#End Region
#Region "Methods"
''' <summary>
''' Determines that 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 Overrides Function IsPointOnObject(x As Single, y As Single) As Boolean
If _tool.ImageViewer.Image IsNot Nothing AndAlso (Pen IsNot Nothing OrElse Brush IsNot Nothing) Then
Dim rect As System.Drawing.RectangleF = GetRectangle(_location, _tool.ImageViewer.Image)
Return rect.Contains(x, y)
Else
Return False
End If
End Function
''' <summary>
''' Returns a bounding box of object, in object space.
''' </summary>
''' <returns>
''' Bounding box of object, in object space.
''' </returns>
Public Overrides Function GetBoundingBox() As System.Drawing.RectangleF
If _tool.ImageViewer.Image IsNot Nothing Then
Return GetRectangle(_location, _tool.ImageViewer.Image)
Else
Return System.Drawing.RectangleF.Empty
End If
End Function
''' <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 Overrides Function GetDrawingBox(viewer As Vintasoft.Imaging.UI.ImageViewer) As System.Drawing.RectangleF
If viewer.Image Is Nothing Then
Return System.Drawing.RectangleF.Empty
End If
Dim rect As System.Drawing.RectangleF = GetRectangle(_location, viewer.Image)
If Pen IsNot Nothing Then
Dim penWidth As Single = Pen.Width / 2F
rect.Inflate(penWidth, penWidth)
End If
Return rect
End Function
''' <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 Overrides Sub DrawInObjectSpace(viewer As Vintasoft.Imaging.UI.ImageViewer, g As System.Drawing.Graphics)
If viewer.Image Is Nothing Then
Return
End If
Dim rect As System.Drawing.RectangleF = GetRectangle(_location, viewer.Image)
If Brush IsNot Nothing Then
g.FillRectangle(Brush, rect)
End If
If Pen IsNot Nothing AndAlso Not CellSize.IsEmpty Then
Dim cellHeight As Single = CSng(Vintasoft.Imaging.Utils.UnitOfMeasureConverter.ConvertToPixels(CellSize.Height, CellUnitOfMeasure))
Dim countRow As Integer = CInt(Math.Truncate(System.Math.Ceiling(rect.Height / cellHeight)))
Dim cellWidth As Single = CSng(Vintasoft.Imaging.Utils.UnitOfMeasureConverter.ConvertToPixels(CellSize.Width, CellUnitOfMeasure))
Dim countColumn As Integer = CInt(Math.Truncate(System.Math.Ceiling(rect.Width / cellWidth)))
If countRow > 0 AndAlso countColumn > 0 Then
Dim x1 As Single = rect.X
Dim y1 As Single = rect.Y + cellHeight
Dim x2 As Single = rect.Right
Dim i As Integer
i = 1
While i < countRow
g.DrawLine(Pen, x1, y1, x2, y1)
i += 1
y1 += cellHeight
End While
x1 += cellWidth
y1 = rect.Y
Dim y2 As Single = rect.Bottom
i = 1
While i < countColumn
g.DrawLine(Pen, x1, y1, x1, y2)
i += 1
x1 += cellWidth
End While
End If
End If
End Sub
''' <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 Overrides Function Clone() As Object
Dim obj As New GridGraphicObject(_tool)
CopyTo(obj)
Return obj
End Function
''' <summary>
''' Copies current WpfGridGraphicObject to the target WpfGridGraphicObject.
''' </summary>
''' <param name="obj">WpfGridGraphicObject to copy.</param>
Public Overrides Sub CopyTo(obj As Vintasoft.Imaging.UI.VisualTools.GraphicObjects.GraphicObject)
MyBase.CopyTo(obj)
Dim gridObj As GridGraphicObject = TryCast(obj, GridGraphicObject)
If gridObj IsNot Nothing Then
gridObj.Location = Location
gridObj.CellSize = CellSize
gridObj.CellUnitOfMeasure = CellUnitOfMeasure
End If
End Sub
''' <summary>
''' Gets the rectangle of grid.
''' </summary>
''' <param name="location">The location.</param>
''' <param name="image">The image.</param>
Private Function GetRectangle(location As System.Drawing.PointF, image As Vintasoft.Imaging.VintasoftImage) As System.Drawing.RectangleF
Dim rect As New System.Drawing.RectangleF(_location.X, _location.Y, image.Width - _location.X, image.Height - _location.Y)
Return rect
End Function
#End Region
End Class
Создание внешнего вида объекта.
VintaSoft Imaging .NET SDK позволяет создать собственный статический объект с нуля или изменить существующий стандартный статический объект.
Внешний вид объекта можно создать переопределив один из следующих методов:
Вот C#/VB.NET код, который демонстрирует, как отобразить повернутый текст путем переопределения метода
GraphicObject.DrawInObjectSpace:
/// <summary>
/// Represents a static rotated text which can be displayed in image viewer.
/// </summary>
public class RotatedTextGraphicObject :
Vintasoft.Imaging.UI.VisualTools.GraphicObjects.TextGraphicObject
{
#region Properties
float _textRotationAngle = 0.0f;
/// <summary>
/// Gets or sets the rotation angle, in degrees, of text.
/// </summary>
/// <value>
/// Default value is <b>0.0</b>.
/// </value>
public float TextRotationAngle
{
get
{
return _textRotationAngle;
}
set
{
_textRotationAngle = value % 360.0f;
}
}
#endregion
#region Methods
/// <summary>
/// Draws the object on specified <see cref="System.Drawing.Graphics" />.
/// </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
/// <see cref="Vintasoft.Imaging.UI.VisualTools.GraphicObjects.GraphicObject.PointTransform" /> is applied
/// to the Graphics, specified by <i>g</i> parameter.
/// </remarks>
public override void DrawInObjectSpace(Vintasoft.Imaging.UI.ImageViewer viewer,
System.Drawing.Graphics g)
{
// save matrix of Graphics
using (System.Drawing.Drawing2D.Matrix m = g.Transform)
{
// create matrix
using (System.Drawing.Drawing2D.Matrix tmp = new System.Drawing.Drawing2D.Matrix())
{
// calculate center point of text
System.Drawing.PointF center = new System.Drawing.PointF(
Rectangle.X + Rectangle.Width / 2.0f,
Rectangle.Y + Rectangle.Height / 2.0f);
// rotate matrix
tmp.RotateAt(_textRotationAngle, center);
// multiply matrix
g.MultiplyTransform(tmp);
}
// draw text
base.DrawInObjectSpace(viewer, g);
// restore matrix of Graphics
g.Transform = m;
}
}
#endregion
}
''' <summary>
''' Represents a static rotated text which can be displayed in image viewer.
''' </summary>
Public Class RotatedTextGraphicObject
Inherits Vintasoft.Imaging.UI.VisualTools.GraphicObjects.TextGraphicObject
#Region "Properties"
Private _textRotationAngle As Single = 0F
''' <summary>
''' Gets or sets the rotation angle, in degrees, of text.
''' </summary>
''' <value>
''' Default value is <b>0.0</b>.
''' </value>
Public Property TextRotationAngle() As Single
Get
Return _textRotationAngle
End Get
Set
_textRotationAngle = value Mod 360F
End Set
End Property
#End Region
#Region "Methods"
''' <summary>
''' Draws the object on specified <see cref="System.Drawing.Graphics" />.
''' </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
''' <see cref="Vintasoft.Imaging.UI.VisualTools.GraphicObjects.GraphicObject.PointTransform" /> is applied
''' to the Graphics, specified by <i>g</i> parameter.
''' </remarks>
Public Overrides Sub DrawInObjectSpace(viewer As Vintasoft.Imaging.UI.ImageViewer, g As System.Drawing.Graphics)
' save matrix of Graphics
Using m As System.Drawing.Drawing2D.Matrix = g.Transform
' create matrix
Using tmp As New System.Drawing.Drawing2D.Matrix()
' calculate center point of text
Dim center As New System.Drawing.PointF(Rectangle.X + Rectangle.Width / 2F, Rectangle.Y + Rectangle.Height / 2F)
' rotate matrix
tmp.RotateAt(_textRotationAngle, center)
' multiply matrix
g.MultiplyTransform(tmp)
End Using
' draw text
MyBase.DrawInObjectSpace(viewer, g)
' restore matrix of Graphics
g.Transform = m
End Using
End Sub
#End Region
End Class
Вот C#/VB.NET код, который демонстрирует, как отобразить имя загруженного файла, текущий уровень масштабирования и ограничивающий прямоугольник изображения в просмотрщике изображений. Объекты, отвечающие за отображение имени загруженного файла и уровня масштабирования, расположены в координатном пространстве просмотрщика, а объект, отвечающий за отображение ограничивающего прямоугольника, находится в координатном пространстве изображения.
/// <summary>
/// Visual tool that draws static text (filename, current zoom factor) over image in
/// image viewer and draws a frame around image in image viewer:
/// </summary>
public class GraphicObjectToolExample :
Vintasoft.Imaging.UI.VisualTools.GraphicObjects.GraphicObjectTool
{
#region Fields
/// <summary>
/// The graphic object that draws a filename over the image in image viewer.
/// </summary>
Vintasoft.Imaging.UI.VisualTools.GraphicObjects.TextGraphicObject _fileNameGraphicObject =
new Vintasoft.Imaging.UI.VisualTools.GraphicObjects.TextGraphicObject();
/// <summary>
/// The graphic object that draws image zoom factor over the image in image viewer.
/// </summary>
Vintasoft.Imaging.UI.VisualTools.GraphicObjects.TextGraphicObject _zoomGraphicObject =
new Vintasoft.Imaging.UI.VisualTools.GraphicObjects.TextGraphicObject();
/// <summary>
/// The graphic object that draws a frame around image in image viewer.
/// </summary>
Vintasoft.Imaging.UI.VisualTools.GraphicObjects.RectangularGraphicObject _borderGraphicObject =
new Vintasoft.Imaging.UI.VisualTools.GraphicObjects.RectangularGraphicObject();
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="GraphicObjectToolExample"/> class.
/// </summary>
public GraphicObjectToolExample()
{
_fileNameGraphicObject.TextAnchor = Vintasoft.Imaging.AnchorType.Top;
_fileNameGraphicObject.FontBrush = System.Drawing.Brushes.Salmon;
_fileNameGraphicObject.Font = new System.Drawing.Font("Arial", 24);
GraphicObjectCollection.Add(_fileNameGraphicObject);
_zoomGraphicObject.TextAnchor =
Vintasoft.Imaging.AnchorType.Bottom | Vintasoft.Imaging.AnchorType.Right;
_zoomGraphicObject.FontBrush = System.Drawing.Brushes.Lime;
_zoomGraphicObject.Font = new System.Drawing.Font("Arial", 20);
GraphicObjectCollection.Add(_zoomGraphicObject);
_borderGraphicObject.PointTransform =
new Vintasoft.Imaging.UI.VisualTools.PixelsToImageViewerPointFTransform();
_borderGraphicObject.Pen = new System.Drawing.Pen(
System.Drawing.Color.FromArgb(128, System.Drawing.Color.Lime), 10);
GraphicObjectCollection.Add(_borderGraphicObject);
}
#endregion
#region Methods
/// <summary>
/// Raises the <see cref="Vintasoft.Imaging.UI.VisualTools.VisualTool.Activated" /> event.
/// </summary>
protected override void OnActivated(System.EventArgs e)
{
base.OnActivated(e);
ImageViewer.ZoomChanged +=
new System.EventHandler<Vintasoft.Imaging.UI.ZoomChangedEventArgs>(ImageViewer_ZoomChanged);
ImageViewer.ClientSizeChanged += new System.EventHandler(ImageViewer_ClientSizeChanged);
ImageViewer.ImageLoaded +=
new System.EventHandler<Vintasoft.Imaging.ImageLoadedEventArgs>(ImageViewer_ImageLoaded);
UpdateGraphicObjects();
}
/// <summary>
/// Raises the <see cref="Vintasoft.Imaging.UI.VisualTools.VisualTool.Deactivated" /> event.
/// </summary>
protected override void OnDeactivated(System.EventArgs e)
{
base.OnDeactivated(e);
ImageViewer.ZoomChanged -= ImageViewer_ZoomChanged;
ImageViewer.ClientSizeChanged -= ImageViewer_ClientSizeChanged;
ImageViewer.ImageLoaded -= ImageViewer_ImageLoaded;
}
/// <summary>
/// Updates the graphic objects.
/// </summary>
void UpdateGraphicObjects()
{
_fileNameGraphicObject.Rectangle = ImageViewer.ClientRectangle;
_zoomGraphicObject.Rectangle = ImageViewer.ClientRectangle;
_zoomGraphicObject.Text = string.Format("Zoom: {0}", ImageViewer.Zoom);
Vintasoft.Imaging.VintasoftImage image = ImageViewer.Image;
if (image == null)
{
_fileNameGraphicObject.Text = string.Empty;
_borderGraphicObject.Rectangle = System.Drawing.Rectangle.Empty;
}
else
{
Vintasoft.Imaging.ImageSourceInfo info = image.SourceInfo;
if (string.IsNullOrEmpty(info.Filename))
_fileNameGraphicObject.Text = string.Empty;
else
{
string text = System.IO.Path.GetFileName(info.Filename);
if (info.PageCount > 1)
text += string.Format(" (Page {0}/{1})", info.PageIndex + 1, info.PageCount);
_fileNameGraphicObject.Text = text;
}
_borderGraphicObject.Rectangle = new System.Drawing.RectangleF(0, 0, image.Width, image.Height);
}
}
/// <summary>
/// Handles the ClientSizeChanged event of the ImageViewer control.
/// </summary>
void ImageViewer_ClientSizeChanged(object sender, System.EventArgs e)
{
UpdateGraphicObjects();
}
/// <summary>
/// Handles the ZoomChanged event of the ImageViewer control.
/// </summary>
void ImageViewer_ZoomChanged(object sender, Vintasoft.Imaging.UI.ZoomChangedEventArgs e)
{
UpdateGraphicObjects();
}
/// <summary>
/// Handles the ImageLoaded event of the ImageViewer control.
/// </summary>
void ImageViewer_ImageLoaded(object sender, Vintasoft.Imaging.ImageLoadedEventArgs e)
{
UpdateGraphicObjects();
}
#endregion
}
''' <summary>
''' Visual tool that draws static text (filename, current zoom factor) over image in
''' image viewer and draws a frame around image in image viewer:
''' </summary>
Public Class GraphicObjectToolExample
Inherits Vintasoft.Imaging.UI.VisualTools.GraphicObjects.GraphicObjectTool
#Region "Fields"
''' <summary>
''' The graphic object that draws a filename over the image in image viewer.
''' </summary>
Private _fileNameGraphicObject As New Vintasoft.Imaging.UI.VisualTools.GraphicObjects.TextGraphicObject()
''' <summary>
''' The graphic object that draws image zoom factor over the image in image viewer.
''' </summary>
Private _zoomGraphicObject As New Vintasoft.Imaging.UI.VisualTools.GraphicObjects.TextGraphicObject()
''' <summary>
''' The graphic object that draws a frame around image in image viewer.
''' </summary>
Private _borderGraphicObject As New Vintasoft.Imaging.UI.VisualTools.GraphicObjects.RectangularGraphicObject()
#End Region
#Region "Constructors"
''' <summary>
''' Initializes a new instance of the <see cref="GraphicObjectToolExample"/> class.
''' </summary>
Public Sub New()
_fileNameGraphicObject.TextAnchor = Vintasoft.Imaging.AnchorType.Top
_fileNameGraphicObject.FontBrush = System.Drawing.Brushes.Salmon
_fileNameGraphicObject.Font = New System.Drawing.Font("Arial", 24)
GraphicObjectCollection.Add(_fileNameGraphicObject)
_zoomGraphicObject.TextAnchor = Vintasoft.Imaging.AnchorType.Bottom Or Vintasoft.Imaging.AnchorType.Right
_zoomGraphicObject.FontBrush = System.Drawing.Brushes.Lime
_zoomGraphicObject.Font = New System.Drawing.Font("Arial", 20)
GraphicObjectCollection.Add(_zoomGraphicObject)
_borderGraphicObject.PointTransform = New Vintasoft.Imaging.UI.VisualTools.PixelsToImageViewerPointFTransform()
_borderGraphicObject.Pen = New System.Drawing.Pen(System.Drawing.Color.FromArgb(128, System.Drawing.Color.Lime), 10)
GraphicObjectCollection.Add(_borderGraphicObject)
End Sub
#End Region
#Region "Methods"
''' <summary>
''' Raises the <see cref="Vintasoft.Imaging.UI.VisualTools.VisualTool.Activated" /> event.
''' </summary>
Protected Overrides Sub OnActivated(e As System.EventArgs)
MyBase.OnActivated(e)
AddHandler ImageViewer.ZoomChanged, New System.EventHandler(Of Vintasoft.Imaging.UI.ZoomChangedEventArgs)(AddressOf ImageViewer_ZoomChanged)
AddHandler ImageViewer.ClientSizeChanged, New System.EventHandler(AddressOf ImageViewer_ClientSizeChanged)
AddHandler ImageViewer.ImageLoaded, New System.EventHandler(Of Vintasoft.Imaging.ImageLoadedEventArgs)(AddressOf ImageViewer_ImageLoaded)
UpdateGraphicObjects()
End Sub
''' <summary>
''' Raises the <see cref="Vintasoft.Imaging.UI.VisualTools.VisualTool.Deactivated" /> event.
''' </summary>
Protected Overrides Sub OnDeactivated(e As System.EventArgs)
MyBase.OnDeactivated(e)
RemoveHandler ImageViewer.ZoomChanged, AddressOf ImageViewer_ZoomChanged
RemoveHandler ImageViewer.ClientSizeChanged, AddressOf ImageViewer_ClientSizeChanged
RemoveHandler ImageViewer.ImageLoaded, AddressOf ImageViewer_ImageLoaded
End Sub
''' <summary>
''' Updates the graphic objects.
''' </summary>
Private Sub UpdateGraphicObjects()
_fileNameGraphicObject.Rectangle = ImageViewer.ClientRectangle
_zoomGraphicObject.Rectangle = ImageViewer.ClientRectangle
_zoomGraphicObject.Text = String.Format("Zoom: {0}", ImageViewer.Zoom)
Dim image As Vintasoft.Imaging.VintasoftImage = ImageViewer.Image
If image Is Nothing Then
_fileNameGraphicObject.Text = String.Empty
_borderGraphicObject.Rectangle = System.Drawing.Rectangle.Empty
Else
Dim info As Vintasoft.Imaging.ImageSourceInfo = image.SourceInfo
If String.IsNullOrEmpty(info.Filename) Then
_fileNameGraphicObject.Text = String.Empty
Else
Dim text As String = System.IO.Path.GetFileName(info.Filename)
If info.PageCount > 1 Then
text += String.Format(" (Page {0}/{1})", info.PageIndex + 1, info.PageCount)
End If
_fileNameGraphicObject.Text = text
End If
_borderGraphicObject.Rectangle = New System.Drawing.RectangleF(0, 0, image.Width, image.Height)
End If
End Sub
''' <summary>
''' Handles the ClientSizeChanged event of the ImageViewer control.
''' </summary>
Private Sub ImageViewer_ClientSizeChanged(sender As Object, e As System.EventArgs)
UpdateGraphicObjects()
End Sub
''' <summary>
''' Handles the ZoomChanged event of the ImageViewer control.
''' </summary>
Private Sub ImageViewer_ZoomChanged(sender As Object, e As Vintasoft.Imaging.UI.ZoomChangedEventArgs)
UpdateGraphicObjects()
End Sub
''' <summary>
''' Handles the ImageLoaded event of the ImageViewer control.
''' </summary>
Private Sub ImageViewer_ImageLoaded(sender As Object, e As Vintasoft.Imaging.ImageLoadedEventArgs)
UpdateGraphicObjects()
End Sub
#End Region
End Class