Getpixel() Function

getpixel() function is used to get the color of pixel present at any specified location (x,y).

Declaration

int getpixel (int x, int y);
  • It contains two arguments, first argument represents the X-coordinate and second argument represents the Y-coordinate.
  • For better understanding go through the program:

Program

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

{

int gd=DETECT, gm,col;

char a[50];

initgraph(&gd,&gm," ");

putpixel(320,240,RED);

col=getpixel(320,240);

sprintf(a,"color of pixel at location (320,240) is %d",y);

outtextxy(30,100,a);

getch();

closegraph();

}

The above program firstly plots the pixel of red color on the location (320,240). Now for getting the number associated with the color of pixel we used getpixel() function in the program. getpixel() includes two arguments 320 (X-coordinate) and 240 (Y-coordinate) which shows the position of the red color pixel. getpixel() function stores the number associated with red color in the integer type variable col then sprintf() and outtextxy() function together print the "color of pixel at location (320,240) is 4"at location (30,100).

Output

c graphics