×

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

Snake Game in C

Snake game in C is basic desktop console program which was created in 1970’s. It is an old classic game which was played by every child across the world. This...

4 minutes read.

Actual and Formal Parameters

Any variable declared within the parenthesis is referred to as the parameters during the function declaration. Parameters tell the function about the argument datatype, their order, and the number of...

5 minutes read.

Binomial Coefficient Program in C

What is Binomial coefficient? In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k...

3 minutes read.

Memory layout in C

Memory layout in C The C language is designed so that it becomes easier for a programmer to decide the amount of memory they want to use in a program. C program...

4 minutes read.

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

4 minutes read.

Multiplication Table in C

Displaying table up to 10 #include <stdio.h> int main() {     int num, i = 1;     printf("     Enter any Number:");     scanf("%d", &num);     printf("Multiplication table of %d: ", num);  ...

1 minute read.

Length of an Array Function in C

In C, there is no built-in function to get the length of an array. However, there are a few ways you can determine the length of an array. It is necessary...

15 minutes read.

Bit Stuffing Program in C

What is a Bit Stuffing? The process of inserting one or more extra bits (0) into data is known as bit stuffing. To provide signalling information to a recipient, these are...

3 minutes read.

C/C++ Program to Find the Size of int, float, double and char

In this tutorial, we will learn how to use the sizeof operator to determine the size of each variable. Program to Determine Variable Size Write a C or C++ program to determine the...

2 minutes read.

How to initialize array to zero in C

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

Sizeof in C

A pointer in C is defined as a variable that stores the information of the address of another variable. A pointer can be incremented or decremented, which means we can...

4 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it...

3 minutes read.

Memory leak in C

What is memory leak in C? Memory leak occurs when we keep allocating memory in the heap without freeing it, i.e., the allocated memory in heap is not released back to...

3 minutes read.

Difference between Array and List in C

C# Array vs List is where the abstraction and implementation of human computing meet.  Arrays are incredibly related to the hardware concept of contiguous and contiguous memory, where each part is...

3 minutes read.

tolower() Function in C

The tolower() capability is characterized in the ctype.h header document. In the event that the person passed is a capitalized letter set, the tolower() capability switches capitalized letters in order...

3 minutes read.

How to use atoi() function in C

Introduction: The atoi() is a function used in C programming language. This function converts string characters to the integer value. The atoi() function converts the input string to the return type of...

4 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.

Flexible Array Members in a Structure in C

There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are...

4 minutes read.

Simple hash() function in C

Introduction In this context, we briefly discuss HASH FUNCTION, HASHING or HASH TABLE in C. It is a function used to map data and mapped arbitrary sizes to the fixed-size values. The...

7 minutes read.

fopen() function in C

fopen() function, is one of the file handling functions. It is used to open the existing file and perform operations on the file. If the file does not exist, it...

3 minutes read.