似乎对setbkcolor(...)没有作用
要改变背景,用函数 floodfill
图片要是16色,不支持真彩的。
对于2色的,读者自行修改即可。
还有问题请E-main:cangzhu@163.com
#include "stdio.h"
#include "graphics.h"
#include "alloc.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;
int xmax;
int ymax;
void InitGraph()
{
int mod=EGA;
int dr=EGAHI;
initgraph(&mod,&dr,"");
xmax=getmaxx();
ymax=getmaxy();
}
void CloseGraph()
{
closegraph();
}
void Exit(char *ErrorCode)
{
printf("%s",ErrorCode);
getch();
exit(0);
}
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;
}
}
void main()
{
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;
InitGraph();
for(i=0;i<16;i++)
{
setcolor(i);
line(10,i*20+10,xmax-10,i*20+10);
}
if((fp1=fopen("NewPic.bmp","w+"))==NULL)
{
Exit("Can Not Open The File.\n");
}
if((fp2=fopen("quad.dat","rb"))==NULL)
{
Exit("Can Not Open The File.\n");
}
Height=ymax;
Width=xmax;
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);
setbkcolor(2);
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);
}
}
getch();
CloseGraph();
}
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |