This article shows how to create a Mini Paint Application using a C# Windows Forms application. I will show some basic features of the paint application. Before starting, I hope you have a basic undersanding of GDI+ Graphics. I have added comments to the code so that you can easily understand the code.
Let's understand the Mini Paint Application structure:
Here is the code used in this application:
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. namespace MiniPaint
  5. {
  6. public partial class Form1 : Form
  7. {
  8. public Form1()
  9. {
  10. InitializeComponent();
  11. g = pnl_Draw.CreateGraphics();
  12. }
  13. bool startPaint = false;
  14. Graphics g;
  15. //nullable int for storing Null value
  16. int? initX = null;
  17. int? initY = null;
  18. bool drawSquare = false;
  19. bool drawRectangle = false;
  20. bool drawCircle = false;
  21. //Event fired when the mouse pointer is moved over the Panel(pnl_Draw).
  22. private void pnl_Draw_MouseMove(object sender, MouseEventArgs e)
  23. {
  24. if(startPaint)
  25. {
  26. //Setting the Pen BackColor and line Width
  27. Pen p = new Pen(btn_PenColor.BackColor,float.Parse(cmb_PenSize.Text));
  28. //Drawing the line.
  29. g.DrawLine(p, new Point(initX ?? e.X, initY ?? e.Y), new Point(e.X, e.Y));
  30. initX = e.X;
  31. initY = e.Y;
  32. }
  33. }
  34. //Event Fired when the mouse pointer is over Panel and a mouse button is pressed
  35. private void pnl_Draw_MouseDown(object sender, MouseEventArgs e)
  36. {
  37. startPaint = true;
  38. if (drawSquare)
  39. {
  40. //Use Solid Brush for filling the graphic shapes
  41. SolidBrush sb = new SolidBrush(btn_PenColor.BackColor);
  42. //setting the width and height same for creating square.
  43. //Getting the width and Heigt value from Textbox(txt_ShapeSize)
  44. g.FillRectangle(sb, e.X, e.Y, int.Parse(txt_ShapeSize.Text), int.Parse(txt_ShapeSize.Text));
  45. //setting startPaint and drawSquare value to false for creating one graphic on one click.
  46. startPaint = false;
  47. drawSquare = false;
  48. }
  49. if(drawRectangle)
  50. {
  51. SolidBrush sb = new SolidBrush(btn_PenColor.BackColor);
  52. //setting the width twice of the height
  53. g.FillRectangle(sb, e.X, e.Y, 2*int.Parse(txt_ShapeSize.Text), int.Parse(txt_ShapeSize.Text));
  54. startPaint = false;
  55. drawRectangle = false;
  56. }
  57. if(drawCircle)
  58. {
  59. SolidBrush sb = new SolidBrush(btn_PenColor.BackColor);
  60. g.FillEllipse(sb, e.X, e.Y, int.Parse(txt_ShapeSize.Text), int.Parse(txt_ShapeSize.Text));
  61. startPaint = false;
  62. drawCircle = false;
  63. }
  64. }
  65. //Fired when the mouse pointer is over the pnl_Draw and a mouse button is released.
  66. private void pnl_Draw_MouseUp(object sender, MouseEventArgs e)
  67. {
  68. startPaint = false;
  69. initX = null;
  70. initY = null;
  71. }
  72. //Button for Setting pen Color
  73. private void button1_Click(object sender, EventArgs e)
  74. {
  75. //Open Color Dialog and Set BackColor of btn_PenColor if user click on OK
  76. ColorDialog c = new ColorDialog();
  77. if(c.ShowDialog()==DialogResult.OK)
  78. {
  79. btn_PenColor.BackColor = c.Color;
  80. }
  81. }
  82. //New
  83. private void newToolStripMenuItem_Click(object sender, EventArgs e)
  84. {
  85. //Clearing the graphics from the Panel(pnl_Draw)
  86. g.Clear(pnl_Draw.BackColor);
  87. //Setting the BackColor of pnl_draw and btn_CanvasColor to White on Clicking New under File Menu
  88. pnl_Draw.BackColor = Color.White;
  89. btn_CanvasColor.BackColor = Color.White;
  90. }
  91. //Setting the Canvas Color
  92. private void btn_CanvasColor_Click_1(object sender, EventArgs e)
  93. {
  94. ColorDialog c = new ColorDialog();
  95. if(c.ShowDialog()==DialogResult.OK)
  96. {
  97. pnl_Draw.BackColor = c.Color;
  98. btn_CanvasColor.BackColor = c.Color;
  99. }
  100. }
  101. private void btn_Square_Click(object sender, EventArgs e)
  102. {
  103. drawSquare = true;
  104. }
  105. private void btn_Rectangle_Click(object sender, EventArgs e)
  106. {
  107. drawRectangle = true;
  108. }
  109. private void btn_Circle_Click(object sender, EventArgs e)
  110. {
  111. drawCircle = true;
  112. }
  113. //Exit under File Menu
  114. private void exitToolStripMenuItem_Click(object sender, EventArgs e)
  115. {
  116. if(MessageBox.Show("Do you want to Exit?","Exit",MessageBoxButtons.YesNo,MessageBoxIcon.Information)==DialogResult.Yes)
  117. {
  118. Application.Exit();
  119. }
  120. }
  121. //About under Help Menu
  122. private void aboutMiniPaintToolStripMenuItem_Click(object sender, EventArgs e)
  123. {
  124. About a = new About();
  125. a.ShowDialog();
  126. }
  127. }
  128. }
Final preview:
I hope you like it. Thanks.