# BGI library – Part 4 (BGI Image Handling Functions & Linear Drawing Functions)

By | July 14, 2015
In the previous article, we discussed several functions for drawing ellipses and polygons. In this article, we shall discuss the remaining BGI library functions, which include some BGI image handling functions and some functions for linear drawing.

### BGI Image Handling Functions and Linear Drawing Functions

The following functions have been discussed here :

a) linerel (int dx, int dy)
b) lineto (int x, int y)
c) moverel (int dx, int dy)
d) moveto (int x, int y)
e) imagesize (int left, int top, int right, int bottom)
f) getimage (int left, int top, int right, int bottom, void far *bitmap)
g) putimage (int left, int top, void far *bitmap, int op)

a) linerel (int dx, int dy)

This function draws a line from the cursor point to a point at a relative distance of dx along x and dy along y, from the current position.
Example :
#include <graphics.h>
#include <conio.h>
int main()
{
int gd = DETECT, gm;
initgraph (&gd, &gm, “c://turboc3//bgi”);
setcolor (GREEN);
linerel (100, 100);
linerel (300, 0);
linerel (0, 100);
linerel (-300, 0);
getch();
closegraph ();
return 0;
}
Output of linerel() Example b) lineto (int x, int y)

This function draws a line from the current position to (x, y).

Example :

#include <graphics.h>

#include <conio.h>
int main()
{
int gd = DETECT, gm;
initgraph (&gd, &gm, “c://turboc3//bgi”);
setcolor (CYAN);
lineto (100, 100);
lineto (300, 100);
lineto (300, 200);
lineto (100, 200);
getch();
closegraph ();

return 0;
}

Output of lineto() Example c) moverel (int dx, int dy)

This function moves the cursor by a relative distance of dx along x-axis and dy along y-axis.

Example :

#include <graphics.h>

#include <conio.h>
int main()
{
int gd = DETECT, gm;
initgraph (&gd, &gm, “c://turboc3//bgi”);
setcolor (WHITE);
moverel(100, 100);
lineto (300, 200);
lineto (100, 200);
lineto (100, 100);
getch();
closegraph ();

return 0;
}

Output of moverel() Example d) moveto (int x, int y)

This function moves the cursor to (x, y).

Example :

#include <graphics.h>

#include <conio.h>
int main()
{
int gd = DETECT, gm;
initgraph (&gd, &gm, “c://turboc3//bgi”);
setcolor (CYAN);
moveto(100, 100);
lineto (300, 100);
lineto (300, 200);

lineto (100, 200);

lineto (100, 100);
getch();
closegraph ();

return 0;
}

Output of moveto() Example e) imagesize (int left, int top, int right, int bottom)

This function determines the size of the memory space required to store a bitmap image.

f) getimage (int left, int top, int right, int bottom, void far *bitmap)

This function copies an image from the screen to memory. The values stored by its arguments have been listed below :

# left : x-coordinate of the top-left corner of the block.

# top : y-coordinate of the top-left corner of the block.

# right : x-coordinate of the bottom-right corner of the block.

# bottom : y-coordinate of the bottom-right corner of the block.

# bitmap : the address of the memory location where the image would be stored.

g) putimage (int left, int top, void far *bitmap, int op)

This function puts an image (that was previously saved with getimage) back to the screen, with the upper-left corner of the image placed at (left, top). The address of the memory location from where the image is to be retrieved is represented by bitmap. The last argument op specifies a combination operator that controls how the color for each destination pixel on the screen is computed.

There are five possible values for op. These are listed below : Example :

#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <malloc.h>
#include <iostream.h>
#include <stdlib.h>

void *buff;

int main()
{
int gd = DETECT, gm, errorcode;
initgraph (&gd, &gm, “c://turboc3//bgi”);
errorcode = graphresult ();
if (errorcode != grOk)
{
cout<<“Graphics error :: “<<grapherrormsg (errorcode);
cout<<“n Press any key to Exit…”;
getch();
exit(1);
}

/* setbkcolor (int color) : Sets the current background color to the color specified by color. */

setbkcolor (LIGHTCYAN);

setcolor (RED);
setfillstyle (SOLID_FILL, RED);
circle (100, 100, 50);
floodfill (100, 100, RED);
setcolor (WHITE);
outtextxy (75, 100, “MANISH”);
buff = malloc (imagesize (50, 50, 150, 150));
getimage (50, 50, 150, 150, buff);

putimage (50, 50, buff, XOR_PUT); /* Erase image */

putimage (100, 100, buff, COPY_PUT);
putimage (300, 100, buff, COPY_PUT);
putimage (500, 100, buff, COPY_PUT);
putimage (100, 300, buff, COPY_PUT);
putimage (300, 300, buff, COPY_PUT);
putimage (500, 300, buff, COPY_PUT);

getch();
closegraph();
return 0;
}

Output of putimage() Example Now, we have discussed all the functions in BGI library. Using all the functions listed in the four articles of this series, you can create high quality graphical applications of several types.

To see some examples of this, you can view the following programs created using BGI graphics :

(Visited 490 times, 1 visits today)

## 3 thoughts on “BGI library – Part 4 (BGI Image Handling Functions & Linear Drawing Functions)”

1. Windows Code Bits

BGI graphics won't work on Visual Studio. You should use Turbo C++ for using BGI.

Since you use Visual Studio, Windows GDI is what you need. We will post articles on GDI soon.

2. George

How do I use it? I use Visual Studio 2010 Express and I don't have graphics.h file.

3. Jean

This article series on BGI library functions is really great. I have been assigned a project create some cool graphics using BGI and these articles of yours helped me accomplish this difficult task. Thanks a lot 🙂