Fill color using putpixel() function of graphics.h in C

In this article, we are going to learn about the putpixel() function of graphics.h header file in C programming language, and use it to filling color inside different areas.
Submitted by Manu Jemini, on March 24, 2018

To put a pixel on the screen at a particular position, calling the putixel() function is a good way. This function takes three parameters as the position of the pixel and also the color of the pixel.

In the example below, we implemented three for loops which will make three squares of different colors, because we have decided the boundaries of the squares as lower and upper limit of the loop.

Every loop has different upper limit and lower limit, with different pixel colors. So, it will look like three different squares of different color, even though these are just pixels.

If we change the limits of loops the squares will also change and we can also change the color of the pixel and color of the square will change.

graphics.h - putpixel() function in C

#include <graphics.h>
#include <conio.h>

int main() 
{
	//initilizing graphic driver and 
	//graphic mode variable
	int graphicdriver=DETECT,graphicmode;

	//calling initgraph
	initgraph(&graphicdriver,&graphicmode,"c:\\turboc3\\bgi");

	//Printing message for user
	outtextxy(20, 20 + 20, "Program to fill color in different areas using putpixel in C graphics");

	//initilizing the type of variables

	int i,j;

	//loop for first area
	for(i=60;i<=120;i++)
	{
		for(j=60;j<=120;j++)
		{
			putpixel(j, i, RED);
		}
	}

	//loop of second area
	for(i=121;i<=180;i++)
	{
		for(j=121;j<=180;j++)
		{
			putpixel(j, i, GREEN);
		}
	}

	//loop of third area
	for(i=181;i<=240;i++)
	{
		for(j=181;j<=240;j++)
		{
			putpixel(j, i, BLUE);
		}
	}

	getch();

	return 0;
}

Output

graphics.h - putpixel() function  in C



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.