题目:
界面:
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _2017_2绘图程序
{
public partial class Form1 : Form
{
Bitmap myBitmap;
Graphics myBMPGraphics, myPicGraphics;
Pen myPen, myClearTrackPen,mytransPen,mydongtaiPen, myClearTrackPen1;
bool startDrawMark = false;
Point startPt, prePt, preTrackPt;
List<Point> myLinePts = new List<Point>();
GraphicsPath myPath;
Matrix myMatrix;
public Form1()
{
InitializeComponent();
myBitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
myBMPGraphics = Graphics.FromImage(myBitmap); //内存的位图画板
myPicGraphics = this.pictureBox1.CreateGraphics(); //窗体PictureBox的画板
myPen = new Pen(Color.Blue, 3);
myClearTrackPen = new Pen(pictureBox1.BackColor, 3); //清除动态橡皮线的笔
myClearTrackPen1 = new Pen(pictureBox1.BackColor, 3);
myClearTrackPen1.DashStyle = DashStyle.Dot;
mytransPen = new Pen(Color.Red, 3);//平移的笔
mydongtaiPen = new Pen(Color.Blue, 3);
mydongtaiPen.DashStyle = DashStyle.Dot;
myMatrix = new Matrix();
myPath = new GraphicsPath(); //路径
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
//if (myLinePts.Count > 1)
//{
// myBMPGraphics.TranslateTransform(10, 10, MatrixOrder.Prepend);
// myBMPGraphics.DrawLines(mytransPen, myLinePts.ToArray());
// myPicGraphics.DrawImage(myBitmap, 0, 0);
//}
if (myLinePts.Count > 1)
{
myMatrix.Reset();
myMatrix.Translate(10, 10);
myPath.Transform(myMatrix);
// myPath.Transform是会保留,比如第一次点击平移,后面它的矩阵就变为平移一次的矩阵
//之后在平移也是在原来平移一次矩阵的基础上进行平移
//因此关键在于点要在结束的时候获取,并且重新绘制的时候重置矩阵
myBMPGraphics.DrawPath(mytransPen, myPath);
myPicGraphics.DrawImage(myBitmap, 0, 0);
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(myBitmap, 0, 0);
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (e.Clicks == 1)
{
if (startDrawMark == false)
{
//myBMPGraphics.ResetTransform();
startDrawMark = true;
startPt = e.Location;
prePt = startPt;
preTrackPt = startPt;
myPath.Reset();
myLinePts.Clear();
myLinePts.Add(e.Location);
}
else
{
//myPicGraphics.DrawLine(myClearTrackPen, prePt, e.Location);
myBMPGraphics.DrawLine(myPen, prePt, e.Location);
prePt = e.Location;
preTrackPt = e.Location;
myLinePts.Add(e.Location);
}
}
if(e.Clicks==2)
{
myBMPGraphics.DrawLine(myPen, e.Location, startPt);
//if(myLinePts.Count>1)
//{
// myBMPGraphics.DrawLines(myPen, myLinePts.ToArray());
//}
startDrawMark = false;
myPath.AddLines(myLinePts.ToArray());
}
myPicGraphics.DrawImage(myBitmap, 0, 0);
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if(startDrawMark)
{
myPicGraphics.DrawLine(myClearTrackPen1, prePt, preTrackPt);
myPicGraphics.DrawLine(mydongtaiPen, prePt, e.Location);
preTrackPt = e.Location;
myPicGraphics.DrawImage(myBitmap, 0, 0);
}
}
}
2017某油的绘图程序题目,2024年考了一道类似的。