Screen capture program in DOS graphics mode

Source: Internet
Author: User

/* Screen capture program in graphic mode *
Copyright by iron wood box

**************************************** ***********************************/
# Include <stdio. h>
# Include <graphics. h>
# Include <dos. h>
# Define max_width 640
# Define max_height 480
/* Description:
1. The size (in bytes) of the image file is calculated as: biwidth * biheight * bibitcount/8
2. The size of the bitmap file is: the size of the image file + the number of bytes occupied by all information Headers
3. bfoffbits: the size of the bitmap file-the size of the image file (that is, the size of all information headers)
4. horizontal, vertical resolution, biclrused, and biclrimportant are set to 0
5. What is the color intensity in the color information table ?? (4 digits in comparison)
6. The number of bytes for each row of bitmap data (image scan rows) is biwidth * bibitcount/8.
*/
Struct bitmapfileheader/* bitmap file header */
{
Char bftype [2];/* description of the file type, each of which occupies one byte. In Windows, it is BM */
Long bfsize;/* the size of the bitmap file */
Int bfreserved1;/* Reserved bytes, all zero, two in total */
Int bfreserved2;
Long bfoffbits;/* The Byte offset between the file header and the actual image data */
} Bmpheader = {"BM", 0,0, 0,118 };

/* The bitmap information header consists of Bitmap-informationheader and color table */
Struct bitmapinfo
{
/* Bitmap header */
Long bisize;/* size of the Bitmap header, four bytes in total */
Long biwidth;/* the width and height of the image, each of which occupies four bytes */
Long biheight;
Int lanlanes;/* indicates the number of bit sides of the target device, which is 1 in total and occupies two bytes */
Int bibitcount;/* indicates the number of bits/pixels. values 1, 4, 8, 16, 24, and 32 occupy two bytes */
Long bicompression;/* indicates the compression type. There are four bytes in total. 0 indicates no compression ?? */
Long bisizeimage;/* indicates the image size, in bytes. Four bytes in total */
Long bixpelspermeter;/* indicates the horizontal resolution, expressed in pixels/meters. Four bytes in total */
Long biypelspermeter;/* indicates the vertical resolution, expressed in pixels/meters. Four bytes in total */
Long biclrused;/* Number of color indexes in the color table used by the bitmap. If the value is 0, all color palette items are used */
Long biclrimportant;/* Number of color indexes that have an important impact on image display, which is four bytes important for Table 0 */
} BMP info = {40, max_width, max_height };

/* Color information table, Set blue, green and red intensity */
Struct palette_board
{
Int rgbblue;
Int rgbgreen;
Int rgbred;
Int rgbreserved;
} Pal [16] =
{
{0,255, 0,255,255}, {128,128,255, 0}, {255,255, 0}, {, 0}, {, 0}, {, 0}, {, 0}, {, 0 },
{255,255,255, 0}, {0, 0, 0}, {128,128,128, 0}, {192,192,192, 0}, {, 0 },
{128,255,128, 0}, {255,128,255, 0}, {255,128, 255,255,136}, {, 0}
};
/* Red, blue, green, yellow, light red, green, white, black, dark gray, light gray, foreign red, light green, light foreign red, light blue, brown */

/* Compare the color information table with the color information table obtained by scanning, and return the color index number (that is, the array number )*/
Unsigned int get_index (int x, int y)
{
Unsigned int index_num;
Switch (getpixel (x, y ))
{
Case RED: index_num = 0; break;
Case Blue: index_num = 1; break;
Case Green: index_num = 2; break;
Case Yellow: index_num = 3; break;
Case lightred: index_num = 4; break;
Case Cyan: index_num = 5; break;
Case White: index_num = 6; break;
Case Black: index_num = 7; break;
Case darkgray: index_num = 8; break;
Case lightgray: index_num = 9; break;
Case magenta: index_num = 10; break;
Case lightgreen: index_num = 11; break;
Case lightmagenta: index_num = 12; break;
Case lightblue: index_num = 13; break;
Case lightcyan: index_num = 14; break;
Case BROWN: index_num = 15; break;
}
Return index_num;
}

Void scan_screen (File * FP)
{
Int I, J;
Union color/* Two dots hold one byte */
{
Unsigned char CLR;
Struct
{
Unsigned CL: 4;
Unsigned CH: 4;
} Mycolor;
} Color;
For (I = MAX_HEIGHT-1; I> = 0; I --)
For (j = 0; j <MAX_WIDTH-1; j + = 2)/* step is 2, for size of two dots is 1 byte */
{
Color. mycolor. CH = get_index (J, I);/* First dot */
Color. mycolor. Cl = get_index (J + 1, I);/* next dot */
Fwrite (& color. CLR, 1, 1, FP );
}
Sound (1500);/* Bell, tell you that print screen is over */
Delay (4000 );
Nosound ();
}

Void screen_save (char * filename)
{
File * FP;
Int I;
If (FP = fopen (filename, "WB") = NULL) Exit (0 );
/* Write bitmap file information */
Fwrite (bmpheader. bftype, 2, 1, FP );
BMP info. bisizeimage = BMP info. biwidth * BMP info. biheight * BMP info. bibitcount/8;
Bmpheader. bfsize = BMP info. bisizeimage + bmpheader. bfoffbits;
Fwrite (& bmpheader. bfsize, 4,1, FP );
Fwrite (& bmpheader. bfreserved1, 2, 1, FP );
Fwrite (& bmpheader. bfreserved2, 2, 1, FP );
Fwrite (& bmpheader. bfoffbits, 4,1, FP );
/* Write bitmap information header and color table information */
BMP info. bisize = sizeof (struct bitmapinfo );
Fwrite (& BMP info. bisize, 4,1, FP );
Fwrite (& BMP info. biwidth, 4,1, FP );
Fwrite (& BMP info. biheight, 4,1, FP );
Fwrite (& BMP info. biplanes, 2, 1, FP );
Fwrite (& BMP info. bibitcount, 2, 1, FP );
Fwrite (& BMP info. bicompression, 4,1, FP );
Fwrite (& BMP info. bisizeimage, 4,1, FP );
Fwrite (& BMP info. bixpelspermeter, 4,1, FP );
Fwrite (& BMP info. biypelspermeter, 4,1, FP );
Fwrite (& BMP info. biclrused, 4,1, FP );
Fwrite (& BMP info. biclrimportant, 4,1, FP );
/* Write color information table */
For (I = 0; I <16; I ++)
{
Fwrite (& pal. rgbblue, 1, 1, FP );
Fwrite (& pal. rgbgreen, 1, 1, FP );
Fwrite (& pal. rgbred, 1, 1, FP );
Fwrite (& pal. rgbreserved, 1, 1, FP );
}
/* Write bitmap data */
Scan_screen (FP );
Fclose (FP );
}

Void initgr (void)/* BGI graphics initialization */
{
Int GD = detect, GM = 0;
Registerbgidriver (egavga_driver );
Initgraph (& GD, & GM ,"");
}

Void main (void)
{
Initgr ();

/* Here you can add your own code to enrich the screen */
Setcolor (yellow );
Rectangle (100,100,200,150 );
Setfillstyle (1, Cyan );
Floodfill (101,101, yellow );
Setcolor (blue );
Rectangle (100,200,200,250 );
Setfillstyle (1, green );
Floodfill (101,201, blue );
Setcolor (lightgreen );
Rectangle (100,300,200,350 );
Setfillstyle (1, brown );
Floodfill (101,301, lightgreen );
Setcolor (lightred );
Outtextxy (250,400, "copyright by mabiqiang ");
/* Here you can add your own code to enrich the screen */

Screen_save ("save1.bmp ");
Getch ();
Closegraph ();
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.