Welcome Guest! You need to login or register to make posts.

Notification

Icon
Error

Options
Go to last post Go to first unread
Andrew  
#1 Posted : Saturday, April 23, 2005 3:12:00 PM(UTC)
Andrew

Rank: Advanced Member

Groups: Member, Administration
Joined: 8/2/2003(UTC)
Posts: 863

Thanks: 2 times
Was thanked: 25 time(s) in 25 post(s)
Hi everyone,

I am posting an example of the custom rubberband class (WinControl). This class enables you to make polygon selection on the BitmapViewer.

To use it, just create an instance of this class and put it to the Rubberband property of the BitmapViewer control. When you run the application, click the BitmapViewer in those points where you want to have the polygon. The polygon appears when you specify at least four points.

Here is a code of the PolygonRubberband class:

Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Security.Permissions;
using Aurigma.GraphicsMill;
using Aurigma.GraphicsMill.Drawing;
using Aurigma.GraphicsMill.Transforms;

namespace Aurigma.GraphicsMill.WinControls.Samples
{
	public sealed class PolygonRubberband : Aurigma.GraphicsMill.WinControls.UserInputController, IRubberband
	{
		private enum ActionType
		{
			AddNew,
			MoveExisting
		};
		private System.Collections.ArrayList _points;
		private ActionType _actionType;
		private int _movingPoint;

		public PolygonRubberband()
		{
			_actionType = ActionType.AddNew;
			_points = new System.Collections.ArrayList();
			_movingPoint = -1;
		}

		protected override void OnBitmapViewerMouseMove(System.Windows.Forms.MouseEventArgs e)
		{
			if(_actionType == ActionType.MoveExisting)
				MoveExistingPoint(_movingPoint, new System.Drawing.Point(e.X, e.Y));
		}

		protected override void OnBitmapViewerMouseDown(System.Windows.Forms.MouseEventArgs e)
		{
			System.Drawing.Point point = new System.Drawing.Point(e.X, e.Y);
			if(!BitmapViewer.CanvasRenderingRectangle.Contains(point))
				return;

			int pointIndex = FindPointIndex(point);

			if(pointIndex < 0)
			{
				_actionType = ActionType.AddNew;
				AddNewPoint(point);
			}
			else
			{
				_actionType = ActionType.MoveExisting;
				_movingPoint = pointIndex;
			}
	    }

		protected override void OnBitmapViewerMouseUp(System.Windows.Forms.MouseEventArgs e)
		{
			_movingPoint = -1;
			_actionType = ActionType.AddNew;
		}

		protected override void OnBitmapViewerDoubleBufferPaint(System.Windows.Forms.PaintEventArgs e)
		{
			DrawPolygon(e.Graphics);
		}

		private void DrawPolygon(System.Drawing.Graphics graphics)
		{
			if(_points.Count < 4)
				return;

			System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Red, 2);
			System.Drawing.Point point1;
			System.Drawing.Point point2;
			for(int i = 0; i < _points.Count - 1; i++)
			{
				point1 = BitmapViewer.BitmapToControlPoint((System.Drawing.Point)_points[i]);
				point2 = BitmapViewer.BitmapToControlPoint((System.Drawing.Point)_points[i + 1]);
				graphics.DrawLine(pen, point1, point2);
			}

			point1 = BitmapViewer.BitmapToControlPoint((System.Drawing.Point)_points[0]);
			point2 = BitmapViewer.BitmapToControlPoint((System.Drawing.Point)_points[_points.Count - 1]);
			graphics.DrawLine(pen, point1, point2);

			DrawGrips(graphics);
		}

		private void DrawGrips(System.Drawing.Graphics graphics)
		{
			System.Drawing.Brush brush = new System.Drawing.SolidBrush(System.Drawing.Color.Green);
			for(int i = 0; i < _points.Count; i++)
			{
				System.Drawing.Point point = BitmapViewer.BitmapToControlPoint((System.Drawing.Point)_points[i]);
				graphics.FillRectangle(brush, point.X - 5, point.Y - 5, 10, 10);
			}
		}

		private int FindPointIndex(System.Drawing.Point point)
		{
			int returnValue = -1;
			point = BitmapViewer.ControlToBitmapPoint(point);
			for(int i = 0; i < _points.Count; i++)
			{
				System.Drawing.Point tmpPoint = (System.Drawing.Point)_points[i];

				int distX = tmpPoint.X - point.X;
				distX *= distX;

				int distY = tmpPoint.Y - point.Y;
				distY *= distY;
				if(Math.Sqrt(distX + distY) < 10.0)
				{
					returnValue = i;
					break;
				}
			}
			return returnValue;
		}

		private void AddNewPoint(System.Drawing.Point point)
		{
			_points.Add(BitmapViewer.ControlToBitmapPoint(point));
			BitmapViewer.Invalidate();
		}

		private void MoveExistingPoint(int pointIndex, System.Drawing.Point newPosition)
		{
			System.Drawing.Rectangle renderingRectangle = BitmapViewer.CanvasRenderingRectangle;
			int difX = Math.Min(newPosition.X - renderingRectangle.Left, 0);
			if(difX == 0)
				difX = Math.Max(newPosition.X - renderingRectangle.Right, 0);

			int difY = Math.Min(newPosition.Y - renderingRectangle.Top, 0);
			if(difY == 0)
				difY = Math.Max(newPosition.Y - renderingRectangle.Bottom, 0);

			BitmapViewer.Scroll(difX, difY);

			newPosition.X = Math.Max(newPosition.X, renderingRectangle.Left);
			newPosition.X = Math.Min(newPosition.X, renderingRectangle.Right);
			newPosition.Y = Math.Max(newPosition.Y, renderingRectangle.Top);
			newPosition.Y = Math.Min(newPosition.Y, renderingRectangle.Bottom);

			_points[pointIndex] = BitmapViewer.ControlToBitmapPoint(newPosition);
			BitmapViewer.Invalidate();
		}
	};

} // namespace Aurigma.GraphicsMill.WinControls.Samples

Edited by user Monday, December 24, 2007 3:39:35 PM(UTC)  | Reason: Not specified

Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.