该实例是仿画板的实例,里面的代码精致,代码的可移植性强,希望能给做这方面的工程的人一些借鉴,给大家带来帮助!
private void picDraw_MouseMove(object sender, MouseEventArgs e)
{
if (IsDraw)
{
EndPoint = e.Location;
if (dType != DrawType.Pen && dType != DrawType.Eraser)
{
finishImg = (Image)originImg.Clone();
}
g = Graphics.FromImage(finishImg);
g.SmoothingMode = SmoothingMode.AntiAlias; //抗锯齿
switch (dType)
{
case DrawType.Line:
g.DrawLine(p, StartPoint, EndPoint);
break;
case DrawType.Pen:
g.DrawLine(p, StartPoint, EndPoint);
StartPoint = EndPoint;
break;
case DrawType.Rect:
Point leftTop = new Point(StartPoint.X, StartPoint.Y);
int width = Math.Abs(StartPoint.X - e.X), height = Math.Abs(StartPoint.Y - e.Y);
if (e.X < StartPoint.X)
leftTop.X = e.X;
if (e.Y < StartPoint.Y)
leftTop.Y = e.Y;
Rectangle rect = new Rectangle(leftTop, new Size(width, height));
g.DrawRectangle(p, rect);
break;
case DrawType.Ellipse:
leftTop = new Point(StartPoint.X, StartPoint.Y);
int Ewidth = Math.Abs(StartPoint.X - e.X), Eheight = Math.Abs(StartPoint.Y - e.Y);
if (e.X < StartPoint.X)
leftTop.X = e.X;
if (e.Y < StartPoint.Y)
leftTop.Y = e.Y;
rect = new Rectangle(leftTop, new Size(Ewidth, Eheight));
g.DrawEllipse(p, rect);
break;
case DrawType.Eraser:
Pen pen1 = new Pen(Color.White, 20);
pen1.StartCap = LineCap.Round;
pen1.StartCap = LineCap.Round;
g.DrawLine(pen1, StartPoint, EndPoint);
StartPoint = EndPoint;
pen1.Dispose();
break;
case DrawType.Write: //写字前画虚线框
leftTop = new Point(StartPoint.X, StartPoint.Y);
int w = Math.Abs(StartPoint.X - e.X);
int h = Math.Abs(StartPoint.Y - e.Y);
if (e.X < StartPoint.X)
leftTop.X = e.X;
if (e.Y < StartPoint.Y)
leftTop.Y = e.Y;
FontRect = new Rectangle(leftTop, new Size(w, h));
Pen pRect = new Pen(Color.Black);
pRect.DashPattern = new float[] { 4.0F, 2.0F, 1.0F, 3.0F };
g.DrawRectangle(pRect, FontRect);
pRect.Dispose();
break;
}
reDraw();
}
}
private void picDraw_MouseUp(object sender, MouseEventArgs e)
{
IsDraw = false;
originImg = (Bitmap)finishImg;
if (dType == DrawType.Write)
{
//清除虚线框
Pen pRect = new Pen(Color.White);
g.DrawRectangle(pRect, FontRect);
pRect.Dispose();
//写字文本框 呈现
txtWrite.SetBounds(FontRect.Left, FontRect.Top, FontRect.Width, FontRect.Height);
txtWrite.Font = font;
FontPoint = FontRect.Location;
txtWrite.Visible = true;
txtWrite.Focus();
}
//此句的作用是避免窗体最小化后还原窗体时,画布内容“丢失”
//其实没有丢失,只是没刷新而已,读者可以在画布任意处作画,便可还原画布内容
picDraw.Image = originImg;
}
/// <summary>
/// 重绘绘图区(二次缓冲技术)
/// </summary>
private void reDraw()
{
Graphics graphics = picDraw.CreateGraphics();
graphics.DrawImage(finishImg, new Point(0, 0));
graphics.Dispose();
}
/// <summary>
/// 在画布 写字
/// </summary>
/// <param name="str"></param>
private void DrawString(string str)
{
g.DrawString(str, font, new SolidBrush(DrawColor), FontPoint);
reDraw();
}
/// <summary>
/// 在画布 作画
/// </summary>
/// <param name="img"></param>
private void DrawImage(Image img)
{
g = Graphics.FromImage(finishImg);
g.DrawImage(img, new Point(0, 0));
reDraw();
}
private void btnImport_Click(object sender, EventArgs e)
{
txtWrite.Visible = false;
txtWrite.Text = "";
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image file(*.jpg;*.png;*.bmp)|*.jpg;*.png;*.bmp";
if (ofd.ShowDialog() == DialogResult.OK)
{
DrawImage(Image.FromFile(ofd.FileName));
}
}
评论