• 当前位置:首页>>C语言>>C语言编程实例>>tc下将画图保存为图片文件
  • tc下将画图保存为图片文件
  • 这是用tc写bmp文件

    好像对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 tagB99vMAPFILEHEADER
    {
    unsigned int bfType;
    unsigned long bfSize;
    unsigned int bfReserved1;
    unsigned int bfReserved2;
    unsigned long bfOffBits;
    }B99vMAPFILEHEADER;

    typedef struct tagB99vMAPINFOHEADER
    {
    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;
    }B99vMAPINFOHEADER;

    typedef struct tagRGBQUAD
    {
    unsigned char rgbBlue;
    unsigned char rgbGreen;
    unsigned char rgbRed;
    unsigned char rgbReserved;
    }RGBQUAD;

    typedef struct tagB99vMAPINFO
    {
    B99vMAPINFOHEADER bmiHeader;
    RGBQUAD bmiColors[];
    }B99vMAPINFO;

    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;
    B99vMAPFILEHEADER bfh;
    B99vMAPINFOHEADER 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(B99vMAPFILEHEADER),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

    [1] [2] 下一页  

  • 上一篇:tc下也可以读并显示图片
    下一篇:一个产品管理系统