/* 图形模式下抓屏程序 *
CopyRight By 铁木箱子
***************************************************************************/
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#define MAX_WIDTH 640
#define MAX_HEIGHT 480
/*说明:
1.图象文件的大小(单位字节)计算为:biWidth*biHeight*biBitCount/8
2.位图文件的大小为:图象文件的大小+所有信息头所占的字节数
3.bfOffBits计算为:位图文件的大小-图象文件的大小(即所有信息头的大小)
4.水平、垂直分辨率、biClrUsed、biClrImportant全置为0
5.彩色信息表中的颜色强度应置为多少??(相对4位来说)
6.位图数据每一行(图象扫描行)的字节数为:biWidth*biBitCount/8
*/
struct BITMAPFILEHEADER /*位图文件头*/
{
char bfType[2]; /*文件类型说明,各占一个字节,windows系统中为BM*/
long bfSize; /*位图文件的大小*/
int bfReserved1;/*保留字节,都为零,共两个保留*/
int bfReserved2;
long bfOffBits; /*从文件头开始到实际的图象数据间的字节偏移量*/
}BMPHeader={"BM",0,0,0,118};
/*位图信息头由位图信息头(bitmap-informationheader)和彩色表(color table)组成*/
struct BITMAPINFO
{
/*位图信息头*/
long biSize; /*位图信息头的大小,共四字节*/
long biWidth; /*图象的宽度和高度,各占四个字节*/
long biHeight;
int biPlanes; /*为目标设备说明位面数,总为1,占两个字节*/
int biBitCount; /*说明比特数/象素,其值为1、4、8、16、24、32占两字节*/
long biCompression; /*说明压缩类型,共四个字节,0为没压缩??*/
long biSizeImage; /*说明图象的大小,以字节为单位,共四个字节*/
long biXPelsPerMeter; /*说明水平分辨率,用象素/米表示,共四字节*/
long biYPelsPerMeter; /*说明垂直分辨率,用象素/米表示,共四字节*/
long biClrUsed; /*位图实际使用的彩色表中的颜色索引数,为0表使用所有调色板项*/
long biClrImportant;/*对图象显示有重要影响的颜色索引数目,为0表都重要四字节*/
}BMPInfo={40,MAX_WIDTH,MAX_HEIGHT,1,4,0,0,3780,3780,0,0};
/*color information table,set blue,green and red intensity*/
struct PALETTE_BOARD
{
int rgbBlue;
int rgbGreen;
int rgbRed;
int rgbReserved;
}Pal[16]=
{
{0,0,255,0},{255,0,0,0},{0,255,0,0},{0,255,255,0},{128,128,255,0},{255,255,0,0},
{255,255,255,0},{0,0,0,0},{128,128,128,0},{192,192,192,0},{128,0,255,0},
{128,255,128,0},{255,128,255,0},{255,128,0,0},{255,255,136,0},{0,64,128,0}
};
/*依次为红,蓝,绿,黄,浅红,青,白,黑,深灰,浅灰,洋红,浅绿,浅洋红,浅蓝,浅青,棕色*/
/*根据扫描得到的颜色与彩色信息表对照,并返回颜色索引号(即数组序号)*/
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);
BMPInfo.biSizeImage=BMPInfo.biWidth*BMPInfo.biHeight*BMPInfo.biBitCount/8;
BMPHeader.bfSize=BMPInfo.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);
/*写位图信息头和色彩表信息*/
BMPInfo.biSize=sizeof(struct BITMAPINFO);
fwrite(&BMPInfo.biSize,4,1,fp);
fwrite(&BMPInfo.biWidth,4,1,fp);
fwrite(&BMPInfo.biHeight,4,1,fp);
fwrite(&BMPInfo.biPlanes,2,1,fp);
fwrite(&BMPInfo.biBitCount,2,1,fp);
fwrite(&BMPInfo.biCompression,4,1,fp);
fwrite(&BMPInfo.biSizeImage,4,1,fp);
fwrite(&BMPInfo.biXPelsPerMeter,4,1,fp);
fwrite(&BMPInfo.biYPelsPerMeter,4,1,fp);
fwrite(&BMPInfo.biClrUsed,4,1,fp);
fwrite(&BMPInfo.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);
}
/*写位图数据*/
scan_screen(fp);
fclose(fp);
}
void initgr(void) /*BGI图形初始化*/
{
int gd=DETECT,gm=0;
registerbgidriver(EGAVGA_driver);
initgraph(&gd,&gm,"");
}
void main(void)
{
initgr();
/*这里可以添加自己的代码丰富屏幕*/
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");
/*这里可以添加自己的代码丰富屏幕*/
Screen_Save("save1.bmp");
getch();
closegraph();
}