using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication2
{
public partial class Form4 : Form
{
Pen p = null;
Graphics g = null;
List<Point> line = new List<Point>();
List<Point> points = new List<Point>();
int t = 0;
double length = 0;
int pointcount = 0;
SolidBrush b = new SolidBrush(Color.Red);
public Form4()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog openfileDialog = new OpenFileDialog();
openfileDialog.Filter = "Image files (JPeg, Gif, Bmp, etc.)|*.jpg;*.jpeg;*.gif;*.bmp;*.tif; *.tiff; *.png|" +
"JPeg files (*.jpg;*.jpeg)|*.jpg;*.jpeg |GIF files (*.gif)|*.gif |BMP files (*.b" +
"mp)|*.bmp|Tiff files (*.tif;*.tiff)|*.tif;*.tiff|Png files (*.png)| *.png |All f" +
"iles (*.*)|*.*";
if ((openfileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK))
{
this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
this.pictureBox1.ImageLocation = openfileDialog.FileName;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
length = 0;
if (pictureBox1.Image == null)
{
MessageBox.Show("请添加图片。","提示",MessageBoxButtons.OK);
}
if (e.Button == MouseButtons.Left)
{
g.FillRectangle(b, e.X, e.Y, 2, 2);
if (line.Count > 0 )
{
line.Clear();
t = 0;
}
line.Add(new Point(e.X, e.Y));
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
g.FillRectangle(b,e.X,e.Y,2,2);
line.Add(new Point(e.X,e.Y));
length += Math.Sqrt(Math.Pow((e.X - line[line.Count()-2].X), 2) + Math.Pow((e.Y - line[line.Count()-2].Y), 2));
if (line.Count > 1)
{
g.DrawLine(p, line[line.Count - 2].X, line[line.Count - 2].Y, line[line.Count - 1].X, line[line.Count - 1].Y);
}
}
}
private void Form4_Load(object sender, EventArgs e)
{
if (p == null)
{
p = new Pen(Color.Red, 2f);
}
if(g==null)
{
g=this.pictureBox1.CreateGraphics();
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (pictureBox1.Image == null)
{
return;
}
else
{
for (int i = 0; i < line.Count() - 1; i++)
{
e.Graphics.DrawLine(p, line[i].X, line[i].Y, line[i + 1].X, line[i + 1].Y);
}
if (pointcount != 0)
{
t = 0;
drawPoints(line,pointcount,e);
}
}
}
public void drawPoints(List<Point> pline, int nun, PaintEventArgs e)
{
double step = 0;
double len = 0;
Point p = new Point();
if (nun == 1)
{
p = pline[0];
t++;
drawPoint(p, e);
if (e == null)
{
points.Add(p);
}
}
else
{
step = length /(nun - 1);
for (int i = 0; i < pline.Count(); i++)
{
if (i != pline.Count() - 1)
{
len += Math.Sqrt(Math.Pow((pline[i + 1].X - pline[i].X), 2) + Math.Pow((pline[i + 1].Y - pline[i].Y), 2));
}
if (i == pline.Count() - 1 || i == 0)
{
p = pline[i];
t++;
drawPoint(p, e);
if (e == null)
{
points.Add(p);
}
}
else if (len >= step)
{
p = pline[i - 1];
t++;
drawPoint(p, e);
if (e == null)
{
points.Add(p);
}
len = 0;
}
}
}
}
public void drawPoint(Point pl, PaintEventArgs e)
{
if (e == null)
{
Font drawFont = new Font("Arial", 11, FontStyle.Bold);
g.DrawString(t.ToString(), drawFont, new SolidBrush(Color.Lime), pl.X-10, pl.Y-8);
g.DrawEllipse(new Pen(Color.Blue, 2), pl.X - 10, pl.Y - 10, 20, 20);
}
else
{
Font drawFont = new Font("Arial", 11, FontStyle.Bold);
e.Graphics.DrawString(t.ToString(), drawFont, new SolidBrush(Color.Lime), pl.X - 10, pl.Y - 8);
e.Graphics.DrawEllipse(new Pen(Color.Blue, 2), pl.X - 10, pl.Y - 10, 20, 20);
}
}
private void button2_Click(object sender, EventArgs e)
{
pointcount = Int32.Parse(this.comboBox1.Text);
drawPoints(line, pointcount,null);
}
float zhi = 100;
private void button3_Click(object sender, EventArgs e)
{
foreach (Point po in points)
{
showResult(po,zhi);
}
}
public void showResult(Point p,float testvar)
{
float te = 0;
te = testvar / 2;
g.DrawEllipse(new Pen(Color.Red, 2), p.X-te, p.Y-te, testvar, testvar);
if (testvar >= 0 && testvar < 30)
{
SolidBrush sb1 = new SolidBrush(Color.FromArgb(60,Color.Gray));
g.FillEllipse(sb1, p.X-te, p.Y-te, testvar, testvar);
}
else if (testvar >= 30 && testvar < 60)
{
SolidBrush sb2 = new SolidBrush(Color.FromArgb(60,Color.Green));
g.FillEllipse(sb2, p.X-te, p.Y-te, testvar, testvar);
}
else if (testvar >= 60 && testvar < 90)
{
SolidBrush sb3 = new SolidBrush(Color.FromArgb(60,Color.Blue));
g.FillEllipse(sb3, p.X-te, p.Y-te, testvar, testvar);
}
else
{
SolidBrush sb4 = new SolidBrush(Color.FromArgb(60,Color.Red));
g.FillEllipse(sb4, p.X-te, p.Y-te, testvar, testvar);
}
}
private void button4_Click(object sender, EventArgs e)
{
Bitmap bt = new Bitmap(pictureBox1.Image);
string savePath = @"F:\";
JpegSave(bt, savePath, 1000000L);
}
}
}