AllTheTimeWorld.com

csharp drawingA drawing program with other brushes

A drawing program with other brushes

A drawing program with other brushes


Text of video

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Drawing2D;
namespace Drawing
{
    public partial class Form1 : Form
    {
        // The Brush cannot be used directly. We will assign one of the other brushes to it.
        Brush myBrush;
        SolidBrush solidBrush = new SolidBrush(Color.Black);
        TextureBrush textureBrush;
        HatchBrush hatchBrush = new HatchBrush(HatchStyle.Horizontal, Color.Red,  Color.Black);
        // The variable draw is set to true on mouse down, false on mouse up.
        // In mouse move we only draw if draw is true.
        Boolean draw = false;
        // We will use changed to rmindthe user to save when the exit or try to open new file.
        Boolean changed = false;
        int size = 2;
        public Form1()
        {
            InitializeComponent();
            myBrush = hatchBrush;
        }

        private void picDrawing_MouseMove(object sender, MouseEventArgs e)
        {
           // We only draw if the button is down.
            if (draw) {
                // Get the graphics of the picture box.
                Graphics g = picDrawing.CreateGraphics();
                Rectangle rect = new Rectangle(e.X - size, e.Y - size, size*2, size*2);
                g.FillEllipse(myBrush, rect);
             }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Image leopard = Image.FromFile(@"C:\zebra0.com\images\leopard.png", true);
            textureBrush = new TextureBrush(leopard);
        }

        private void fILEToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void mnuOpen_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "All Pictures|*.bmp;*.gif;*.png;*.jpg|JPG|*.jpg|Bitmaps|*.bmp|GIFS|*.gif|PNG|*.png";
            this.openFileDialog1.ShowDialog();
            // If the user selects "Cancel" filename will be "".
            if (openFileDialog1.FileName != "")
            {
                picDrawing.Load(openFileDialog1.FileName);
                // Display the filename in the status bar.
                this.Text = openFileDialog1.FileName;
                statusFilename.Text = openFileDialog1.FileName;
            }
        }

        private void mnuSaveAs_Click(object sender, EventArgs e)
        {
            // The only time we write is if the file doesn't already exist, or the user selects Yes.
            DialogResult result = DialogResult.Yes;
            saveFileDialog1.Filter= "All Pictures|*.bmp;*.gif;*.png;*.jpg|JPG|*.jpg|Bitmaps|*.bmp|GIFS|*.gif|PNG|*.png";
            saveFileDialog1.ShowDialog();
            if(File.Exists(saveFileDialog1.FileName))
            {
                result = MessageBox.Show("File Exists", "Overwrite existing file?", MessageBoxButtons.YesNoCancel);
            }
            if(result== DialogResult.Yes)
            {
                int width = Convert.ToInt32(picDrawing.Width);
                int height = Convert.ToInt32(picDrawing.Height);
                Image bmp = new Bitmap(width, height);
                var gg = Graphics.FromImage(bmp);
                var rect = picDrawing.RectangleToScreen(picDrawing.ClientRectangle);
                gg.CopyFromScreen(rect.Location, Point.Empty, picDrawing.Size);
                bmp.Save(saveFileDialog1.FileName);
            }

        }

        private void picDrawing_MouseDown(object sender, MouseEventArgs e)
        {
            draw = true;
        }

        private void picDrawing_MouseUp(object sender, MouseEventArgs e)
        {
            draw = false;
        }

        private void mnuSmall_Click(object sender, EventArgs e)
        {
            size = 2;
            checkSize(mnuSmall);
        }

        private void mnuMedium_Click(object sender, EventArgs e)
        {
            size = 4;
            checkSize(mnuMedium);
        }
        private void mnuLarge_Click(object sender, EventArgs e)
        {
            size = 6;
            checkSize(mnuLarge);
        }

        private void  checkSize(ToolStripMenuItem chk)
        {
            mnuSmall.Checked = false;
            mnuMedium.Checked = false;
            mnuLarge.Checked = false;
            mnuExtraLarge.Checked = false;
            chk.Checked = true;
        }

        private void mnuExtraLarge_Click(object sender, EventArgs e)
        {
            size = 8;
            checkSize(mnuExtraLarge);
        }

        private void mnuClearDrawing_Click(object sender, EventArgs e)
        {
            // Get the graphics of the picture box.
            Graphics g = picDrawing.CreateGraphics();
            // g.Clear(this.BackColor); ;
            SolidBrush eBrush = new SolidBrush(Color.Transparent);
            g.FillRectangle(eBrush,40,40,60,60);
        }

        private void mnuHatch_Click(object sender, EventArgs e)
        {
            myBrush = hatchBrush;
        }

        private void mnuSolidColor_Click(object sender, EventArgs e)
        {
            myBrush = solidBrush;
        }

        private void mnuLeopard_Click(object sender, EventArgs e)
        {
            myBrush = textureBrush;
        }
    }
}

To Do: You should spend some time with this. Use your imagination! How would you pick two colors for the hatch brush? How would you pick a size? I used a menu, but you could use a scroll bar, or a Numeric up down picker. Use a few textures, try some of the other hatch brushes, etc.

p>