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

转载于:https://www.cnblogs.com/Leotu/p/4683952.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值