Simple Paint Program Using C

Note: You are free to download, use, edit, update and rebuild the program and source codes downloaded from here, But Please don’t forget to give considerable credits to Dreamlover Technology and bestengineeringprojects.com if you do so.

A Paint Program is a program which allows you to draw pictures and symbols into the screen.

Here you will find the tutorial to build a Simple prototype of a paint program. The software build will not be reliable and perfect enough to implement it in a real working environment like to edit a picture, but this project is very helpful to build a basis and experience on Graphics Programming using C. And you can also certainly, Expand and enhance this program to make it implementable in real filed.

C programming is the best programming if you love both simplicity and direct access to core technology. If you use lower level programming languages like assembly then it will be too hard and tedious even to build a simple program, And if you use higher level languages Like Visual Basics, then even though you will be able to almost everything you can do from C, it will be like telling your computer “do this” and your computer does the work with you ever knowing how the computer did that task!That’s the exact reason why we are using “C” here.

So, let’s get started!

If you already know enough about Graphics Library and API and can also program easily using C in DEV C++ Ide , then you can directly download dev c++ , IDE , install WinBGIm Graphics  library , start a new project , copy the code from below and compile the project.

DEV c++ Ide and External Graphics Library:

To develop this Paint Program, We will be using the DEV c++ IDE (Integrated Development Environment)  , Which you download from the official site of DEV c++ (http://www.bloodshed.net/devcpp.html), Also for handling the graphics we will be using the WinBGIm Graphics Library provided by the Computer Science department of Colorado University the full tutorial to implement the WinBGIm Graphics Library into Dev C++ ide can be read from the official site of WinBGIm Graphics Library (http://www.cs.colorado.edu/~main/bgi/dev-c++/  ).

Also the official documentation regarding the available functions and method that can used in the Graphics Library are provided by the official site of the library (http://www.cs.colorado.edu/~main/bgi/doc/  )

Preparation for program:

So to get started first of all download the latest version of DEV c++ , also download the Graphics Library and header file from the links provided above.

Then:

Copy the graphics.h file and paste it to the Include folder of Dev c++ ; usually it is:
“C:/Dev-Cpp/include” but it may vary according to the path where you install the dev c++ Ide.

Now copy the copy the libbgi.a file and paste it in the library folder of dev c++ ide ; usually it is:

“C:/Dev-Cpp/lib”

Now open the DEV c++ IDE and click on :

file>new>project>empty project

and choose a path where you would like to save your project and project name.

Now click on:

file>new>source file

paint_program_devc

Now click alt+p   or click on: project>project option

And then click on “Parameters” tab and paste following code in the text field below linker:

-lbgi

-lgdi32

-lcomdlg32

-luuid

-loleaut32

-lole32

Now you are ready to code.

The Code:

Now you can simply pate following code in the source file and compile , execute it. ( If you would like to know more about the code or would like to read the description of the code then scroll down.):

Using the Simple Paint Program:

After you execute the program or run the exe which you can find the the path where you saved your project, you can see a screen as shown below:

You can see that two windows are opened from the one which contains graphics, you can choose the options by clicking on them, and it also contains a drawing pad , and the one which looks like a command console is to input certain data’s when the program asks for it , like while drawing the circle , you need to enter the radius of circle on this window.

So as you can see this program allows to draw:

Line, Circle, Rectangle and free hand drawing in one of the three  colors  you  choose   ( read , green and blue ) ; By adding further logics and codes into the program you can also add option to draw other shapes , and also in other colors.

It also allows you to erase the drawing if you want to. Through  further  advancement in the code you can also add the option to save and open bitmap images or even jpeg or images in other formats.

Explanation of the code:

If you are good in C programming and do a through study of the  document  of WinBGIm  graphics library (  http://www.cs.colorado.edu/~main/bgi/doc/ ) can easily understand the code.

I have included a simple and not so deep explanation of the code here:

main() function:

It displays a “Input Datas” text in the  console, then it  initializes a 600*600 window where we are going to do every task except  inputting the data’s through keyboard.

Then it calls the function  “windowform()” , which prints the  initial  data’s , and boxes , drawing pad and  colors  in the new windows initialized by “main()” function.

Then it calls the mouseclcik() function which detects the mouse click made by the user and calls the function which draws the objects in drawing pad.

windowform() function:

So the code of windowform() function goes like this:

 

This function: with the help of  function  from graphics.h like bar(), setcolor() etc ; builds the  initial  appearance  of the    window.

This function displays the Pencil, Rectangle, Line, Circle, Exit, Eraser control in the window and also displays the drawing pad and the color option.

mouseclick() function:

This function includes a infinite loop which runs the codes in the function again and again (The program exits only if the exit option in the  window  is not clicked. the exit is performed by using the exit() function. )

It also makes use of  ismouseclick() , mousex() and mousey() function from the graphics.h header file to detect the mouse click by user and also find the position of mouse click to determine which  command  is clicked , and attaches  corresponding  text to the variable “cmd” and “clr” to save the data for the  command  and color option clicked by user.

Then when the user clicks on the drawing pad, this function  handles the variable and program flow to another function  drawnow() to start drawing the objects in the drawing pad.

drawnow() function:

drawnow() function handles the process of drawing or  outputting  the user selected objects to the drawing pad.

The code of drawnow() function goes like:

This function draws the objects into the drawing pad using the functions of graphics.h header file like:

line() , circle() , rectangle() …..

For free hand drawing and erasing it makes the use of function  ismouseclick() , mousex() , mousey() … to find the position of  continuously  changing position of  mouse  while the left button is clicked down , and outputs a color to the current pixel of mouse and it’s surrounding pixels.

And in the case when the exit command is selected this function returns the program flow to the main  function, by braking all the loops and levels of functions, which ends the program.

Note: To understand this code perfectly I suggested to study the  documentation of the functions used here from grahphics.h file from this link:  http://www.cs.colorado.edu/~main/bgi/doc/ , the site contains a  brief  explanation of all the functions supported by the WinBGIm graphics  library , which is extensively used to code this simple paint program.

Leave a Comment