← Back to C Course | Chapter 13: Graphics | Lesson 2 of 10

C की graphics.h Library

graphics.h एक पुरानी library है जो एक C program को एक drawing window खोलने और इस पर draw करने देती है, एक starter art kit की तरह। आप इसे start करते हैं, draw करते हैं, और फिर बंद कर देते हैं।
Syntax
c
#include <graphics.h>

int gd = DETECT, gm;
initgraph(&gd, &gm, "");   // enter graphics mode
// ... drawing calls ...
getch();
closegraph();              // leave graphics mode

graphics.h Header

graphics.h header file 70 से ज़्यादा unique drawing और utility functions access करने के लिए एक gatekeeper की तरह काम करता है। यह आपके program coordinates को BGI drivers इस्तेमाल करके सीधे video controller memory में map करता है।

उदाहरण: The graphics.h Header

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	circle(100, 100, 50);
	getch();
	closegraph();
	return 0;
}

initgraph और closegraph

initgraph() function graphics driver load करता है और system को graphics mode में डालता है। closegraph() function graphics system shut down करता है, सारी allocated memory free करते हुए और text interface वापस load करते हुए।

उदाहरण: initgraph and closegraph

c
#include <graphics.h>
// Program execution starts in main()
int main() {
	// Declare gd and set it to DETECT, gm
	int gd = DETECT, gm;
	initgraph(&gd, &gm, "");
	getch();
	closegraph();
	// Return 0 to signal that the program finished successfully
	return 0;
}

getmaxx और getmaxy

Screen sizes platforms के बीच अलग होते हैं। positions hardcode करने की बजाय, maximum resolution dynamically ढूंढने के लिए getmaxx() और getmaxy() इस्तेमाल करें। यह आपके designs को responsive रखता है।

उदाहरण: getmaxx and getmaxy

c
#include <graphics.h>
// Program execution starts in main()
int main() {
	initgraph(0, 0, "");
	// Declare centerX and set it to getmaxx() / 2
	int centerX = getmaxx() / 2;
	// Declare centerY and set it to getmaxy() / 2
	int centerY = getmaxy() / 2;
	putpixel(centerX, centerY, WHITE);
	getch();
	closegraph();
	// Return 0 to signal that the program finished successfully
	return 0;
}

cleardevice और Background Options

cleardevice() function current graphics screen मिटाता है, इसे background color से भरते हुए। आप setbkcolor() function इस्तेमाल करके default background color set कर सकते हैं।

उदाहरण: cleardevice and Background Options

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	setbkcolor(BLUE);
	cleardevice();
	getch();
	closegraph();
	return 0;
}

Modern Compatibility Details

Standard C compiler packages (जैसे Windows या Linux पर GCC) out of the box graphics.h include नहीं करते। Modern setups custom BGI wrapper libraries इस्तेमाल करते हैं, जो same standard function signatures इस्तेमाल करती हैं।

उदाहरण: Modern Compatibility Details

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	getch();
	closegraph();
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. initgraph call करने से पहले draw करना, ताकि कुछ display न हो।
  2. initgraph को गलत BGI driver path pass करना, ताकि graphics mode initialize करने में यह fail हो जाए।
  3. closegraph() से पहले getch() भूल जाना, ताकि window तुरंत बंद हो जाए।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.