C# - Simple Drawing Application
Submitted by razormist on Thursday, May 3, 2018 - 22:11.
In this tutorial we will create a Simple Drawing Application using C#. C# is a general-purpose, object-oriented programming language. C# automatically manages inaccessible object memory using a garbage collector, which eliminates developer concerns and memory leaks. C# is faster than dynamically typed languages because things are more clearly defined. It contains several classes that support any C# platforms, like game development. It has a friendly environment for all new developers. So let's do the coding.
or also you create the layout by dragging the proper tools to the forms.
Then write these variables inside the Class of the form called Form1.
Lastly, initialize this method inside the Form1 method to create the canvas when the application start running.
Try to run the application and see if it works.
There you go we successfully create a Simple Drawing Application using C#. I hope that this tutorial help you understand on how to develop an application using C#. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!
Getting Started
First you will have to download & install the Visual Studio. Visual Studios is an open source development feel free to create any application that you want. Here's the link for the Visual Studio https://www.visualstudio.com/.Application Design
We will now create the design for the application, first locate the designer file called form1.Designer.cs, this is the default name when you create a new windows form. Then write these codes inside your designer file.- namespace Simple_Drawing_Application
- {
- partial class Form1
- {
- /// <summary>
- /// Required designer variable.
- /// </summary>
- private System.ComponentModel.IContainer components = null;
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- #region Windows Form Designer generated code
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- ((System.ComponentModel.ISupportInitialize)(this.pb_canvas)).BeginInit();
- this.menuStrip1.SuspendLayout();
- this.SuspendLayout();
- //
- // pb_canvas
- //
- this.pb_canvas.BackColor = System.Drawing.Color.White;
- this.pb_canvas.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
- this.pb_canvas.Name = "pb_canvas";
- this.pb_canvas.TabIndex = 0;
- this.pb_canvas.TabStop = false;
- //
- // label1
- //
- this.label1.AutoSize = true;
- this.label1.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.label1.Name = "label1";
- this.label1.TabIndex = 1;
- this.label1.Text = "Size";
- //
- // comboBox1
- //
- this.comboBox1.Font = new System.Drawing.Font("Arial", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.comboBox1.FormattingEnabled = true;
- "8",
- "9",
- "10",
- "11",
- "12",
- "14",
- "16",
- "18",
- "20",
- "22",
- "24",
- "26",
- "28",
- "36",
- "48",
- "72"});
- this.comboBox1.Name = "comboBox1";
- this.comboBox1.TabIndex = 2;
- //
- // menuStrip1
- //
- this.exitToolStripMenuItem,
- this.editToolStripMenuItem});
- this.menuStrip1.Name = "menuStrip1";
- this.menuStrip1.TabIndex = 3;
- this.menuStrip1.Text = "menuStrip1";
- //
- // exitToolStripMenuItem
- //
- this.quitToolStripMenuItem});
- this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
- this.exitToolStripMenuItem.Text = "File";
- //
- // quitToolStripMenuItem
- //
- this.quitToolStripMenuItem.Name = "quitToolStripMenuItem";
- this.quitToolStripMenuItem.Text = "Quit";
- //
- // editToolStripMenuItem
- //
- this.clearToolStripMenuItem});
- this.editToolStripMenuItem.Name = "editToolStripMenuItem";
- this.editToolStripMenuItem.Text = "Edit";
- //
- // clearToolStripMenuItem
- //
- this.clearToolStripMenuItem.Name = "clearToolStripMenuItem";
- this.clearToolStripMenuItem.Text = "Clear";
- //
- // button1
- //
- this.button1.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.button1.Name = "button1";
- this.button1.TabIndex = 4;
- this.button1.Text = "Color";
- this.button1.UseVisualStyleBackColor = true;
- //
- // txt_color
- //
- this.txt_color.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.txt_color.Name = "txt_color";
- this.txt_color.ReadOnly = true;
- this.txt_color.TabIndex = 5;
- //
- // button2
- //
- this.button2.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.button2.Name = "button2";
- this.button2.TabIndex = 6;
- this.button2.Text = "Save";
- this.button2.UseVisualStyleBackColor = true;
- //
- // Form1
- //
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.BackColor = System.Drawing.SystemColors.ButtonHighlight;
- this.Controls.Add(this.button2);
- this.Controls.Add(this.txt_color);
- this.Controls.Add(this.button1);
- this.Controls.Add(this.comboBox1);
- this.Controls.Add(this.label1);
- this.Controls.Add(this.pb_canvas);
- this.Controls.Add(this.menuStrip1);
- this.MainMenuStrip = this.menuStrip1;
- this.Name = "Form1";
- this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
- this.Text = "Drawing Application";
- ((System.ComponentModel.ISupportInitialize)(this.pb_canvas)).EndInit();
- this.menuStrip1.ResumeLayout(false);
- this.menuStrip1.PerformLayout();
- this.ResumeLayout(false);
- this.PerformLayout();
- }
- #endregion
- private System.Windows.Forms.PictureBox pb_canvas;
- private System.Windows.Forms.Label label1;
- private System.Windows.Forms.ComboBox comboBox1;
- private System.Windows.Forms.MenuStrip menuStrip1;
- private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;
- private System.Windows.Forms.ToolStripMenuItem editToolStripMenuItem;
- private System.Windows.Forms.ToolStripMenuItem clearToolStripMenuItem;
- private System.Windows.Forms.ToolStripMenuItem quitToolStripMenuItem;
- private System.Windows.Forms.Button button1;
- private System.Windows.Forms.TextBox txt_color;
- private System.Windows.Forms.Button button2;
- }
- }
Creating the Script
This code contains the function of the application. This code will try to render the canvas to be able to draw a graphics. This contains some important button that enable you to adjust the size of the brush, can change color, and save the image that has been drawns.To do that locate the csharp script, In my case the default script will be called Form1.cs. First import this important module to avoid errors in the script.- using System.IO;
After that write all these important methods to make sure the app will work properply.
- void GenerateVaribles()
- {
- graphics = pb_canvas.CreateGraphics();
- comboBox1.Text = "8";
- color = "#000000";
- txt_color.Text = color;
- CreateCanvas();
- }
- void CreateCanvas() {
- graphics = Graphics.FromImage(bmp);
- pb_canvas.BackgroundImage = bmp;
- pb_canvas.BackgroundImageLayout = ImageLayout.None;
- }
- private void pb_canvas_MouseDown(object sender, MouseEventArgs e)
- {
- draw = true;
- start = e.Location;
- int size;
- if (comboBox1.Text == "")
- {
- size = 8;
- }
- else
- {
- size = Convert.ToInt32(comboBox1.Text);
- }
- Color newColor = ColorTranslator.FromHtml(color);
- p.SetLineCap(System.Drawing.Drawing2D.LineCap.Round, System.Drawing.Drawing2D.LineCap.Round, System.Drawing.Drawing2D.DashCap.Round);
- }
- private void pb_canvas_MouseMove(object sender, MouseEventArgs e)
- {
- if (draw)
- {
- if (e.Button == MouseButtons.Left)
- {
- end = e.Location;
- graphics.DrawLine(p, start, end);
- start = end;
- pb_canvas.Invalidate();
- }
- }
- }
- private void pb_canvas_MouseUp(object sender, MouseEventArgs e)
- {
- draw = false;
- }
- private void clearToolStripMenuItem_Click(object sender, EventArgs e)
- {
- CreateCanvas();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- if (cd.ShowDialog() == DialogResult.OK)
- {
- color = "#" + (cd.Color.ToArgb() & 0x00FFFFFF).ToString("X6");
- txt_color.Text = color;
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- s.Filter = "Png files| *.png|jpeg files| *.jpg|bitmaps | *.bmp";
- if (s.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- if (File.Exists(s.FileName))
- {
- File.Delete(s.FileName);
- }
- if (s.FileName.Contains(".jpg"))
- {
- bmp.Save(s.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
- }
- else if (s.FileName.Contains(".png"))
- {
- bmp.Save(s.FileName, System.Drawing.Imaging.ImageFormat.Png);
- }
- else if (s.FileName.Contains(".bmp"))
- {
- bmp.Save(s.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
- }
- }
- }
- private void quitToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
- GenerateVaribles();
Add new comment
- 2795 views