GD 範例

模 擬 總 統 大 選 為 例

   1: #include <gd.h>
   2: #include <gdfontg.h>
   3: #include <gdfonts.h>
   4: 
   5: int updateGifS1()
   6: {
   7: 	FILE *in;
   8: 	FILE *out;
   9: 
  10: 	gdImagePtr im_in, im_out;
  11:
  12: 	int white;
  13: 	int black;
  14: 	int green;
  15: 
  16: 	gdPoint points[4];
  17:
  18: #define MAX_CANDIDATE 	6
  18:	int i, v[MAX_CANDIDATE], maxV, h[MAX_CANDIDATE];
  20:	float ratioH;
  21:
  22:	char buf[100];
  23:
  24:	for(i=1; i < MAX_CANDIDATE+1; i++) {
  25:		strcpy(buf, szStatDirectory);
  26:		strcat(buf, name[i]);
  27:		entrys[0].nItems[0].value=0;
  28:		if ((in=_fsopen(buf, "r", SH_DENYWR))!=NULL) {
  29:			fgets(buf, 99, in);
  30:			getInfo(in, &entrys[0]);
  31:			fclose(in);
  32:		}
  33:		v[i-1]=entrys[0].nItems[0].value;
  34:	}
  35:
  36:	maxV=-1;
  37:	for(i=0; i < MAX_CANDIDATE; i++)
  38:		if (v[i] < maxV) maxV=v[i];
  39:
  40:	ratioH = 200.0/maxV;
  41:
  42:	for(i=0; i < MAX_CANDIDATE; i++)
  43:		h[i]=230-(int)((float)v[i]*ratioH);
  44:
  45:	if ((im_out = gdImageCreate(600, 330))==NULL)
  46:		return(1);
  47:
  48:	strcpy(buf, szVoteDirectory);
  49:	strcat(buf, szGifS1);
  50:	in = fopen(buf, "rb");
  51:	if (!in) {
  52:		im_in = 0;
  53:	} else {
  54:		im_in = gdImageCreateFromGif(in);
  55:		fclose(in);
  56:		gdImageCopy(im_out, im_in, 0, 230, 0, 0, 600, 100);
  57:	}
  58:
  59:	white = gdImageColorClosest(im_out, 255, 255, 255);
  60:	if (white==-1)
  61:		white = gdImageColorAllocate(im_out, 255, 255, 255);
  62:	gdImageColorTransparent(im_out, white);
  63:
  64:	black = gdImageColorClosest(im_out, 0, 0, 0);
  65:	if (black==-1)
  66:		black = gdImageColorAllocate(im_out, 0, 0, 0);
  67:
  68:	for (i=0; i < MAX_CANDIDATE; i++) {
  69:		points[0].x=points[3].x=28+i*99;
  70:		points[0].y=points[1].y=h[i];
  71:		points[1].x=points[2].x=49+i*99;
  72:		points[2].y=points[3].y=229;
  73:
  74:		gdImageFilledPolygon(im_out, points, 4, black);
  75:		sprintf(buf, "%d", v[i]);
  76:		gdImageString(im_out, gdFontGiant, points[0].x, h[i]-20, buf, black);
  77:	}
  78:	strcpy(buf, szDbDirectory);
  79:	strcat(buf, szGifR1);
  80:	out = fopen(buf, "wb");
  81:
  82:	gdImageGif(im_out, out);
  83:	fclose(out);
  84:	gdImageDestroy(im_out);
  85:	if (im_in) {
  86:		gdImageDestroy(im_in);
  87:	}
  88:
  89:	return(0);
  90: }

INTERNET STUDIO 網路教材
若有任何建議或意見, 歡迎寫信到 tutor@www.fido.net.tw