Graphics in C Language

In a C program, first step is to initialize the graphics drivers on the computer. This is done using the initgraph method provided in graphics.h library. In the next few pages we will discuss graphics.h library in details. Important functions in graphic.h library will be discussed in details and samples programs will be provided to show the power of C programming language.

We will restrict our discussion on Graphics in C Language to 16 bit C programming and MS DOS environment and 640×480 VGA monitor.

Graphics mode Initialization

First of all we have to call the initgraph function that will initialize the graphics mode on the computer. initigraph has the following prototype.

Initgraph initializes the graphics system by loading the graphics driver from disk (or validating a registered driver) then putting the system into graphics mode. Initgraph also resets all graphics settings (color, palette, current position, viewport, etc.) to their defaults, then resets graphresult to 0.

*graphdriver

Integer that specifies the graphics driver to be used. You can give graphdriver a value using a constant of the graphics_drivers enumeration type whcih is listed in graphics.h. Normally we use value as “0” (requests auto-detect). Other values are 1 to 10 and description of each enumeration type is listed here.

*graphmode

Integer that specifies the initial graphics mode (unless *graphdriver = DETECT). If *graphdriver = DETECT, initgraph sets *graphmode to the highest resolution available for the detected driver. You can give *graphmode a value using a constant of the graphics_modes enumeration type and description of each enumeration type is listed here.

*pathtodriver

Specifies the directory path where initgraph looks for graphics drivers (*.BGI) first.

  1. If they’re not there, initgraph looks in the current directory.
  2. If pathtodriver is null, the driver files must be in the current directory.

*graphdriver and *graphmode must be set to valid graphics_drivers and graphics_mode values or you’ll get unpredictable results. (The exception is graphdriver = DETECT.)

After a call to initgraph, *graphdriver is set to the current graphics driver, and *graphmode is set to the current graphics mode. You can tell initgraph to use a particular graphics driver and mode, or to auto detect the attached video adapter at run time and pick the corresponding driver. If you tell initgraph to auto detect, it calls detectgraph to select a graphics driver and mode.

Normally, initgraph loads a graphics driver by allocating memory for the driver (through _graphgetmem), then loading the appropriate .BGI file from disk. As an alternative to this dynamic loading scheme, you can link a graphics driver file (or several of them) directly into your executable program file.

Here is a simple program that initializes the graphics mode in C programming language and print the line in graphics mode.

The below program draws a circle in the current drawing color with its center at (150,150) and the radius (100) given by radius.

Normally the screen which we see in DOS/Command Mode is in the text mode which means it is meant for text only. And for graphics we need to initialize graphics mode using initgraph() method defined in graphics.h?.

The circle command takes a X coordinate which means Vertical axis and Y coordinate which means Horizontal axis. And the last one is the radius of the circle.

closegraph();

This function unloads the graphics drivers and returns the screen back to text mode.

Here a sample program to illustrate how to use BARS which are used for visual statistics. The bar is filled using the current fill pattern and fill color. Bar method accepts parameters i.e. left, top, right and bottom. The setfillstyle() method can be used to fill the bar with a different color or pattern.

146 Comments

    • Grpahics mode in C language can be initializes using initgraph function. This function Initializes the graphics system.

      To start the graphics system, you must first call initgraph.

      initgraph initializes the graphics system by loading a graphics driver from disk (or validating a registered driver) then putting the system into graphics mode.

      initgraph also resets all graphics settings (color, palette, current
      position, viewport, etc.) to their defaults, then resets graphresult to 0.

    • i m creating a clock program using c but i cant use vgagl.h. because my pc not support a vgagl.h . can anyone send how to install and set the path in linux vgagl.h

    • i m creating a clock program using c but i cant use vgagl.h. because my pc not support a vgagl.h . can anyone send how to install and set the path in linux vgagl.h

    • vgagl.h is not must.

      Try This program

      #include
      #include
      #include
      #include
      #include

      void draw_clock();
      void draw_hand(int,int);
      void draw_digital(int,int,int);
      int midx,midy;
      main()
      {
      int driver=DETECT,mode=0;
      initgraph(&driver,&mode,”c:/tc/bgi”);
      struct time *t;
      midx=getmaxx()/2;
      midy=getmaxy()/2;
      draw_clock();
      while(!kbhit())
      {
      gettime(t);
      draw_hand(2,t->ti_hour%12*5);
      draw_hand(1,t->ti_min);
      draw_hand(0,t->ti_sec);
      draw_digital(t->ti_hour,t->ti_min,t->ti_sec);
      delay(1000);
      cleardevice();
      draw_clock();
      }
      closegraph();
      getch();
      }
      void draw_digital(int h,int m,int s)
      {
      char *hh,*mm,*ss;
      hh = itoa(h,hh,10);
      mm = itoa(m,mm,10);
      ss = itoa(s,ss,10);
      outtextxy(midx-25,midy+120,hh);
      outtextxy(midx-15,midy+120,” : “);
      outtextxy(midx,midy+120,mm);
      outtextxy(midx+15,midy+120,” : “);
      outtextxy(midx+25,midy+120,ss);
      }
      void draw_hand(int type,int value) // 0=Second, 1=Min, 2 = Hour
      {
      double x,y,angle;
      int length,thickness,color;
      length=(type==0)?85:(type==1)?80:75;
      thickness=(type==0)?1:(type==1)?2:3;
      color=(type==0)?RED:(type==1)?BLUE:GREEN;
      setcolor(color);
      setlinestyle(0,1,thickness);
      angle = ( ( 90 – (value * 6) ) * M_PI ) / 180;
      x= length*cos(angle);
      y=length*sin(angle);
      line(midx,midy,midx+x,midy-y);
      }
      void draw_clock()
      {
      int i,hours=12;
      char *msg;
      double radian,x,y;
      setlinestyle(0,1,3);
      setcolor(YELLOW);
      circle(midx,midy,100);
      setcolor(RED);
      settextjustify(1,1);
      for(i=90;i<450;i+=30)
      {
      msg=itoa(hours,msg,10);
      radian=(i*M_PI)/180;
      x=90 * cos(radian);
      y=90* sin(radian);
      outtextxy(midx+x,midy-y,msg);
      hours–;
      }
      setlinestyle(0,1,1);
      setfillstyle(1,BLUE);
      fillellipse(midx,midy,2,2);
      }

    • Here is another graphics project.

      ——– Original Message ——–
      i want to know the mini-progects in graphics using c language.

  1. Here everything is provided to use in programme.
    i have been trying to run the graphic programe
    it does not give any error but not also works.
    i have heard about that error encounters becoz of not being enabled the graphic option from the graphics library.I used it that way also, but still not working.
    plz fix that problem for us.
    Thank you

    • Hello
      Have you tried to give the proper path for the grphics library like

      initgraph(&dr,&mode,”Path to graphis lib”);
      path to graphics lib = “c:\tc\bgi”

      If you turbo c program is installed in C drive. Check to see if you are providing the proper path to the graphics library.

      If you are still having the problem ask again..and we will try to help you out.

      ——– Original Message ——–
      Here everything is provided to use in programme.
      i have been trying to run the graphic programe
      it does not give any error but not also works.
      i have heard about that error encounters becoz of not being enabled the graphic option from the graphics library.I used it that way also, but still not working.
      plz fix that problem for us.
      Thank you

    • At first you define a structure with “typedef” keyword. Like

      After defining the structure then you can create objects of this above structure.And the syntax is:
      student s;//Here s be the object of this structure student.

  2. I want to have the coding of the game “square(dots and line)” in graphics using c/c++ urgently as I have to submit it in my college or I’ll fail.
    HELP! HELP!

  3. hi, can you please give sample graphics programs using dos.h? the amazing the better. i hope i can receive the program/s immediately. thank..

  4. write a program using one dimensional array that searches a number if it is found on the list of the given 5 input numbers and locate its exact location in the list…..

    SAMPLE INPUT/OUTPUT DIALOGUE:
    enter a list of numbers: 5 4 8 2 6
    enter a number to be searhed: 2
    2 is found in location 4

    pls….help….

    • hi i try this pgm and got o/p.if u want take this code

      ——– Original Message ——–
      write a program using one dimensional array that searches a number if it is found on the list of the given 5 input numbers and locate its exact location in the list…..

      SAMPLE INPUT/OUTPUT DIALOGUE:
      enter a list of numbers: 5 4 8 2 6
      enter a number to be searhed: 2
      2 is found in location 4

      pls….help….

    • ——– Original Message ——–
      write a program using one dimensional array that searches a number if it is found on the list of the given 5 input numbers and locate its exact location in the list…..

      SAMPLE INPUT/OUTPUT DIALOGUE:
      enter a list of numbers: 5 4 8 2 6
      enter a number to be searhed: 2
      2 is found in location 4

      pls….help….

    • hi good morning.. could help me in making sample program that the objects are moving .?even just a short program?
      ——– Original Message ——–
      hi. good day can u create a program that will display a unique graphics using c programming please.. i need it badly.. thank u…
      dhrexelle@yahoo.com

  5. can I get the algorithm for zooming an image using turboc?
    can u tell me how to pick up the pixels of an image which i ferther wanted to process?

  6. hai
    i wants to develope the c code for cpu perfomance using graphics method.i finished graph
    with xy axis and generated a wave.But i want to give the value of y is cpu perfomance (ie cpu perfomance % level).how can i get the cpu usage % vale as input for my coding?

  7. Pleasa give me an example of c program which shows how to use coloring codes with graphics.. like for example when your going to make a cartoon character with c program and then coloring it. Thanks! ^^

  8. Can you give me the code of Identifying Pre-order,inorder and post Order traversal with the given predefined tree? Note: including graphics..and using c programming languages..(Turbo C)..thnx

    • pls lend the code for turbo c in declaring correct initialization of graphics. An example code would be better. tnx

    • plzzzzzzzzzz plzzzzzzzzz can anyone tel me how to read a bmp file in cc++ program and how can be write

  9. Hello Sir/MAdam,
    My name is chaitanya.
    Can you help me in getting a list of mini projects along with the description and source code for second year student in computer graphics using C.Its very urgent.Plz help me in collecting the mini projects list in computer graphics using C.

    Plz forward the list to the following email id:
    kilaru.chaitanya84@gmail.com

    Regards,
    Chaitanya

  10. hi Sir/Madam
    My name is Enam
    can you help me in using c++ graphics.h in Windows Because i get a message while compiling that BGI graphics not supported under Windows.im using Borland C++ 5..
    Thanks.

    • Copy all files of bgi folder and past it in d bin folder & other method is set griphics driver gd=DETECT &

      initgraph(&gd,&gm,” SPECIFY HEARE THE UR GRAPHICS BIG FOLDER WITH HOLE PATH”);

      DO IT….

      IF U GOT ERROR TELL ME ON niteshmeshram@rediffmail.com

      ——– Original Message ——–
      hi Sir/Madam
      My name is Enam
      can you help me in using c++ graphics.h in Windows Because i get a message while compiling that BGI graphics not supported under Windows.im using Borland C++ 5..
      Thanks.

    • Hi,
      I have installed TC in C drive.
      Now, I tried executing the graphics sample program.
      It doesn’t give any errors or warnings while compiling but doesn’t run.
      Gives an error “BGI Error: Graphics not initialized( use ‘initgraph’).
      I tried typing the path “c:\tc\bgi” in the parameters of initgraph function but still the same.
      I tried executing the above given sample program but again gives a runtime error “Graphics Error: Device driver not found (EGAVGA.BGI)….”
      Tried typing the ath in this program as well but still the same.
      Please help.

    • I tried typing the path “c:\tc\bgi” in the parameters of initgraph function but still the same.

      You should use “c:\\tc\\bgi” use backslash twice as \ is an escape character and C compiler interprets it differently. Also make sure you have bgi folder in you Turbo C installation folder as all the graphics drivers and libraries reside in this folder.

    • Hey.
      I tried that as well but still the same.
      I have a TC folder in my C drive.
      It has a lot of folders in it(BGI, BIN, INCLUDE…).
      All of these folders also have a lot of files in ech of them.
      I have to set path everytime I want to start programming.

    • sorry…I didnt put \\ after every folder name….thanks….it works….thanks a lot for your help

  11. i am using Turbo C++
    i m bignner in C & tring to run graphics pro. but there is an error :
    Graphics error:
    Why?
    pls give me solusation.

  12. Im using this function with the correct syntac however i wasnt able to display any miniature window of the POS screen that im trying to display…can anyone can give me an example on how to use this function properly

  13. Hi,
    I want to read a JPEG/TIFF/img image.And i want to do some work on the pixel values of that image.
    I done this one by converting that image into pixel values.
    But what i want is “without converting image into pixel values (externally),how can we do operations on that image”
    Please send the solution.
    Thanks.

  14. hello sir, I just wanna ask your help. On how can I make a 3 points in turbo c and connect them in order to have a triangle as an output of it.How can I code it sir.please he me I need It Asap….
    thanks for your help. God Bless

  15. HI,
    I’m not new to C Graphics, but I want to know more stuff in C Graphics. Could you suggest me ‘which book should I follow?’.

    my mail id: mail2merightnow at gmail.com

    Thanks with Warm Regards,
    Sankar

  16. hello i got a programming project ,the question is write a program that implements a simple library inventory management system to help library sraff manage operation.use built in(primitive)data types,program control flows,function,file input/output and array.got student and librarian module.pls help

  17. The following elements of the C programming language will be required in the implementation of this assignment:

    -> Built-in (primitive) data types
    -> Program control flows (e.g.: Selection, Repetition)
    -> Function
    -> File Input/Output
    -> Array (for strings
    Write a program that implements a simple Library Inventory Management System to help the Library staff to manage the operations in the library.

Leave a Reply