Graphics.h File For C

Graphics.h File For C Rating: 4,8/5 5889 reviews

C graphics using graphics.h functions or WinBGIM (Windows 7) can be used to draw different shapes, display text in different fonts, change colors and many more. Using functions of graphics.h in Turbo C compiler you can make graphics programs, animations, projects, and games. You can draw circles, lines, rectangles, bars and many other geometrical figures. You can change their colors using the available functions and fill them. Following is a list of functions of graphics.h header file. Every function is discussed with the arguments it needs, its description, possible errors while using that function and a sample C graphics program with its output.

  1. Graphics.h Header File C++
  2. Graphics.h Header File For Dev C++

C graphics

C graphics examples

I have been searching to get the source code of the header file graphics.h and its associated library in order to integrate it with my C program. At the same time, I am interested in those cross-platform libraries that works on more than one compiler. The graphics.h header file provides access to a simple graphics library that makes it possible to draw lines, rectangles, ovals, arcs, polygons, images, and strings on a graphical window. The second step is initialize the graphics drivers on the computer using initgraph method of graphics.h library.

1. Drawing concentric circles

#include <graphics.h>

int main()
{
int gd = DETECT, gm;
int x =320, y =240, radius;
initgraph(&gd,&gm,'C:TCBGI');
for( radius =25; radius <=125; radius = radius +20)
circle(x, y, radius);
getch();
closegraph();
return0;
}

2. C graphics program moving car

#include <graphics.h>
#include <dos.h>

int main()
{
int i, j =0, gd = DETECT, gm;

initgraph(&gd,&gm,'C:TCBGI');

settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(25,240,'Press any key to view the moving car');

getch();

for( i =0; i <=420; i = i +10, j++)
{
rectangle(50+i,275,150+i,400);
rectangle(150+i,350,200+i,400);
circle(75+i,410,10);
circle(175+i,410,10);
setcolor(j);
delay(100);

if( i 420)
break;
if( j 15)
j =2;

cleardevice();// clear screen
}

getch();
closegraph();
return0;
}

C graphics functions

C graphics programs

Graphics in Windows 7 or Vista

Most of the functions are two dimensional except bar3d which draws a 3d bar, you can also implement these functions using already existing algorithms. You can also use these functions in C++ programs. You can use these functions for developing programs in Windows 7 and Vista using Dev C++ compiler. For that you need to download an additional package WinBGIm, download WinBGIm. Now open Dev C++ compiler go to Tools->Package Manager, use install button and then browse the package location. Now create a new project and select WinBGIm. This library also offers many functions which can be used for image manipulation, you can open image files, create bitmaps and print images, RGB colors and mouse handling.

Compiling graphics codes on CodeBlocks IDE shows an error: “Cannot find graphics.h”. This is because graphics.h runs is not available in the library folder of CodeBlocks. To successfully compile graphics code on CodeBlocks, setup winBGIm library.

How to include graphics.h in CodeBlocks ?

Please follow below steps in sequence to include “graphics.h” in CodeBlocks to successfully compile graphics code on Codeblocks.
Step 1 : To setup “graphics.h” in CodeBlocks, first set up winBGIm graphics library. Download WinBGIm from http://winbgim.codecutter.org/ or use this link.

Step 2 : Extract the downloaded file. There will be three files:

  • graphics.h
  • winbgim.h
  • libbgi.a
  • Step 3 : Copy and paste graphics.h and winbgim.h files into the include folder of compiler directory. (If you have Code::Blocks installed in C drive of your computer, go through: Disk C >> Program Files >> CodeBlocks >> MinGW >> include. Paste these two files there.)


    Step 4 : Copy and paste libbgi.a to the lib folder of compiler directory.

    Step 5 : Open Code::Blocks. Go to Settings >> Compiler >> Linker settings.

    Step 6 : In that window, click the Add button under the “Link libraries” part, and browse.
    Select the libbgi.a file copied to the lib folder in step 4.

    Step 7 : In right part (ie. other linker options) paste commands

    -lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32

    Step 8 : Click Ok

    Step 9 : Try compiling a graphics.h program in C or C++, still there will be an error. To solve it, open graphics.h file (pasted in include folder in step 3) with Notepad++. Go to line number 302, and replace that line with this line : int left=0, int top=0, int right=INT_MAX, int bottom=INT_MAX,

    Step 10 : Save the file. Done !

    Note : Now, you can compile any C or C++ program containing graphics.h header file. If you compile C codes, you’ll still get an error saying: “fatal error: sstream : no such file directory”.

    For this issue, change your file extension to .cpp if it is .c

    Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.

    Recommended Posts:

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Graphics.h Header File C++

    Please Improve this article if you find anything incorrect by clicking on the 'Improve Article' button below.

    Graphics.h Header File For Dev C++