This forum contains outdated content and is available for reading only. Please contact technical support if you have any questions.

Measurements

Is it possible to do measurements in Mill.Net? Like retrieving the length of a line? (of course after 'callibrating' the image)
Hello,

You can implement your rubberband for BitmapViewer control and do measurements using points, marked by rubberband, zoom ratio and resolutions of bitmap.

If you have questions regarding this matter, please, let me know.
hi,

i'm new to Mill.net... i'm evaluating a couple of components...
it would be nice to have some sample of this "rubberband" handling for measurements?
Hi, ChrisT,

Here is small sample of rubberband for the BitmapViewer control which implements measurement. After connecting the rubberband object to BitmapViewer instance you may specify points by mouse click and obtain the distance between them using InchDistance, MmDistance and PixelDistance properties of the rubberband. Here rubberband class code:

public class DistanceMeterRubberband: Aurigma.GraphicsMill.WinControls.UserInputController, Aurigma.GraphicsMill.WinControls.IRubberband
{
	//
	// Construction / destruction
	//
	public DistanceMeterRubberband()
	{	
		points = new System.Drawing.Point[2];			
		points[0] = System.Drawing.Point.Empty;
		points[1] = System.Drawing.Point.Empty;

		waitingIndex = 0;

		pen = new System.Drawing.Pen(System.Drawing.Color.Red, 1.0f);
		brush = new System.Drawing.SolidBrush(System.Drawing.Color.Green);
	}


	#region "Distance calculation"
	public double PixelDistance
	{
		get
		{
			double dx = Math.Abs(points[0].X - points[1].X) + 1;
			double dy = Math.Abs(points[0].Y - points[1].Y) + 1;
			
			return Math.Sqrt(dx * dx + dy * dy);
		}
	}


	public double InchDistance
	{
		get
		{
			return GetUnitDistance(Aurigma.GraphicsMill.Unit.Inch);
		}
	}


	public double MmDistance
	{
		get
		{
			return GetUnitDistance(Aurigma.GraphicsMill.Unit.Mm);
		}
	}


	private double GetUnitDistance(Aurigma.GraphicsMill.Unit destinationUnit)
	{
		int pixelsDx = Math.Abs(points[0].X - points[1].X) + 1;
		int pixelsDy = Math.Abs(points[0].Y - points[1].Y) + 1;

		double unitDx = Aurigma.GraphicsMill.UnitConverter.ConvertPixelsToUnits(this.BitmapViewer.Bitmap.HorizontalResolution, pixelsDx, destinationUnit);
		double unitDy = Aurigma.GraphicsMill.UnitConverter.ConvertPixelsToUnits(this.BitmapViewer.Bitmap.VerticalResolution, pixelsDy, destinationUnit);

		return Math.Sqrt(unitDx * unitDx + unitDy * unitDy);
	}
	#endregion //"Distance calculation"


	#region "Overloaded methods from UserInputController"
	protected override void OnBitmapViewerMouseDown(System.Windows.Forms.MouseEventArgs e)
	{
		if (!this.IsBitmapLoaded)
			return;

		System.Drawing.Point bitmapPoint = this.BitmapViewer.ControlToBitmapPoint(new System.Drawing.Point(e.X, e.Y));
		if (bitmapPoint.X < 0 || bitmapPoint.X >= this.BitmapViewer.Bitmap.Width ||
			bitmapPoint.Y < 0 || bitmapPoint.Y >= this.BitmapViewer.Bitmap.Height)
			return;

		points[waitingIndex] = bitmapPoint;
		waitingIndex = (waitingIndex + 1) % 2;

		this.BitmapViewer.Invalidate();
	}


	protected override void OnBitmapViewerDoubleBufferPaint(System.Windows.Forms.PaintEventArgs e)
	{
		if(!IsBitmapLoaded)
			return;

		DrawAnchorPoint(e.Graphics, points[0]);
		DrawAnchorPoint(e.Graphics, points[1]);
	}
	#endregion


	#region "Drawing stuff"
	private void DrawAnchorPoint(System.Drawing.Graphics g, System.Drawing.Point pnt)
	{
		System.Drawing.Point controlPnt = this.BitmapViewer.BitmapToControlPoint(pnt);

		int radius = 4;
		g.FillEllipse(brush, controlPnt.X - radius, controlPnt.Y - radius, radius * 2, radius * 2);
		g.DrawEllipse(pen, controlPnt.X - radius, controlPnt.Y - radius, radius * 2, radius * 2);			
	}
	#endregion


	#region "Private variables"
	private System.Drawing.Point[] points;
	private int waitingIndex;
	
	private System.Drawing.Pen pen;
	private System.Drawing.Brush brush;
	#endregion //"Private variables"
}

Here is the code sample which shows how to use this rubberband. Add this line of code into initialization method of your application:
bitmapViewer.Rubberband = new DistanceMeterRubberband();
and add handler for BitmapMouseUp event of the BitmapViewer instance with the following code:

private void bitmapViewer_BitmapMouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
	if (bitmapViewer.Bitmap.IsEmpty)
		return;

	DistanceMeterRubberband dr = (DistanceMeterRubberband)bitmapViewer.Rubberband;

	pixelDistanceLabel.Text = String.Format("Pixels: {0:F}", dr.PixelDistance);
	inchDistanceLabel.Text = String.Format("Inches: {0:F}", dr.InchDistance);
	mmDistanceLabel.Text = String.Format("Mm: {0:F}", dr.MmDistance);
}