×

How to include graphics.h in C?

How to include graphics.h in code blocks?

Graphics .h is a header that allows drawing lines, rectangles, ovals, arcs, polygons, pictures, and strings on a graphical window by giving access to a basic graphics library.  <graphics.h> is a predefine header file in C, which allows you to use the graphics function in your C program. You need to use graphics.h header file into your C program.

You can draw geometrical shapes, design simple animations etc.

graphics.h is a header file containing functions that help you create diagrams such as circles, rectangles and other 2D figures. We can even do some basic animations, such as a moving car.

#include<graphics.h>

WinBGIM (Windows 7) or C graphics using the graphics.h functions can be used to draw, display text in various fonts, change colours, and do various other things. Using the graphics.h functions in the turbo c compiler, you can create graphics programs, animations, projects, and games. You can create circles, lines, rectangles, bars, and other geometrical shapes. You can fill them and change their colours using the available functions. The functions of the graphics.h header files are listed below. Each function is discussed in detail, including the arguments it requires, its description, potential errors when using that function, and a sample C graphics program with its output.

The header file specified in graphics.h is a header file that contains graphical functions. It is included using the preprocessor directive #include. Some of these functions include put pixel(), which plots a pixel with the specified color on a given point, and line(), which draws a line between two points.

How do I add graphics.h to Code Blocks?

Please complete the steps below in order to include "graphics.h" in code Blocks and successfully compile graphics code on Code blocks.

Step 1: Before creating "graphics.h" in Code Blocks, install the winBGIm graphics library. Windham can be downloaded from http://winbgim.codecutter.org/ or by following this link.

UPDATE: The library building at http://winbgim.codecutter.org/ is old, built around 2006. A linking error occurs when linking against that with a 64-bit compiler toolchain. A recent build of the library with 64-bit compatibility is available at here.

Step 2: Extract the downloaded file in step two. Three files will be present:

graphics.h
winbgim.h
libbgi.a

Step 3: Insert the files graphics.h and winbgim.h into the compiler directory's include folder. (If your computer's C drive has Code::Blocks installed, follow these steps: Disk C >> Program Files >> Code Blocks >> MinGW >> include. Add these two files, thereby pasting.)

Step 4: Copy and paste libbgi.an into the compiler directory's lib folder.

Step 5: Open Code: Blocks in step 5. Select Options >> Compiler >> Linker settings from the menu.

Step 6: Select "Link libraries" in that window and click the Add button. Go to the lib folder where the libbgi.a file was copied in step 4.

Step 7: Paste commands in the appropriate section (other linker options).

Step 8: lbgi lgdi32 lcomdlg32 luuid loleaut32 lole32

Step 9: There will still be an error if you attempt to compile graphics.h application in C or C++. To fix it, open the graphics.h file in Notepad++ (copied into the included folder in step 3). Line 302 should be changed to read, "int left=0, int top=0, int right=INT MAX, int bottom=INT MAX,"

Save the file in step ten. Done!

Note: Any C or C++ application that contains the graphics.h header file can now be built. You will still see the "fatal error: stream: no such file directory" issue even if you build C codes.

Overview

Visit link, to download WinBGIm.

Take it out to line 302 of graphics.h, open it. To make int top=0, modify int right=0.

Place the files graphics.h and winbgim.h in the compiler directory's include folder.

Add libbgi.a to your compiler directory's lib folder.

Using code: blocks access to the linker settings menu under Settings, Compiler and Debugger.

Libby.a file can be selected by clicking the Add button in the Link Libraries section.

Paste commands in the right part (i.e., other linker options).

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

Press "OK"

How to setup in Dev C++:

Download graphics.h to the Dev-C++ folders' include/ subfolder.

To use the WinBGIm subdirectory of the Dev-C++ directories, download libbgi.a to the lib/ directory.

When a programme uses the #include graphics.h> directive, the linker must be told to link in specific libraries. Alt-P is the Dev-C++ command to perform this action. Type the following into the Linker box of the Parameters tab in the pop-up window:

-lbgi\s-lgdi32\s-lcomdlg32\s-luuid\s-loleaut32\s-lole32

Programs that use the WinBGIm graphics library can now be compiled and run.

C graphics examples

1. Drawing concentric circles

1. #include <graphics.h> 
2.   int main() 
4. {  
5.    int gd = DETECT, gm; 
6.    int x = 320, y = 240, radius; 
7.   
8.    initgraph(&gd, &gm, "C:\\TC\\BGI"); 
9.   
10.    for ( radius = 25; radius <= 125 ; radius = radius + 20) 
11.       circle(x, y, radius); 
12.   
13.    getch(); 
14.    closegraph(); 
15.    return 0; 
16. }

2. C graphics program moving car

1. #include <graphics.h> 
2. #include <dos.h> 
3.   
4. int main() 
5. { 
6.    int i, j = 0, gd = DETECT, gm; 
7.   
8.    initgraph(&gd,&gm,"C:\\TC\\BGI"); 
9.   
10.    settextstyle(DEFAULT_FONT,HORIZ_DIR,2); 
11.    outtextxy(25,240," Press any key on the keyboard to view the moving car"); 
12.   
13.    getch(); 
14.   
15.    for( i = 0 ; i <= 420 ; i = i + 10, j++ ) 
16.    { 
17.       rectangle(50+i,275,150+i,400); 
18.       rectangle(150+i,350,200+i,400); 
19.       circle(75+i,410,10); 
20.       circle(175+i,410,10); 
21.       setcolor(j); 
22.       delay(100); 
23.   
24.       if( i == 420 ) 
25.          break; 
26.       if ( j == 15 ) 
27.          j = 2; 
28.   
29.       cleardevice(); // clear screen 
30.    } 
31.   
32.    getch(); 
33.    closegraph(); 
34.    return 0; 
35. }

Windows 7 or Vista graphics

Except for bar3d, which draws a three-dimensional bar, most of the functions are two-dimensional; you can also implement these functions using pre-existing algorithms. These functions can also be used in C++ programmes. These functions can be used to develop Windows 7 and Vista programmes using the Dev C++ compiler. You will need to download an additional package WinBGIm to do so.

Launch the Dev C++ compiler, navigate to Tools->Package Manager, click the install button, and browse the package location. Create a new project and choose WinBGIm. This library also includes many functions for image manipulation, such as opening image files, creating bitmaps and printing images, RGB colours, and mouse handling.

Steps:

When compiling graphics code in code Blocks IDE, an error message appears: "Cannot find graphics.h." This is because graphics.h runs do not exist in the code Blocks library folder. Set up the winBGIm library to successfully compile graphics code on code Blocks.

Uses:

The primary use of <graphics.h> library is to include and facilitate graphical operations in your program. For example, WinBGIM (Windows 7) or the graphics.h functions in C can create various shapes, show text in various fonts, alter colours, and more. You can create graphics-based projects, animations, games, and programmes using the functions in graphics.h provided by the turbo c compiler. You can draw many geometrical figures, including circles, lines, rectangles, and bars. Utilizing the offered functions, you can fill them and alter their hue. The functions of the graphics.h header files are listed below. Each function is explained together with the arguments it requires, its explanation, potential pitfalls, and an example C program.

C graphics can be used in the following:

  • Functions
  • Programs

C graphics apply to:

Programs Functions is not a typical header. At best, it needs to be updated and most frequently refers to Borland's BGI API header for DOS.

However, it is relatively straightforward; WinBGIm is a Win32 implementation of the BGI interface. The lowest-level Windows graphics interface, Win32 GDI calls, is used in its implementation. Given that the source code is available, it might be a straightforward method of learning how GDI functions.

However, WinBGIm is not at all cross-platform. Most higher-level GUI libraries, like wxWidgets and Qt, include simple graphics primitives if that is all you are after. There are suggested more straightforward libraries.


Related Topics

Top Array Keywords in C

Sorting array in C Types of array in C Array of structure in C How do you initialize an array in C Array declaration in C Array of strings in C Multidimensional array in C Reverse array...

1 minute read.

Bank management system in C

This is a mini project that is constructed completely using the C language. The bank management system is a beginner friendly project. To construct this user only needs to know...

12 minutes read.

Palindrome Number in C

What is a Palindrome? A number that doesn't change when reversed is known as a palindrome. We reverse a number and compare it to the original number to determine whether it is...

4 minutes read.

Entry Control Loop in C

Initially, an entry control loop verifies the termination condition at the entry point. After that, its control passes to the main body of the while or for loop if the...

3 minutes read.

Pascal Triangle in C

The pascal triangle in c is an array of binomial coefficients in triangular form. Here the nth row contains the binomial coefficient of ncr.In a pascal triangle, every number is...

2 minutes read.

C printf and Scanf

Input-Output functions in C Programming In C Language, the printf() and scanf() are inbuilt library functions that used for input and output. It is defined in the header file“<stdio.h>”. printf() Function: In C...

2 minutes read.

Loop Questions in C

Question 1: What is For loop syntax? Ans: The syntax that has been used for the ‘for’ loop in C programming language is: for (initialization statement /*for providing a value to variables*/; test...

19 minutes read.

Types of Pointers in C

Pointers in C A pointer is a variable and this variable contains the address of another variable, i.e it’s a variable which has the address of another variable as its value....

4 minutes read.

Random Access - Lseek in C

Introduction lseek is a system call that is used to alter the location of the read/write pointer of a file descriptor. The lseek system call basically moves the current read/write location...

4 minutes read.

Difference between Scope and Lifetime in C

In this article, we will discuss about the key differences between scope and lifetime in C, including their definitions, applications, and how they relate to one another. By the end...

8 minutes read.

Classification of Programming language

 Classification of Programming language  The programming language represents a set of instructions compiled along with each other to perform a specific task provided by the CPU (Central Processing Unit). A programming...

3 minutes read.

How to delete a file in C

A file is a group of data kept on a secondary device, such as a hard disc. It is typically utilized as a real-world application with a lot of data. These...

3 minutes read.

Math functions in C

Math functions in C: In the C programming language, the compiler allows the developer to perform mathematical operations through the header’s functions, represented by <math.h>. The <math.h> header defines various mathematical...

3 minutes read.

Data Structures And Algorithms in C

C language is one of the most flexible and simple languages. So, it is always better to learn any concept in C language compared to other languages. Let us look...

13 minutes read.

Nested Structure in C

In C language, we can create nested Structure (Structure within Structure). There are two ways to define a nested structure. By separate structure By Embedded structure   Separate structure In a separate...

1 minute read.

Built-in functions in C

The function is a set of instructions and statements enclosed in the "{}" delimiter. In c, there are two types of functions. Pre-define functions/ Built-in functionsUser define function. Built-in functions in C:- These...

8 minutes read.

Attributes in C

Attributes are one of modern C++ key features. It is the process by which initiator can add more information to the language instead of introducing new keywords for every attribute....

4 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

Builtin functions of GCC compiler

Certain built-in functions are present in the GCC compiler. These features are as follows: Function_ built in_popcount (x) The number of 1s in data of the integer type can be counted using...

3 minutes read.

Difference between rand() and srand() function in C

What is the rand()?The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.