📄 savebmp.h
字号:
#include <stdio.h>
#include <graphics.h>
#include <malloc.h>
#include <stdlib.h>
#include <math.h>
typedef struct tagBITMAPFILEHEADER
{
unsigned int bfType;
unsigned long bfSize;
unsigned int bfReserved1;
unsigned int bfReserved2;
unsigned long bfOffBits;
}BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER
{
unsigned long biSize;
long biWidth;
long biHeight;
unsigned int biPlanes;
unsigned int biBitCount;
unsigned long biCompression;
unsigned long biSizeImage;
long biXPelsPerMeter;
long biYPelsPerMeter;
unsigned long biClrUsed;
unsigned long biClrImportant;
}BITMAPINFOHEADER;
typedef struct tagRGBQUAD
{
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
unsigned char rgbReserved;
}RGBQUAD;
typedef struct tagBITMAPINFO
{
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[];
}BITMAPINFO;
long WidthBytes(long Width,int BitCount)
{
long WBytes;
WBytes=(Width*BitCount+31)/8;
WBytes=WBytes/4*4;
return WBytes;
}
unsigned char SetPalette(int Colors,unsigned char data)
{
switch(Colors)
{
case 16:
switch(data)
{
case 1:
return 4;
case 4:
return 1;
case 3:
return 6;
case 6:
return 3;
case 9:
return 12;
case 12:
return 9;
case 11:
return 14;
case 14:
return 11;
default:
return data;
}
case 2:
if(data==1)
return 15;
else
return 0;
}
}
int SaveScreen(char *path)
{
long i,j;
long WBytes;
long Height,Width;
FILE *fp1,*fp2;
BITMAPFILEHEADER bfh;
BITMAPINFOHEADER bih;
unsigned char dataH,dataL;
RGBQUAD quad[16];
unsigned char *LineData;
if((fp1=fopen(path,"w"))==NULL)
{
return 0;
}
if((fp2=fopen("C:\\quad.dat","w"))==NULL)
{
return 0;
}
Height=480;
Width=640;
WBytes=WidthBytes(Width,4);
bfh.bfType='M'*256+'B';
bfh.bfSize=WBytes*Height+118;
bfh.bfReserved1=0;
bfh.bfReserved2=0;
bfh.bfOffBits=118;/*40+14+16*4*/
fwrite(&bfh,sizeof(BITMAPFILEHEADER),1,fp1);
bih.biSize=40;
bih.biWidth=Width;
bih.biHeight=Height;
bih.biPlanes=1;
bih.biBitCount=4;
bih.biCompression=0;
bih.biSizeImage=Height*WBytes;
bih.biXPelsPerMeter=0;
bih.biYPelsPerMeter=0;
bih.biClrUsed=0;
bih.biClrImportant=0;
fwrite(&bih,sizeof(BITMAPINFOHEADER),1,fp1);
fread(quad,sizeof(RGBQUAD),16,fp2);
fwrite(quad,sizeof(RGBQUAD),16,fp1);
for(i=Height-1;i>=0;i--)
{
for(j=0;j<WBytes;j++)
{
dataH=getpixel(2*j+1,i+1);
dataL=getpixel(2*j+2,i+1);
SetPalette(16,dataL);
SetPalette(16,dataH);
dataL=(dataH<<4)+dataL;
fwrite(&dataL,1,1,fp1);
}
}
fclose(fp1);
fclose(fp2);
printf("success!");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -