Paint Program in C

    4 Votes

Paint program in C is just similar to MS paint. It allows drawing pictures and saving them. The structure of C paint is divided into menu bar, tool bar, color tool bar, status bar and draw pad to draw pictures. C graphics provides 16 number of colors, so that only 16 colors are available to use. A few no. of tool bars (rectangle tool bar, line tool, circle tool  etc) are provided by C paint. Status bar displays help messages while using C paint. Draw pad facilitates the user to draw the pictures inside draw pad window. The algorithms, coding and implementation of C paint we discussed further. The execution of the programs may work a bit slowly.The coding is done in such a way that it can be improved and implemented with more applications in an easy way.

Initially C language is in text mode, it should be changed to graphics mode. Hence we must first change over to graphics mode. In our program the function initgraph() is responsible for switching the mode from text to graphics .DETECT is a macro defined in 'graphics.h'. It requests initgraph() to automatically determine which graphics driver to load in order to switch to the highest resolution graphics mode. The initgraph() function takes three parameters, the graphics driver, the graphics mode and the path to the driver file. Once the driver has been loaded, initgraph() sets up the numeric values of the graphics mode chosen in the variables gd and gm respectively. Here we are assuming that the driver files are in the directory 'c:\\turboc30\\bgi'. Hence the path passed to initgraph() is 'c:\\turboc30\\bgi'.

Structure of Paint using C

The basic structure of C paint is shown below. The screen or the window is divided into 5 areas:

  • Menu bar
  • Tool bar
  • Color tool bar
  • Status bar
  • Draw pad

The following gives a overview of the structure. Various predefined functions used to design the structure are:

  • Setbkcolor() is used to change back ground color.
  • Using outtextxy() new, open, save, about, exit options are displayed.
  • Using bar() and setfillstyle() function draw pad is drawn.
  • Using rectangle() function 6 squares are drawn with same size at fixed x position and varying y position.
  • Using rectangle() function 15 squares boxes are drawn at right side of the screen at fixed x position and varying y position.
  • A special symbol is placed at the bottom to indicate current drawing color and current back ground color and there are drawn with bar() functions.
  • Status bar placed at the bottom is drawn using bar() function.
  • In the status bar using outtextxy() x and y positions are displayed.
  • All the menu bar options are placed inside a rectangle window using rectangle() function.

The flow chart the main function which gives the entire structure of the program is show here.

Algorithm

Menubar

Menu bar provides the users with open and save options for the pictures. It consists of five options.

  • New
  • Open
  • Save
  • Credits
  • Exit

Procedure to open a new file:

  • Move mouse pointer to new option.
  • Use left click of mouse to select it.
  • Using new(), user defined function a new file is opened.

New - clear the draw pad area using bar() function.

Procedure to open an existing file

1. Move mouse pointer to open option.

2. Use left click of mouse to select it.

3. Using open(), user defined function existing file is opened.

Open

1. Using fopen() function open file given by the user.

2. Copy the data into a temp file and save it temporarily.

3. Plot each pixel using putpixel() function by reading data from temp file.

4. Close the temp file

5. If file doesn't exists display an error message (incorrect file name).

Procedure to save the picture:

1. Move mouse pointer to save option.

2. Use left click of mouse to select it.

3. Using save(), user defined function the picture is saved with given file name.

Save

1. Using fopen() function open a new file with given name in write mode.

2. Get each pixel using getpixel() function and write it into the file.

3. After writing the data close the file using fclose() function.

About

Gives the information about the creators and developers of the program.

Exit

Exits from C paint.

Toolbar

Tool bar makes the users works or drawing pictures easier. The various tool bars provided are:

1. Rectangle tool

2. Pencil tool

3. Circle tool

4. Line tool

5. Fill area tool

6. Polygon tool

Procedure to draw rectangle

1. Move mouse pointer to rectangle tool.

2. Use left click of mouse to select it.

3. Using rect(), user defined function a rectangle is drawn.

Pencil Tool

It is used for drawing images with free hand.

Circle

We can draw either circle or ellipse or circle using this tool.

Line Tool

Using the bresenhams algorithm for drawing lines, we can draw a line.

Fill Area tool

This is used to fill a closed area.

Polygon tool

By using bresenhams line algorithm, we draw the lines that are necessary to construct the polygon and use a simple for loop to join the lines.

Color tool bar

C graphics provides only 16 different colors shown in EGA directory. All these 16 colors are available in the directory “ bgi ”. The 16 colors and their corresponding integer values are given below:

BLACK (0), BLUE (1), GREEN(2), CYAN (3), RED (4), MAGENTA (5), BROWN(6),LIGHTGRAY (7), DARKGRAY(8), LIGHTBLUE (9), LIGHTGREEN (10), LIGHTCYAN (11), LIGHTRED (12), LIGHTMAGENTA (13), YELLOW (14), WHITE (15).

The colors with integer value > 8 cannot be used as background color.

Status bar

The status bar is placed at the bottom of the display screen to show the help options and also to show where the pointer is currently present.

Drawpad

A draw pad is provided to draw the required image. We can select the color the background of the drawpad. 

Mouse pointer

The various mouse functions can be accessed by setting up the AX register with different values (service number) and issuing interrupt number 51. The functions are listed below.

initmouseptr()

In this function AX is set to "1". When this function is called in main() it displays the mouse pointer. The position of the pointer can be changed by using the mouse.

hidemouseptr()

In this function AX is set to "2".When this function is called in main() it hides the mouse pointer. This function is useful while drawing figures, first the mouse pointer is kept hidden, then the figure is been drawn and again the mouse pointer is been called.

getmouseptr()

In this function AX is set to "3". This function returns the position of the mouse pointer. It contains three parameters,they are xpos,ypos,click. xpos and ypos returns the position of x co-ordinate and y co-ordinate respectively. Click is the integer variable which returns the values 1,2,3 corresponding to the button pressed on the mouse and 0 for buttons being not pressed. If any key is pressed kbhit returns nonzero integer; if not it returns zero.

setmouseptr()

In this function AX is set to "4". This function sets the mouse pointer to specific position . CX is been loaded by x co-ordinate of the mouse pointer and DX is been loaded with the y co-ordinate of the mouse pointer.

Scope for future work

1. The algorithms used in this project work bit a slow. We need to improve the algorithms.

2. More applications can be added to the current applications.

3. The program can be made user friendly by adding more help options.

4. The display of the output can be improved.

5. Currently the images are saved in raw file format. The program must be extended to save the file into .bmp or .jpeg format.

6. The algorithm if written in languages other than C can make us add more applications easily.

7. We may extend it to draw 3D images.

Conclusion

Finally we are able to produce an application that would allow us to draw images using several options provided and save them and retrieve them whenever necessary.

Attachments:
Download this file (CODE.TXT)Paint Program in C[Source Code]27 Kb

Popular Videos

communication

How to improve your Interview, Salary Negotiation, Communication & Presentation Skills.

Got a tip or Question?
Let us know

Related Articles

Travel Planner using Genetic Algorithm
Data Recovery and Undeletion using RecoverE2
PC CONTROLLED ROBOTIC CAR
Routino Router Algorithm
Data Leakage Detection
Scene Animation System Project
Data Structures and Algorithms Visualization Tool
Solving 0-1 Knapsack Problem using Genetic Algorithm
Software Watermarking Project
Android Gesture Recognition
Internet working between OSI and TCP/IP Network Managements with Security Features Requirements
Web Image Searching Engine Using SIFT Algorithm
Remote Wireless Sensor Networks for Water Quality Monitoring Requirements
Ranking Spatial Data by Quality Preferences
Scalable Learning Of Collective Behaviour
Computational Metaphor Extraction And Interpretation
Designing a domain independent Rules Engine For Business Intelligence
Graph Colouring Algorithm
Gesture Based Computing
Facial Expression Detection