Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
4.2 kB
3
Indexable
Never
        enum Tool
        {
            Pencil,
            Eraser,
            Elipse,
            Rectangle,
            Line
        }

        Bitmap bitmap;
        Graphics graphics;
        bool canPaint=false;

        Pen pencil = new Pen(Color.Black, 1);
        Pen eraser = new Pen(Color.White, 30);
        Point Start, End;

        int width, height;
        Point shapeStart, shapeEnd;

        Tool actualTool = Tool.Pencil;
        ColorDialog color = new ColorDialog();

        public Form1()
        {
            InitializeComponent();
            bitmap = new Bitmap(Canvas.Width, Canvas.Height);
            graphics = Graphics.FromImage(bitmap);
            Canvas.Image = bitmap;
            pbColor.BackColor = Color.Black;
        }

        private void btnClear_Click(object sender, System.EventArgs e)
        {
            graphics.Clear(Color.White);
            /*do sprawdzenia*/
            Canvas.Image = bitmap;
        }

        private void Canvas_MouseDown(object sender, MouseEventArgs e)
        {
            canPaint = true;
            /*współrzędne kursora*/
            Start = e.Location;
            End = e.Location;

            /*wspołrzędne rysowanej figury*/
            shapeStart = e.Location;
        }

        private void Canvas_MouseUp(object sender, MouseEventArgs e)
        {
            canPaint = false;

            //width = Math.Abs(shapeEnd.X - shapeStart.X);
            //height = Math.Abs(shapeEnd.Y - shapeStart.Y);
            if (actualTool == Tool.Rectangle)
                graphics.DrawRectangle(pencil, shapeStart.X, shapeStart.Y, width, height);
            if (actualTool == Tool.Elipse)
                graphics.DrawEllipse(pencil, shapeStart.X, shapeStart.Y, width, height);
            if (actualTool == Tool.Line)
                graphics.DrawLine(pencil, shapeStart, shapeEnd);
        }

        private void btnElipse_Click(object sender, EventArgs e)
        {
            actualTool = Tool.Elipse;
        }

        private void btnRectangle_Click(object sender, EventArgs e)
        {
            actualTool = Tool.Rectangle;
        }

        private void btnLine_Click(object sender, EventArgs e)
        {
            actualTool = Tool.Line;
        }

        private void Canvas_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            if (canPaint)
            {
                if (actualTool == Tool.Rectangle)
                    g.DrawRectangle(pencil, shapeStart.X, shapeStart.Y, width, height);
                if (actualTool == Tool.Elipse)
                    g.DrawEllipse(pencil, shapeStart.X, shapeStart.Y, width, height);
                if (actualTool == Tool.Line)
                    g.DrawLine(pencil, shapeStart, shapeEnd);
            }
        }

        private void btnColor_Click(object sender, EventArgs e)
        {
            if (color.ShowDialog() != DialogResult.OK)
                return;

            pencil.Color = color.Color;
            pbColor.BackColor = color.Color;
        }

        private void btnPencil_Click(object sender, System.EventArgs e)
        {
            actualTool = Tool.Pencil;
        }

        private void btnEraser_Click(object sender, System.EventArgs e)
        {
            actualTool = Tool.Eraser;
        }

        private void Canvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (canPaint)
            {
                if (actualTool == Tool.Pencil)
                {
                    End = e.Location;
                    graphics.DrawLine(pencil, Start, End);
                    Start = End;
                }
                else if (actualTool == Tool.Eraser)
                {
                    End = e.Location;
                    graphics.DrawLine(eraser, Start, End);
                    Start = End;
                }
            }

            Canvas.Refresh();
            shapeEnd = e.Location;
            width = Math.Abs(shapeEnd.X - shapeStart.X);
            height = Math.Abs(shapeEnd.Y - shapeStart.Y);
        }
Leave a Comment