用于TC下的截屏代码。
使用方法:以下代码,可以保存到一个文件,例如CPYSCR.H,放到INCLUDE文件夹下。/**************************************/
/* Desc: 用于拷贝TC下的屏幕 */
/* By: hoodlum1980 */
/* Email: jinfd@ */
/* QQ: 17777538 */
/* Date: 2008-02-25 */
/**************************************/
/* Include All Needed Header*/
#if !defined(__STDIO_DEF_)
#include <stdio.h>
#endif
#ifndef ptrdiff_t
#include <mem.h>
#endif
#if !defined(__COLORS)
#include <graphics.h>
#endif
/*#define MAX_X 640;*/
/*#define MAX_Y 480;*/
void CopyScreen(char* filename,int left,int top,int right,int bottom);
/* pixel : keep channel order with readfile order */
typedef struct _PIXEL
{
unsigned char b;
unsigned char g;
unsigned char r;
} PIXEL;
/* color item in palette */
typedef struct _RGBQUAD
{
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
unsigned char rgbReserved;
} RGBQUAD;
/* bitmap file header */
typedef struct _BITMAPFILEHEADER
{
unsigned int type;
long fileSize;
long reserved;
long offbits;
} BITMAPFILEHEADER,*PBITMAPFILEHEADER;
/* bitmap info header */
typedef struct _BITMAPINFOHEADER
{
long dwSize;
long width;
long height;
int planes;
int bpp;
long compression;
long sizeImage;
long hResolution;
long vResolution;
long colors;
long importantColors;
} BITMAPINFOHEADER,*PBITMAPINFOHEADER;
/* copy the screen and save to a bmp file */
/* filename- filename of bmp file */
/* 将把屏幕截图保存为一个bpp=4的位图 */
void CopyScreen(char* filename,int left,int top,int right,int bottom)
{
int i,j,width,height,bpp=4,colorUsed=16,stride,index0,index1;
BITMAPFILEHEADER fileHeader;
BITMAPINFOHEADER infoHeader;
RGBQUAD *palette;
unsigned char *pPixels;
FILE *stream;
int COLORS[16][3]=
{
/* R G B Index ColorName */
{ 0, 0, 0}, /* 00 Black */
{ 0, 0,255}, /* 01 Blue */
{ 0,128, 0}, /* 02 Green */
{ 0,255,255}, /* 03 Cyan */
{255, 0, 0}, /* 04 Red */
{255, 0,255}, /* 05 Magenta */
{165, 42, 42}, /* 06 Brown */
{211,211,211}, /* 07 LightGray */
{169,169,169}, /* 08 DarkGray */
{173,216,230}, /* 09 LightBlue */
{144,238,144}, /* 10 LightGreen */
{144,238,238}, /* 11 LightCyan */
{238,144,144}, /* 12 LightRed */
{238,144,238}, /* 13 LightMegenta */
{255,255, 0}, /* 14 Yellow */
{255,255,255}, /* 15 White */
};
width=right-left+1;
height=bottom-top+1;
stride=(width*bpp+31)/32*4;
/* fill file header */
fileHeader.type=0x4D42; /* filetype:"BM" ascii code */
fileHeader.fileSize=sizeof(infoHeader);
fileHeader.fileSize+=sizeof(RGBQUAD)*colorUsed;
fileHeader.fileSize+=((long)stride)*height;
fileHeader.reserved=0;
fileHeader.offbits=sizeof(fileHeader)+sizeof(infoHeader)+4*16;
/* fill info header */
infoHeader.dwSize=sizeof(BITMAPINFOHEADER);
infoHeader.width=width;
infoHeader.height=height;
infoHeader.planes=1; /* must set to 1 */
infoHeader.bpp=bpp;
infoHeader.sizeImage=((long)stride)*height;
infoHeader.hResolution=0x27;
infoHeader.vResolution=0x27;
infoHeader.colors=16;
infoHeader.importantColors=0;
/* fill RGBQUAD items */
palette=malloc(sizeof(RGBQUAD)*16);
if(palette==NULL)
{
printf("malloc for palette fail!");
return;
}
for(i=0;i<16;i++)
{
palette[i].rgbRed = COLORS[i][0];
palette[i].rgbGreen = COLORS[i][1];
palette[i].rgbBlue = COLORS[i][2];
palette[i].rgbReserved = 0;
}
/* open the file in wb mode */
stream=fopen(filename,"wb");
if(stream==NULL)
{
printf("\nOpen file fail!\n");
return;
}
fwrite(&fileHeader,sizeof(fileHeader),1,stream);
fwrite(&infoHeader,sizeof(infoHeader),1,stream);
fwrite(palette,sizeof(RGBQUAD),16,stream);
free(palette);
/* write the bitmap data: color indexes ,bpp=4 */
pPixels=malloc(stride);
if(pPixels==NULL)
{
printf("\nAllocate Memory for pPixels fail!\n");
return;
}
for(j=bottom;j>=top;j--)
{
memset(pPixels,0,stride);
for(i=left;i<=right;i+=2)
{
/* 2 pixels to 1 byte */
index0=getpixel(i,j);
index1=(i==right)? 0: getpixel(i+1,j);
pPixels[i/2]=(index0<<4)+index1;
}
fwrite(pPixels,1,stride,stream);
}
fclose(stream);
}
————————————————————————————文件结束分割线————————————————————————————————
使用方法非常简单,
只有一个主要的函数,就是CopyScreen(char* file,int left,int top,int right,int bottom);
例如
#include <CPYSCR.H>
main()
{
......
CopyScreen("C:\\TC\\1.bmp",0,0,600,480);
}
以上代码把屏幕保存成为一个BMP位图,(bpp=4),即4bit/pixel。当然,这里我们使用的是默认的系统调色板颜色~~~~,如果用户改变了调色板颜色就会导致颜色不一致了。
[[it] 本帖最后由 hoodlum1980 于 2008-2-25 11:15 编辑 [/it]]