|
在屏幕中绘制一个矩形图案并画出其对角线,要求按任意键输出三个相同图案。 l 图像存储大小函数imagesize() unsigned imagesize(int xl,int yl,int x2,int y2); 作用是返回存储一块屏幕图像所需的内存大小(用字节数表示)。参数x1,y1为图像左上角坐标,参数x2,y2为图像右下角坐标。 l 保存图像函数getimage() void getimage(int xl,int yl, int x2,int y2, void *buf); 作用是保存左上角与右下角所定义的屏幕上的图像到指定的内存空间中。参数x1,y1为图像左上角坐标,参数x2,y2为图像右下角坐标,参数buf指向保存图像的内存地址。 l 输出图像函数putimage() void putimge(int x,int,y,void *buf, int op); 作用是将一个以前已经保存在内存中的图像输出到屏幕指定的位置上。数x1,y1为将要输出的图像左上角坐标,参数x2,y2为将要输出的图像右下角坐标,参数buf指向保存图像的内存地址,参数op规定如何释放内存中图像,op的值常见表:
#include <graphics.h> #include <stdlib.h> #include <conio.h> main() { int gdriver, gmode; unsigned size; void *buf; gdriver = DETECT; initgraph(&gdriver, &gmode, ""); /*图形界面初始化*/ setcolor(15); /*设置绘图颜色为白色*/ rectangle(20, 20, 200, 200); /*画正方形*/ setcolor(RED); /*设置绘图颜色为红色*/ line(20, 20, 200, 200); /*画对角线*/ setcolor(GREEN); /*设置绘图颜色为绿色*/ line(20, 200, 200, 20); outtext("press any key,you can see the same image!!"); getch(); size = imagesize(20, 20, 200, 200); /*返回这个图像存储所需字节数*/ if (size != - 1) { buf = malloc(size); /*buf指向在内存中分配的空间*/ if (buf) { getimage(20, 20, 200, 200, buf); /*保存图像到buf指向的内存空间*/ putimage(100, 100, buf, COPY_PUT); /*将保存的图像输出到指定位置*/ putimage(300, 50, buf, COPY_PUT); putimage(400, 150, buf, COPY_PUT); } } getch(); closegraph(); /*退出图形状态*/ } |