Paint Application In Windows Forms Application Using F#

Introduction

In this article you'll see how to make a paint application in a Windows Forms application. With this article, you can learn how to create a paint application that enables you to draw pictures.

ColorDialog Object

The ColorDialog class represents a dialog box that displays available colors along with controls that enable the user to define custom colors. See the figure given below.

System.Windows.Forms.ColorDialog

color-dialog.jpg

ColorBlend Object

Defines arrays of colors and positions used for interpolating color blending in a multicolor gradient. This class cannot be inherited.

 System.Drawing.Drawing2D.ColorBlend

CreateGraphics( ) Method

Using the CreateGraphics method, you can draw lines and shapes, render text, or display and manipulate images with GDI+. You need to create a Graphics object. The Graphics object represents a GDI+ drawing surface, and is the object that is used to create graphical images.

    System.Windows.Forms

  • Create a Graphics Object
  • Use the Graphics object to draw lines and shapes, render text, or display and manipulate images

SmoothingMode Property

The smoothingMode Property specifies whether lines, curves, and the edges of filled areas use smoothing (also called antialiasing).

    System.Drawing

Now let's use the following procedure.

Step 1:

Open Visual Studio then click on "Create New Project" then select "F# Console Application".

create-app.jpg

Step 2:

Now go the Solution Explorer on the right side of the application. Right-click on the "References" then select "Add references".

select-references.jpg


add-references.jpg

Step 3:

After selecting "Add References", in the framework template you need to select "System.Windows.Forms" and "System.Drawing" while holding down the Ctrl key and Click on "Ok." 

import-namespaces.jpg

Step 4:

Write the following code in the F# application:

// Learn more about F# at http://fsharp.net 

//specifies the memory location of the class files 

//that will be needed in our application 

open System.Collections.Generic 

open System 

open System.Windows.Forms 

open System.ComponentModel 

open System.Drawing 

open System.Drawing.Drawing2D 

let drawingform = new Form(Text="Paint Application",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(400, 350),StartPosition=FormStartPosition.CenterScreen) 

//creates our control 

let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 200)) 

let erasebutton=new Button(Text="Erase", Location=new System.Drawing.Point(120, 200)) 

let colorbutton=new Button(Text="Brush Color", Location=new System.Drawing.Point(40, 200)) 

drawingform.Controls.Add(exitbutton) 

drawingform.Controls.Add(erasebutton) 

drawingform.Controls.Add(colorbutton) 

//creates a color dialog box 

let colordlg=new ColorDialog() 

//creates a colorblend object 

let mutable color=new ColorBlend() 

 

let gr=drawingform.CreateGraphics() 

gr.SmoothingMode<-SmoothingMode.HighQuality 

//when the form is loaded, change its color to white 

drawingform.Load.Add(fun background-> 

//set the default brush color to indigo 

color.Colors<-[|Color.Indigo|]

drawingform.BackColor<-Color.White) 

 

drawingform.MouseMove.Add(fun trail-> 

//when the mouse button is moved and the left button is clicked 

 

if (trail.Button=System.Windows.Forms.MouseButtons.Left)then 

//draw the object assign the color seleted from the color dialog as a brush color 

gr.FillRectangle(new SolidBrush(color.Colors.[0]),new Rectangle(trail.X,trail.Y,5,5)))  

//when the erase button is clicked 

//erase the object drawn in the form 

erasebutton.Click.Add(fun erase->gr.Clear(Color.White))  

//when the exit button is clicked 

//quit the form                                                                                                               

exitbutton.Click.Add(fun quit->drawingform.Close()) 

//when the brush color button is selected                                                           

colorbutton.Click.Add(fun colors-> 

//display the Color Dialog box 

if colordlg.ShowDialog()=DialogResult.OK then 

//store the value selected by the user in our colorblend object 

color.Colors<-[|colordlg.Color|]) 

//executes our application 

Application.Run(drawingform) 

 

Step 5:

Debug the application by pressing F5 and the result will appear in the application as in the following:

after-debug.jpg

Step 6:

Now you can choose any color by the click on Brush Color and draw the object.

select-brush.jpg


Step 7:

Define Custom Colors.

define-customcolr.jpg

Step 7:

draw-object.jpg

Step 8:

If you want to erase the pictures then you just need to click on the Erase button, then everything will disappear from the form.

after-debug.jpg

Summary

In this article you have seen how to make a Paint application in a Windows Forms application. A ColorDialog control allows users to launch a Windows Color Dialog and let them select a solid color or create a custom color from available colors. We discussed how to use a Windows Color Dialog and set its properties in a Windows Forms application. I hope it would help you to understand.


Similar Articles