Graphics in C Language
Graphics in C Language
We will restrict our discussion on Graphics in C Language to 16 bit C programming and MS DOS environment. In a C Program first of all you need 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 discuees in details and samples programmes will be provided to show the power of C programming language.
Graphics mode Initialization
First of all we have to call the initgraph function that will intialize the graphics mode on the computer. initigraph have the following prototype.
void initgraph(int far *graphdriver, int far *graphmode, char far *pathtodriver);
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.
*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.
*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.
*pathtodriver
Specifies the directory path where initgraph looks for graphics drivers (*.BGI) first.
- If they’re not there, initgraph looks in the current directory.
- 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 autodetect the attached video adapter at run time and pick the corresponding driver. If you tell initgraph to autodetect, 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 sample program that initializes the graphics mode in C Language.
#include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> int main(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; /* initialize graphics mode */ initgraph(&gdriver, &gmode, ""); /* read result of initialization */ errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); /* return with error code */ } /* draw a line */ line(0, 0, getmaxx(), getmaxy()); /* clean up */ getch(); closegraph(); return 0; }
The graphics programming in c language is discussed in brief to provide an over view to the beginner.
/* Sample program to draw a circle*/ #include<graphics.h> #include<conio.h> main() { int gd=DETECT,gm; initgraph(&gd,&gm,""); /* initialization of graphic mode */ circle(150,150,100); getch(); closegraph(); /* Restore orignal screen mode */ } /* End of program */
Normally the screen which u view in DOS is in the text mode which means it is meant for text. And for graphics u need to initialize graphics mode. And for this to happen u need to include graphics.h?.
circle(x coordinate ,y coordinate , radius);
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();
With out this function the screen mode will still remain in graphic mode and when u come out, to DOS u will see a different screen, which is not in the text mode.
/*A program to draw a space with stars*/ #include<graphics.h> main() { int gd=DETECT,gm; int i,x,y; initgraph(&gd,&gm,""); line(0,0,640,0); line(0,0,0,480); line(639,0,639,480); line(639,479,0,479); for(i=0;i<=1000;i++) { x=rand()%639; y=rand()%480; putpixel(x,y,15); } getch(); closegraph(); } /* End of program */
/*Here a sample program to illustrate how to use BARS which are used for visual statistics */ #include<graphics.h> main() { int gd=DETECT,gm,maxx,maxy,x,y,button; initgraph(&gd,&gm,""); line(80,150,200,150); line(80,150,80,50); settextstyle(1,HORIZ_DIR,1); outtextxy(100,153,"<-X axis"); settextstyle(1,VERT_DIR,1); outtextxy(60,50,"<-Y axis"); bar(100,100,120,150); bar(130,120,150,150); getch(); closegraph(); }
Pages: [Page - 1] [Page - 2] [Page - 3] [Page - 4]
Tags: C Programming, C++ Programming, Graphics
Like What you See?
Become one of the regulars by subscribing! You'll be the first to know when we add more great posts just like this. Join up by either RSS Feeds or Email Updates today!
There are 130 Comments to this post. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response or TrackBack from your own site.



































I want to know,to create a program of classes implemintation in c++. thank’s
I want to know to write a program like game in c++.
I want to know aboiut the pprogram which can print the
location of mouse in C
I want to know how create game in graphics
I want to know about the program which can print the location of mouse (in vc++)
I want to know how to initialise mouse in c or c++
Here is the programme that you want. Initilize the mouse in c Language and take the input from mouse.
http://www.mycplus.com/featured-articles/programming-mouse-in-c-and-c/
——– Original Message ——–
i want to know how to initialise mouse in c or c++
i wants to draw the system performance (ie cpu usage % level)in c coding with graphical method. how can i get the cpu usage percentage value as input for my program?
I want to know the initialization of graphics mode .
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.
Constant Values __________________ DETECT | 0 (requests autodetection) CGA | 1 MCGA | 2 EGA | 3 EGA64 | 4 EGAMONO | 5 IBM8514 | 6 HERCMONO | 7 ATT400 | 8 VGA | 9 PC3270 | 10 #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> int main(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; /* initialize graphics mode */ initgraph(&gdriver, &gmode, ""); /* read result of initialization */ errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ { printf("Graphics error: %s ", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); /* return with error code */ } /* draw a line */ line(0, 0, getmaxx(), getmaxy()); /* clean up */ getch(); closegraph(); return 0; }I want to know morer about graphics in c or c++
Have a look at this page, it has many graphics utilities writen in C and C languages. You can learn alot by examining the source code provided with these programs.
http://www.mycplus.com/category/free-utilities/
——– Original Message ——–
i want to know morer abot graphics in c or c++
I want to know the mini-progects in graphics using c language.
Here are the links for Graphics projects
http://www.mycplus.com/free-utilities/scientific-calculator/
http://www.mycplus.com/free-utilities/stack-implementation/
——– Original Message ——–
i want to know the mini-progects in graphics using c language.
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
Here is another graphics project.
——– Original Message ——–
i want to know the mini-progects in graphics using c language.
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
how to create a object in c language
At first you define a structure with “typedef” keyword. Like
typedef struct student{ char name[10]; int roll; char class[5]; };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.
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!
some sample of c graphics program related to practical purpose or some games or space related progem
i want simple project to show the animation using c language and graphics tool .
projrct with program code
send me on om_sharma87 at yahoo.com
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..
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
#include<stdio.h> #include<conio.h> void main() { int i,n,a[10],k,j,b=0; printf(" Enter the number of elements in array"); scanf("%d",&n); printf(" enter a list of numbers:"); for(i=1;i<=n;i++) { scanf("%d",&a[i]); } printf(" enter a number to be searched:"); scanf("%d",&k); for(j=1;j<=n;j++) { if(a[j]==k) { printf("%d is found in location %d",k,j); b++; } } if(b==0) { printf(" The number is not present in the array."); } getch(); }——– 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 Dude here is a example of searching in one dimensional array....*/ #include<stdio.h> #include<conio.h> void main() { int i,j,k,a[10]; clrscr(); printf("Enter nay 5 number:"); for(i=0i<5;i++) { scanf("%d",&a[i]); } printf("Enter again any number to search in the list:"); scanf("%d",&j); for(i=0;i<5;i++) { if(a[i]==j) k=a[i]; } if(k==j) { printf("Number found in the list:"); } else { printf"Number not found in the list:"); } getch(); } // plz send your feedback about this program at "pandeypavankumar@gmail.com"——– 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 day can u create a program that will display a unique graphics please.. i need it badly.. thank u…
dhrexelle@yahoo.com
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
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
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?
i want to know how we can take input in graphic mode…i.e not in text mode..
plz tell dat function or some method.
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?
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! ^^
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
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
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
I am in search of such a good person to help me in programming a simple voting program through C. According to this program there will be different symbols and voting will be done by clicking it. If anyone is there, please help me to complete this. I am just a learner on c but i have assignment to do so.
please write me mail
http://www.anjan.com.np
anjanbhattarai1@yahoo.com
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.
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
can anyone help on how to use this functions using C?
send me hints or sample codes using these functions: jhane_pretz31@yahoo.com or panaligan_robertjay@yahoo.com
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.
I want to know the simple concepts of graphics programming.because now i am new to this subject.plz give me the idea friends.
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
hello,
I have made one program in C.In which i want movement of ball from a person’s leg to the goal.just help me
respected sir or mem,
I have a problem loading BGI in my pc . can you sugges me that how can i run BGI graphics in my pc.
Hello sir,
i am santosh,i haven’t any information about c graphics. i am very curious to know about c graphics.i repuest you to please ,send me notes about c graphics on my e-mail id step by step
E-mailid-san_desh1986@reddiffmail.com
sir,please do this for me.good day
santosh
Hello sir,
i am santosh,i haven’t any information about c graphics. i am very curious to know about c graphics.i repuest you to please ,send me notes about c graphics on my e-mail id step by step
E-mailid-san_desh1986@reddiffmail.com
sir,please do this for me.good day
santosh
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
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
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.
hai i’m seenu.. i want to more about graphics in c.. can you suggest me. my mail-id following here.
seenuge@gmail.com
How to color one face of a cube in C language?
i want to learn all about graphical programming in C language where do i start
Can anybody tel me how to generate an EKG/ECG waveform using graphic.h in C
Can u otherwise mail me the tutorials for learning C-Graphics… M interested in knowin it. My id is viji_ram1989@yahoo.co.in
give me nman some samples of graphics in turbo c++..ung madali lng xa pro magnda,,,need k kc xa sa isa kng subject,,,thanx,,,asap pls
give me nman some samples of graphics in turbo c++..ung madali lng xa pro magnda,,,need k kc xa sa isa kng subject,,,thanx,,,asap pls
asap plz,,can u send to my email add more samples of graphics in turbo c++..i need it in my subject..
5 samples?plz?thanx a lot…this s my email add,
gharylle_08@yahoo.com
can you send me some algorithm more about graphics in c…and how to improve it…this my email..rmark_023@yahoo.com…thanks..
Is it necessary to initialize the graphic drivers. In some programs we are not using the initialization method?
amit srivastava:
You can capture the mouse movement using MFC in VC++ in the following way. Create a standard MFC exe file and in DocumentView calss there will be mouse events i.e. OnMouseMove(), OnLButtonDown(), you can get the position of mouse in any function. Both the function accepts two parameters (UINT nFlags, CPoint point). And in Point object there will be mouse position (x,y) and you can get them easily.
@raj:
You can get a calculator progam here.
http://www.mycplus.com/free-utilities/scientific-calculator/
@Gcablay_18:
There are tutorials available here in C++ for creating classes.
http://www.mycplus.com/tutorials/cplusplus-programming-tutorials/classes-2/
@vineet:
This function shutdown the graphics mode and returns to the position it was before the initgraph function was called. Closegraph function releases all the resources occupied by the graphics system like memry, fonts, drivers etc…
You can learn more about graphics functions here
http://www.mycplus.com/featured-articles/c-language-graphics-library-reference-part-1/