namespace 画图_0043
{
public enum ShapeType //定义形状类型
{
Line, Rectangle, Ellipse, FreeDraw, FillRectangle, Curve, FillEllipse, Circle, FillCircle, Eraser, Pick, Font1, Polygon
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
color = colorHatch1.CurrentColor;
mousePath = new GraphicsPath();
}
public Bitmap bitmap = null;
ShapeType type;
Color color;
Shape shape;
Point p1, p2;
Graphics g;
List<Shape> shapeList = new List<Shape>();
Curve tmpCurve;
Polygon tmpPolygon;
GraphicsPath mousePath;
Dtext fm1 = new Dtext();
//点击鼠标左键开始绘制图形
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
p1 = new Point(e.X, e.Y);
if (type == ShapeType.Curve)
tmpCurve.AddPoint(p1);
if (type == ShapeType.Polygon)
tmpPolygon.AddPoint(p1);
}
//点击鼠标右键
if (e.Button == MouseButtons.Right)
{
shapeList.Add(tmpCurve);
tmpCurve = new Curve(color);
shapeList.Add(tmpPolygon);
tmpPolygon = new Polygon(color);
}
pictureBox1.Refresh();
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
//鼠标选择所要绘制的图形控件时...
if (e.Button == MouseButtons.Left)
{
p2 = new Point(e.X, e.Y);
switch (type)
{
case ShapeType.Line:
shape = new Line(color, p1, p2);
break;
case ShapeType.FreeDraw:
mousePath.AddLine(p2, p2);
shape = new Eraser(color, mousePath);
break;
case ShapeType.Ellipse:
shape = new Ellipse(color, p1, p2);
break;
case ShapeType.FillEllipse:
shape = new FillEllipse(color, p1, p2);
break;
case ShapeType.Circle:
shape = new Circle(color, p1, p2);
break;
case ShapeType.FillCircle:
shape = new FillCircle(color, p1, p2);
break;
case ShapeType.Curve:
shape = new Curve(color);
break;
case ShapeType.Eraser:
break;
case ShapeType.Pick:
break;
case ShapeType.Polygon:
break;
case ShapeType.Font1:
shape = new Font1(color, p1, fm1.xv());
break;
default:
break;
}
pictureBox1.Refresh();
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
//鼠标松开,图形确定
if (shape != null)
shapeList.Add(shape);
shape = null;
//清理自由绘图时的鼠标路径
mousePath = new GraphicsPath();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
//显示绘图
g = e.Graphics;
foreach (Shape s in shapeList)
s.Draw(g);
if (shape != null)
shape.Draw(g);
if (tmpCurve != null)
tmpCurve.Draw(g);
if (tmpPolygon != null)
tmpPolygon.Draw(g);
}
//圆
private void circle_Click(object sender, EventArgs e)
{
type = ShapeType.Circle;
}
//填充圆
private void fillcircle_Click(object sender, EventArgs e)
{
type = ShapeType.FillCircle;
}
// 椭圆
private void ellipse_Click(object sender, EventArgs e)
{
type = ShapeType.Ellipse;
}
//填充椭圆
private void fillellipse_Click(object sender, EventArgs e)
{
type = ShapeType.FillEllipse;
}
//矩形
private void rectangle_Click(object sender, EventArgs e)
{
type = ShapeType.Rectangle;
}
//填充矩形
private void fillrectangle_Click(object sender, EventArgs e)
{
type = ShapeType.FillRectangle;
}
//划线
private void line_Click(object sender, EventArgs e)
{
g = Graphics.FromImage(bitmap);
type = ShapeType.Line;
}
//画笔
private void point_Click(object sender, EventArgs e)
{
g = Graphics.FromImage(bitmap);
type = ShapeType.FreeDraw;
}
//曲线
private void curve_Click(object sender, EventArgs e)
{
type = ShapeType.Curve;
tmpCurve = new Curve(color);
}
//字体
private void font_Click(object sender, EventArgs e)
{
fm1.Show();
type = ShapeType.Font1;
}
private void pick_Click(object sender, EventArgs e)
{
type = ShapeType.Pick;
}
//多边形
private void btnPolygon_Click(object sender, EventArgs e)
{
type = ShapeType.Polygon;
tmpPolygon = new Polygon(color);
}
//选择颜色
private void colorHatch1_ColorChange_1(object sender, MyControl.ColorHatch.ColorChangeEventArgs e)
{
color = colorHatch1.CurrentColor;
}
private void Form1_Load(object sender, EventArgs e)
{
bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
g = Graphics.FromImage(bitmap);
this.pictureBox1.Controls.Clear();
Graphics.FromImage(bitmap).Clear(Color.White); //消除底图的黑色
this.pictureBox1.Image = (Bitmap)bitmap.Clone();
}
//图像设计代码如下
//记录源图文件路径
string currentImagrFile;
//源图像
Bitmap sourceBitmap;
//真正执行编译的图像
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
if (sourceBitmap != null)
{
g.DrawImage(sourceBitmap ,378,12,sourceBitmap.Width ,sourceBitmap .Height );
}
}
//打开原图像
private void tbnLoad_Click(object sender, EventArgs e)
{
//打开一个选择文件对话框以便加载要处理的图像
openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "所有 图片文件(*.bmp/*.jpg/*.gif)|*.*|Jpeg文件(*.jpg)|*.jpg|Bitmap文件(*.bmp)|*.bmp|gif文件(*.gif)|*.gif";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (DialogResult.OK == openFileDialog1.ShowDialog())
{
currentImagrFile = openFileDialog1.FileName;
//显示所加载的源图像以便与处理后的图像对比
pictureBox1.Image = Bitmap.FromFile(currentImagrFile ,false );
//备份所加载的图像以便快速重新加载
sourceBitmap = (Bitmap)Image.FromFile(currentImagrFile );
//对窗体进行重新绘制,这将强制执行Paint事件处理程序
Invalidate();
//当重新加载图像时,应重置标示
this .label1 .Text ="源图像";
this.label2 .Text ="源图像(待处理)";
}
}
//保存图像
private void tbnSave_Click(object sender, EventArgs e)
{
if (sourceBitmap !=null )
{
Bitmap bitmap = sourceBitmap;
//选择图像保存位置和保存类型
saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Bitmap文件(*.bmp)|*.bmp|Jpeg文件(*.jpg)|*.jpg|gif文件(*.gif)|*.gif";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (DialogResult.OK ==saveFileDialog1 .ShowDialog ())
{
//保存图像文件
bitmap .Save (saveFileDialog1.FileName );
}
}
else
{
MessageBox.Show ("无处理后的图像可保存。","提示",MessageBoxButtons .OK ,MessageBoxIcon.Error );
}
}
//二值化
private void btnBinarize_Click(object sender, EventArgs e)
{
//调用二值化方法
Binarize();
this.label2 .Text ="二值化图";
Invalidate();
}
//二值化方法
public void Binarize()
{
if (sourceBitmap !=null )
{
string Rbrthreshold = Interaction.InputBox("请输入一个-255~255之间的值。\n\n注: 正负值可使二值化为不同的“黑白效果“。", "二值化值设置", "70",100,100);
if (Rbrthreshold == "" || Convert.ToInt16(Rbrthreshold) < -255 || Convert.ToInt16(Rbrthreshold) > 255)
{
MessageBox.Show("值必须在-255~255之间。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
Rectangle rect = new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height);
System.Drawing.Imaging.BitmapData bmpData = sourceBitmap.LockBits(rect ,System.Drawing .Imaging .ImageLockMode .ReadWrite ,sourceBitmap.PixelFormat);
unsafe
{
int blue;
int RgbTh = Convert.ToInt16(Rbrthreshold );
byte *ptr=(byte *)(bmpData .Scan0 );
for (int i=0;i<bmpData.Height ;i++)
{
for(int j=0;j<bmpData .Width ;j++)
{
if (RgbTh > 0)
{
blue = (int)(ptr[0] < System.Math.Abs(RgbTh) ? 0 : 255);
}
else
{
blue = (int)(ptr[0] >System.Math.Abs(RgbTh) ? 0 : 255);
}
//green = ptr[1];
//red=ptr [2];
ptr [0]=ptr [1]=ptr [2]=(byte)(blue);
ptr += 3;
}
ptr += bmpData.Stride - bmpData.Width * 3;
}
}
sourceBitmap.UnlockBits(bmpData);
}
}
else
{
MessageBox.Show("无图像可处理", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
// 滤波
private void btnFilter_Click(object sender, EventArgs e)
{
//调用滤波的方法
Filter();
this.label2.Text = "滤波图";
Invalidate();
}
// 滤波的 方法
public void Filter()
{
if (sourceBitmap != null)
{
Color c = new Color();
int rr, gg,bb, r1, g1, b1, i, j, rx, gx, bx, k1, k2;
for (i = 1; i < sourceBitmap.Width - 1; i++)
{
for (j = 1; j < sourceBitmap.Height - 1; j++)
{
rx = 0; gx = 0; bx = 0;
for (k1 = -1; k1 <= 1; k1++)
{
for (k2 = -1; k2 <= 1; k2++)
{
c = sourceBitmap.GetPixel(i + k1, j + k2);
r1 = c.R;
g1 = c.G;
b1 = c.B;
rx = rx + r1;
gx = gx + g1;
bx = bx + b1;
}
}
rr = (int)(rx / 8);
gg = (int)(gx / 8);
bb = (int)(bx / 8);
if (rr > 255) rr = 255;
if (gg > 255) gg = 255;
if (bb > 255) bb = 255;
Color c1 = Color.FromArgb(rr, gg, bb);
sourceBitmap.SetPixel(i, j, c1);
}
}
}
else
{
MessageBox.Show("无图像可处理", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
shape类
public abstract class Shape
{
protected Color color;
public Shape(Color c)
{
this.color = c;
}
public abstract void Draw(Graphics g);
}
//定义线条
public class Line : Shape
{
protected Point p1, p2;
public Line(Color c, Point p1, Point p2)
: base(c)
{
this.p1 = p1;
this.p2 = p2;
}
public override void Draw(Graphics g)
{
g.DrawLine(new Pen(color), p1, p2);
}
}
//定义自由绘画
public class Eraser : Shape
{
protected Color white;
GraphicsPath path;
public Eraser(Color c, GraphicsPath path)
: base(c)
{
this.path = path;
}
public override void Draw(Graphics g)
{
g.DrawPath(new Pen(white), path);
}
}
//定义椭圆
public class Ellipse : Line
{
protected Point lc;
protected int Width, Height;
public Ellipse(Color c, Point p1, Point p2)
: base(c, p1, p2)
{
Width = Math.Abs(p1.X - p2.X);
Height = Math.Abs(p1.Y - p2.Y);
int a = p1.X < p2.X ? p1.X : p2.X;
int b = p1.Y < p2.Y ? p1.Y : p2.Y;
lc = new Point(a, b);
}
public Point lctn
{
get
{
return lc;
}
set
{
lc = value;
}
}
public override void Draw(Graphics g)
{
g.DrawEllipse(new Pen(color), lc.X, lc.Y, Width, Height);
}
}
//定义填充椭圆
public class FillEllipse : Ellipse
{
public FillEllipse(Color c, Point p1, Point p2) : base(c, p1, p2) { }
public override void Draw(Graphics g)
{
g.FillEllipse(new SolidBrush(color), lc.X, lc.Y, Width, Height);
}
}
//定义圆形
public class Circle : Line
{
protected Point lc;
protected int Width, Height;
public Circle(Color c, Point p1, Point p2)
: base(c, p1, p2)
{
double r = Math.Sqrt((Math.Abs(p1.X - p2.X) * Math.Abs(p1.X - p2.X)) + (Math.Abs(p1.Y - p2.Y) * Math.Abs(p1.Y - p2.Y)));
double a = p1.X - r;
double b = p1.Y - r;
Width = Convert.ToInt32(2 * r);
Height = Convert.ToInt32(2 * r);
lc = new Point(Convert.ToInt32(a), Convert.ToInt32(b));
}
public Point lctn
{
get
{
return lc;
}
set
{
lc = value;
}
}
public override void Draw(Graphics g)
{
g.DrawEllipse(new Pen(color), lc.X, lc.Y, Width, Width);
}
}
//定义填充圆形
public class FillCircle : Circle
{
public FillCircle(Color c, Point p1, Point p2) : base(c, p1, p2) { }
public override void Draw(Graphics g)
{
g.FillEllipse(new SolidBrush(color), lc.X, lc.Y, Width, Height);
}
}
//定义曲线
public class Curve : Shape
{
public List<Point> Points;
public Curve(Color c)
: base(c)
{
Points = new List<Point>();
}
public void AddPoint(Point p)
{
Points.Add(p);
}
public override void Draw(Graphics g)
{
if (Points.Count >= 2)
g.DrawCurve(new Pen(color), Points.ToArray());
}
}
//定义形状
public class Polygon : Shape
{
public List<Point> Points;
public Polygon(Color c)
: base(c)
{
Points = new List<Point>();
}
public void AddPoint(Point p)
{
Points.Add(p);
}
public override void Draw(Graphics g)
{
if (Points.Count >= 2)
g.DrawPolygon(new Pen(color), Points.ToArray());
}
}
public class Font1 : Shape
{
protected PointF loca;
protected float Width, Height;
protected String text;
public Font1(Color c, Point p1, String t)
: base(c)
{
this.text = t;
int a = p1.X;
int b = p1.Y;
loca = new PointF(a, b);
}
public PointF Location
{
get
{
return loca;
}
set
{
loca = value;
}
}
public override void Draw(Graphics g)
{
g.DrawString(text, new Font("Arial", 10), Brushes.Black, Location);
}
}