欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C#-交互式绘图

程序员文章站 2024-03-13 13:05:57
...

结果如图所示:

C#-交互式绘图

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.Windows.Forms;


namespace 交互式绘图1
{
    enum GraphType
    { empty, free, line, rectangle,ellipse,polygon,bezier,closedcurve}


    public partial class Form1 : Form
    {
        Bitmap myBitmap;
        Graphics myGraphics,myPicGraphics;
        GraphType myGraphType;
        Pen myPen, myTrackPen, myOutlinePen;
        bool startDraw=false;
        Point startPt, PrevPt, P2, P3, EndPt;
        Point TrackPt;
        int besizerPtNum;
        List<Point> myCurvePts = new List<Point>();
        public Form1()
        {
            InitializeComponent();
            myBitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            myGraphics = Graphics.FromImage(myBitmap);
            myPicGraphics = this.pictureBox1.CreateGraphics();
            myGraphType = GraphType.empty;
            myPen = new Pen(Color.Black, 3);
            myTrackPen = new Pen(myPen.Color, myPen.Width);
            myTrackPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
            myOutlinePen = new Pen(Color.Red, 1);
            myOutlinePen.DashStyle = DashStyle.Dash;


        }


        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //myGraphics.DrawRectangle(Pens.Red, e.X - 1, e.Y - 1, 3, 3);
                if (myGraphType == GraphType.free)
                {
                    startDraw = true;
                    startPt = e.Location;
                }
                if (myGraphType == GraphType.line)
                {
                    startDraw = true;
                    startPt = e.Location;
                    TrackPt = startPt;
                }
                if (myGraphType == GraphType.rectangle)
                {
                    startDraw = true;
                    startPt = e.Location;
                    TrackPt = startPt;


                }
                if (myGraphType == GraphType.ellipse)
                {
                    startDraw = true;
                    startPt = e.Location;
                    TrackPt = startPt;
                }
                if (myGraphType == GraphType.polygon)
                {
                    if (startDraw == false)
                    {
                        startDraw = true;
                        startPt = e.Location;
                        TrackPt = startPt;
                        PrevPt = startPt;
                    }
                    else
                    {
                        if (e.Clicks == 1)
                        {
                            myGraphics.DrawLine(myPen, PrevPt, e.Location);
                            PrevPt = e.Location;
                        }
                        if (e.Clicks == 2)
                        {
                            startDraw = false;
                            myGraphics.DrawLine(myPen, e.Location,startPt );
                        }
                    }
                }
                if (myGraphType == GraphType.bezier)
                {
                    if (startDraw == false)
                    {
                        startDraw = true;
                        startPt = e.Location;
                        TrackPt = startPt;
                        PrevPt = startPt;
                        besizerPtNum = 1;
                    }
                    else
                    {
                        if (e.Clicks == 1)
                        {
                            if (besizerPtNum == 1)
                            {
                                myTrackPen.Color = pictureBox1.BackColor;
                                myPicGraphics.DrawLine(myTrackPen, PrevPt, TrackPt);
                                myGraphics.DrawLine(myOutlinePen, startPt, e.Location);
                                
                                P2 = e.Location;
                                PrevPt = P2;
                                TrackPt = P2;
                                besizerPtNum = 2;
                            }
                            else if (besizerPtNum == 2)
                            {
                                myTrackPen.Color = pictureBox1.BackColor;
                                myPicGraphics.DrawLine(myTrackPen, PrevPt, TrackPt);
                                myGraphics.DrawLine(myOutlinePen, P2, e.Location);
                                P3 = e.Location;
                                PrevPt = P3;
                                TrackPt = P3;
                                besizerPtNum = 3;
                            }
                            else if (besizerPtNum == 3)
                            {
                                myTrackPen.Color = pictureBox1.BackColor;
                                myPicGraphics.DrawLine(myTrackPen, PrevPt, TrackPt);
                                myGraphics.DrawLine(myOutlinePen, P3, e.Location);
                                EndPt = e.Location;


                                myGraphics.DrawBezier(myPen, startPt, P2, P3, EndPt);


                                besizerPtNum = 1;
                                startPt = EndPt;
                                PrevPt = startPt;
                                TrackPt = startPt;
                            }
                        }
                        if (e.Clicks == 2)
                        {
                            startDraw = false;
                        }
                    }
                }
                if (myGraphType == GraphType.closedcurve)
                {
                    if (startDraw == false)
                    {
                        startDraw = true;
                        //myCurvePts.Clear();
                        myCurvePts.Add(e.Location);
                        PrevPt = e.Location;
                        TrackPt = e.Location;
                    }
                    else
                    {
                        if (e.Clicks == 1)
                        {
                            myTrackPen.Color = pictureBox1.BackColor;
                            myPicGraphics.DrawLine(myTrackPen, PrevPt, TrackPt);
                            myGraphics.DrawLine(myOutlinePen, PrevPt, e.Location);
                            myCurvePts.Add(e.Location);
                            PrevPt = e.Location;
                            TrackPt = e.Location;
                        }
                        if (e.Clicks == 2)
                        {
                            startDraw = false;
                            myGraphics.DrawClosedCurve(myPen, myCurvePts.ToArray());
                            //myGraphics.DrawCurve(myPen, myCurvePts.ToArray());
                            myCurvePts.Clear();
                        }
                    }
                }
                myPicGraphics.DrawImage(myBitmap, 0, 0);
            }
        }


        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (startDraw)
            {
                if (myGraphType == GraphType.closedcurve)
                {
                    myTrackPen.Color = pictureBox1.BackColor;
                    myPicGraphics.DrawLine(myTrackPen, PrevPt, TrackPt);
                    myTrackPen.Color = myPen.Color;
                    myPicGraphics.DrawLine(myTrackPen, PrevPt, e.Location);
                    TrackPt = e.Location;
                }
                if (myGraphType == GraphType.bezier)
                {
                    myTrackPen.Color = pictureBox1.BackColor;
                    myPicGraphics.DrawLine(myTrackPen, PrevPt, TrackPt);
                    myTrackPen.Color = myPen.Color;
                    myPicGraphics.DrawLine(myTrackPen, PrevPt, e.Location);
                    TrackPt = e.Location;
                }
                if (myGraphType == GraphType.polygon)
                {
                    myTrackPen.Color = pictureBox1.BackColor;
                    myPicGraphics.DrawLine(myTrackPen, PrevPt, TrackPt);
                    myTrackPen.Color = myPen.Color;
                    myPicGraphics.DrawLine(myTrackPen, PrevPt, e.Location);
                    TrackPt = e.Location;
                }
                if (myGraphType == GraphType.free)
                {
                    myGraphics.DrawLine(myPen, startPt, e.Location);
                    startPt = e.Location;
                }
                if (myGraphType == GraphType.line)
                {
                    myTrackPen.Color = pictureBox1.BackColor;
                    myPicGraphics.DrawLine(myTrackPen, startPt, TrackPt);
                    myTrackPen.Color = myPen.Color;
                    myPicGraphics.DrawLine(myTrackPen, startPt, e.Location);
                    TrackPt = e.Location;
                }
                if (myGraphType == GraphType.rectangle)
                {
                    myTrackPen.Color = pictureBox1.BackColor;
                    if (startPt.X <= TrackPt.X && startPt.Y <= TrackPt.Y)
                        myPicGraphics.DrawRectangle(myTrackPen, startPt.X, startPt.Y,
                                                    Math.Abs(TrackPt.X - startPt.X), Math.Abs(TrackPt.Y - startPt.Y));
                    if(startPt.X <= TrackPt.X && startPt.Y > TrackPt.Y)
                        myPicGraphics.DrawRectangle(myTrackPen, startPt.X, TrackPt.Y,
                                                    Math.Abs(TrackPt.X - startPt.X), Math.Abs(TrackPt.Y - startPt.Y));
                    if (startPt.X > TrackPt.X && startPt.Y <= TrackPt.Y)
                        myPicGraphics.DrawRectangle(myTrackPen, TrackPt.X, startPt.Y,
                                                    Math.Abs(TrackPt.X - startPt.X), Math.Abs(TrackPt.Y - startPt.Y));
                    if (startPt.X > TrackPt.X && startPt.Y > TrackPt.Y)
                        myPicGraphics.DrawRectangle(myTrackPen, TrackPt.X, TrackPt.Y,
                                                    Math.Abs(TrackPt.X - startPt.X), Math.Abs(TrackPt.Y - startPt.Y));
                    myTrackPen.Color = myPen.Color;
                    if (startPt.X <= e.X && startPt.Y <= e.Y)
                        myPicGraphics.DrawRectangle(myTrackPen, startPt.X, startPt.Y,
                                                    Math.Abs(e.X - startPt.X), Math.Abs(e.Y - startPt.Y));
                    if (startPt.X <= e.X && startPt.Y > e.Y)
                        myPicGraphics.DrawRectangle(myTrackPen, startPt.X, e.Y,
                                                    Math.Abs(e.X - startPt.X), Math.Abs(e.Y - startPt.Y));
                    if (startPt.X > e.X && startPt.Y <= e.Y)
                        myPicGraphics.DrawRectangle(myTrackPen, e.X, startPt.Y,
                                                    Math.Abs(e.X - startPt.X), Math.Abs(e.Y - startPt.Y));
                    if (startPt.X > e.X && startPt.Y > e.Y)
                        myPicGraphics.DrawRectangle(myTrackPen, e.X, e.Y,
                                                    Math.Abs(e.X - startPt.X), Math.Abs(e.Y - startPt.Y));
                    TrackPt = e.Location;
                }
                //if (myGraphType == GraphType.ellipse)
                //{
                //    myTrackPen.Color = pictureBox1.BackColor;
                //    if (startPt.X <= TrackPt.X && startPt.Y <= TrackPt.Y)
                //        myPicGraphics.DrawEllipse(myTrackPen, startPt.X, startPt.Y,
                //                                    Math.Abs(TrackPt.X - startPt.X), Math.Abs(TrackPt.Y - startPt.Y));
                //    if (startPt.X <= TrackPt.X && startPt.Y > TrackPt.Y)
                //        myPicGraphics.DrawEllipse(myTrackPen, startPt.X, TrackPt.Y,
                //                                    Math.Abs(TrackPt.X - startPt.X), Math.Abs(TrackPt.Y - startPt.Y));
                //    if (startPt.X > TrackPt.X && startPt.Y <= TrackPt.Y)
                //        myPicGraphics.DrawEllipse(myTrackPen, TrackPt.X, startPt.Y,
                //                                    Math.Abs(TrackPt.X - startPt.X), Math.Abs(TrackPt.Y - startPt.Y));
                //    if (startPt.X > TrackPt.X && startPt.Y > TrackPt.Y)
                //        myPicGraphics.DrawEllipse(myTrackPen, TrackPt.X, TrackPt.Y,
                //                                    Math.Abs(TrackPt.X - startPt.X), Math.Abs(TrackPt.Y - startPt.Y));
                //    myTrackPen.Color = myPen.Color;
                //    if (startPt.X <= e.X && startPt.Y <= e.Y)
                //        myPicGraphics.DrawEllipse(myTrackPen, startPt.X, startPt.Y,
                //                                    Math.Abs(e.X - startPt.X), Math.Abs(e.Y - startPt.Y));
                //    if (startPt.X <= e.X && startPt.Y > e.Y)
                //        myPicGraphics.DrawEllipse(myTrackPen, startPt.X, e.Y,
                //                                    Math.Abs(e.X - startPt.X), Math.Abs(e.Y - startPt.Y));
                //    if (startPt.X > e.X && startPt.Y <= e.Y)
                //        myPicGraphics.DrawEllipse(myTrackPen, e.X, startPt.Y,
                //                                    Math.Abs(e.X - startPt.X), Math.Abs(e.Y - startPt.Y));
                //    if (startPt.X > e.X && startPt.Y > e.Y)
                //        myPicGraphics.DrawEllipse(myTrackPen, e.X, e.Y,
                //                                    Math.Abs(e.X - startPt.X), Math.Abs(e.Y - startPt.Y));
                //    TrackPt = e.Location;
                //}
                myPicGraphics.DrawImage(myBitmap, 0,0);
            }
        }


        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (myGraphType == GraphType.free)
            {
                startDraw = false;
            }
            if (myGraphType == GraphType.line)
            {
                startDraw = false;
                EndPt = e.Location;
                myGraphics.DrawLine(myPen, startPt, EndPt);
            }
            if (myGraphType == GraphType.rectangle)
            {
                startDraw = false;
                if (startPt.X <= e.X && startPt.Y <= e.Y)
                    myGraphics.DrawRectangle(myPen, startPt.X, startPt.Y,
                                                Math.Abs(e.X - startPt.X), Math.Abs(e.Y - startPt.Y));
                if (startPt.X <= e.X && startPt.Y > e.Y)
                    myGraphics.DrawRectangle(myPen, startPt.X, e.Y,
                                                Math.Abs(e.X - startPt.X), Math.Abs(e.Y - startPt.Y));
                if (startPt.X > e.X && startPt.Y <= e.Y)
                    myGraphics.DrawRectangle(myPen, e.X, startPt.Y,
                                                Math.Abs(e.X - startPt.X), Math.Abs(e.Y - startPt.Y));
                if (startPt.X > e.X && startPt.Y > e.Y)
                    myGraphics.DrawRectangle(myPen, e.X, e.Y,
                                                Math.Abs(e.X - startPt.X), Math.Abs(e.Y - startPt.Y));
            }
            //if (myGraphType == GraphType.ellipse)
            //{
            //    startDraw = false;
            //    if (startPt.X <= e.X && startPt.Y <= e.Y)
            //        myGraphics.DrawEllipse(myPen, startPt.X, startPt.Y,
            //                                    Math.Abs(e.X - startPt.X), Math.Abs(e.Y - startPt.Y));
            //    if (startPt.X <= e.X && startPt.Y > e.Y)
            //        myGraphics.DrawEllipse(myPen, startPt.X, e.Y,
            //                                    Math.Abs(e.X - startPt.X), Math.Abs(e.Y - startPt.Y));
            //    if (startPt.X > e.X && startPt.Y <= e.Y)
            //        myGraphics.DrawEllipse(myPen, e.X, startPt.Y,
            //                                    Math.Abs(e.X - startPt.X), Math.Abs(e.Y - startPt.Y));
            //    if (startPt.X > e.X && startPt.Y > e.Y)
            //        myGraphics.DrawEllipse(myPen, e.X, e.Y,
            //                                    Math.Abs(e.X - startPt.X), Math.Abs(e.Y - startPt.Y));
            //}
            myPicGraphics.DrawImage(myBitmap, 0, 0);
        }


        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(myBitmap, 0, 0);
        }


        private void *画线ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            myGraphType = GraphType.free;
        }


        private void 直线ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            myGraphType = GraphType.line;
        }


        private void 矩形ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            myGraphType = GraphType.rectangle;
        }


        private void 椭圆ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            myGraphType = GraphType.ellipse;
        }


        private void 多边形ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            myGraphType = GraphType.polygon;
        }


        private void bezier曲线ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            myGraphType = GraphType.bezier;
        }


        private void 闭合曲线ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            myGraphType = GraphType.closedcurve;
        }


    }
}

相关标签: C# 交互式绘图