C语言使用gd库函数详细记录

1.绘图函数

(1)绘制一个像素点的函数

  void gdImageSetPixel(gdImagePtr im, int x, int y, int color)

  参数列表

  第一参数:gdImagePtr类型,为对象

  第二参数:画图的横坐标

  第三参数:画图纵坐标

  第四参数:要绘制的颜色

(2)在两个端点之间画一条直线

   void gdImageLine(gdImagePtr im, int x1, int y1, int x2, int y2, int color)

  参数列表依次是:对象,两个端点的坐标,颜色

(3)用于绘制一个多边形的函数,至少三个

void gdImagePolygon(gdImagePtr im, gdPointPtr points, int pointsTotal, int color)

具体事例

#include<stdio.h>
#include <gdfontl.h>
#include <gd.h>
#include <string.h>

int main(int argc,char **argv)
{
gdImagePtr im;
FILE*pngout;
int black;
int white;
/* Points of polygon */
gdPoint points[3];
im = gdImageCreate(100, 100);
/* Background color (first allocated) */
black = gdImageColorAllocate(im, 0, 0, 0);
/* Allocate the color white (red, green and
blue all maximum). */
white = gdImageColorAllocate(im, 255, 255, 255);
/* Draw a triangle. */
points[0].x = 50;
points[0].y = 0;
points[1].x = 99;
points[1].y = 99;
points[2].x = 0;
points[2].y = 99;
gdImagePolygon(im, points, 3, white);
/* ... Do something with the image, such as
saving it to a file... */
/* Destroy it */
pngout = fopen("test1.png","wb");
gdImagePng(im, pngout);
fclose(pngout);
gdImageDestroy(im);

return 0;

}

 

(4) 剪切区域的函数

void gdImageSetClip(gdImagePtr im, int x1, int y1, int x2, int y2) 

参数分别为起始坐标和终止坐标,建立了剪切长方形。一旦gdImageSetClip被调用,未来所有的绘图操作仍将在指定剪裁区域,直到一个新的gdImageSetClip调用。

示例

#include<stdio.h>
#include <gdfontl.h>
#include <gd.h>
#include <string.h>

int main(int argc,char **argv)
{
gdImagePtr im;
FILE*pngout;
int black;
int white;
im = gdImageCreate(100, 100);
/* Background color (first allocated) */
black = gdImageColorAllocate(im, 0, 0, 0);
/* Allocate the color white (red, green and blue all maximum). */
white = gdImageColorAllocate(im, 255, 255, 255);
/* Set the clipping rectangle. */
gdImageSetClip(im, 25, 25, 75, 75);
/* Draw a line from the upper left corner to the lower right corner.
Only the part within the clipping rectangle will appear. */
gdImageLine(im, 0, 0, 99, 99, white);
/* ... Do something with the image, such as
saving it to a file ... */
/* Destroy it */
pngout = fopen("test1.png","wb");
gdImagePng(im, pngout);
fclose(pngout);
gdImageDestroy(im);

return 0;
}

(5)void gdImageGetClip(gdImagePtr im, int *x1P, int *y1P, int *x2P, int *y2P)

获取剪切矩形的边界

gdImagePtr im = gdImageCreateTrueColor(100, 100);
int x1, y1, x2, y2;
gdImageSetClip(im, 25, 25, 75, 75);
gdImageGetClip(im, &x1, &y1, &x2, &y2);
printf("%d %d %d %d\n", x1, y1, x2, y2);

The above code would print:

25 25 75 75
posted @ 2015-07-28 18:08  划过蓝天  阅读(754)  评论(0编辑  收藏  举报