Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace mladja
{
public partial class Form1 : Form
{
private List<Point> polygonP = new List<Point>();//lista tacaka
private Color _color = Color.Blue; // boja linija
private float _width = 1; // sirina linije
private bool isDoubleClicked = false;
public Form1()
{
InitializeComponent();
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (polygonP.Count > 0)
{
this.Text = "Prva: " + polygonP[0].ToString() + " - trenutna: " +
new Point(e.X, e.Y).ToString();
}
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
if (polygonP.Count > 1)
{
//draw the lines using a GraphicsPath
System.Drawing.Drawing2D.GraphicsPath gPath = new System.Drawing.Drawing2D.GraphicsPath();
gPath.AddLines(polygonP.ToArray());
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
using (Pen p = new Pen(_color, _width))
e.Graphics.DrawPath(p, gPath);
if (isDoubleClicked)
{
e.Graphics.FillPolygon(Brushes.Black, polygonP.ToArray()); //boji poligon
//uklanjaju se sve mogucnosti za dalje crtanje(onemogućuje sve ivente)
panel1.MouseClick -= panel1_MouseClick;
panel1.MouseMove -= panel1_MouseMove;
panel1.Paint -= panel1_Paint;
panel1.MouseDoubleClick -= panel1_MouseDoubleClick;
}
//cleanup
gPath.Dispose();
}
}
private void panel1_MouseDoubleClick(object sender, MouseEventArgs e)
{
polygonP.Add(new Point(polygonP[polygonP.Count - 1].X,
polygonP[polygonP.Count - 1].Y));
polygonP.Add(new Point(polygonP[0].X,
polygonP[0].Y));
this.panel1.Invalidate();
isDoubleClicked = true;
}
private void panel1_MouseClick(object sender, MouseEventArgs e)
{
polygonP.Add(new Point(e.X, e.Y));
this.panel1.Invalidate();
textBox1.Text += new Point(e.X, e.Y).ToString();
}
private void CalculateArea()
{
int totalArea = 0;
int x = this.Location.X + 20;
int y = this.Location.Y + 20;
int sbar = 0;
ProgressBar pbar = new ProgressBar();
pbar.Location = new Point(35, 411);
pbar.Name = "progressbar";
pbar.Width = 713;
pbar.Height = 20;
Controls.Add(pbar);
pbar.Minimum = 0;
pbar.Maximum = panel1.Size.Width - panel1.Location.X - x;
for (int i = panel1.Location.X + x; i < panel1.Size.Width; i++)
{
for (int j = panel1.Location.Y + y; j < panel1.Size.Height; j++)
{
if (GetColor(new Point(i, j)).Name == "ff000000")
{
totalArea++;
}
}
sbar += 1;
pbar.Value = sbar;
}
MessageBox.Show("Povrsina je " + totalArea.ToString() + " tacaka");
}
private Color GetColor(Point position)
{
using (var bitmap = new Bitmap(1, 1))
{
using (var graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(position, new Point(0, 0), new Size(1, 1));
}
return bitmap.GetPixel(0, 0);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (isDoubleClicked == false)
{
MessageBox.Show("Kontura nije zatvorena");
return;
}
CalculateArea();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace mladja
{
public partial class Form1 : Form
{
private List<Point> polygonP = new List<Point>();//lista tacaka
private Color _color = Color.Blue; // boja linija
private float _width = 1; // sirina linije
private bool isDoubleClicked = false;
public Form1()
{
InitializeComponent();
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (polygonP.Count > 0)
{
this.Text = "Prva: " + polygonP[0].ToString() + " - trenutna: " +
new Point(e.X, e.Y).ToString();
}
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
if (polygonP.Count > 1)
{
//draw the lines using a GraphicsPath
System.Drawing.Drawing2D.GraphicsPath gPath = new System.Drawing.Drawing2D.GraphicsPath();
gPath.AddLines(polygonP.ToArray());
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
using (Pen p = new Pen(_color, _width))
e.Graphics.DrawPath(p, gPath);
if (isDoubleClicked)
{
e.Graphics.FillPolygon(Brushes.Black, polygonP.ToArray()); //boji poligon
//uklanjaju se sve mogucnosti za dalje crtanje(onemogućuje sve ivente)
panel1.MouseClick -= panel1_MouseClick;
panel1.MouseMove -= panel1_MouseMove;
panel1.Paint -= panel1_Paint;
panel1.MouseDoubleClick -= panel1_MouseDoubleClick;
}
//cleanup
gPath.Dispose();
}
}
private void panel1_MouseDoubleClick(object sender, MouseEventArgs e)
{
polygonP.Add(new Point(polygonP[polygonP.Count - 1].X,
polygonP[polygonP.Count - 1].Y));
polygonP.Add(new Point(polygonP[0].X,
polygonP[0].Y));
this.panel1.Invalidate();
isDoubleClicked = true;
}
private void panel1_MouseClick(object sender, MouseEventArgs e)
{
polygonP.Add(new Point(e.X, e.Y));
this.panel1.Invalidate();
textBox1.Text += new Point(e.X, e.Y).ToString();
}
private void CalculateArea()
{
int totalArea = 0;
int x = this.Location.X + 20;
int y = this.Location.Y + 20;
int sbar = 0;
ProgressBar pbar = new ProgressBar();
pbar.Location = new Point(35, 411);
pbar.Name = "progressbar";
pbar.Width = 713;
pbar.Height = 20;
Controls.Add(pbar);
pbar.Minimum = 0;
pbar.Maximum = panel1.Size.Width - panel1.Location.X - x;
for (int i = panel1.Location.X + x; i < panel1.Size.Width; i++)
{
for (int j = panel1.Location.Y + y; j < panel1.Size.Height; j++)
{
if (GetColor(new Point(i, j)).Name == "ff000000")
{
totalArea++;
}
}
sbar += 1;
pbar.Value = sbar;
}
MessageBox.Show("Povrsina je " + totalArea.ToString() + " tacaka");
}
private Color GetColor(Point position)
{
using (var bitmap = new Bitmap(1, 1))
{
using (var graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(position, new Point(0, 0), new Size(1, 1));
}
return bitmap.GetPixel(0, 0);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (isDoubleClicked == false)
{
MessageBox.Show("Kontura nije zatvorena");
return;
}
CalculateArea();
}
}
}
Napominjem da su svi probani racunari priblizno istih performansi,kao i da je u pitanju isto okruzenje MVS 2010 kao i isti operativni sistem,Windows 7.