资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

c语言怎么获取像素点函数 c语言像素画

C语言打开图像文件后读取像素

C语言打开图像文件后运用以下代码就可以读取像素,具体如下:

10余年的儋州网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。营销型网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整儋州建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“儋州网站设计”,“儋州网站推广”以来,每个客户项目都认真落实执行。

#ifndef IMAGE_H

#define IMAGE_H

void image_info(FILE* file);

void image_save(FILE *file);

void image_gray();

void image_binarization();

void image_opposite();

void image_channel(); //抽取RGB通道

void image_bright();//改变图像亮度

typedef struct BMP

{

//14字节

unsigned short bfType; //文件标识 2字节 必须为BM

unsigned int bfSize; //文件大小 4字节

unsigned short bfReserved1; //保留,每字节以"00"填写 2字节

unsigned short bfReserved2; //同上 2字节

unsigned int bfOffBits; //记录图像数据区的起始位置(图象数据相对于文件头字节的偏移量)。 4字节

//40字节

unsigned int biSize; //表示本结构的大小 4字节

int biWidth; //位图的宽度 4字节

int biHeight; //位图的高度 4字节

unsigned short biPlanes; //永远为1 , 2字节

unsigned short biBitCount; //位图的位数 分为1 4 8 16 24 32 2字节

unsigned int biCompression; //压缩说明 4字节

unsigned int biSizeImage; //表示位图数据区域的大小以字节为单位 4字节

int biXPelsPerMeter; //用象素/米表示的水平分辨率 4字节

int biYPelsPerMeter; //用象素/米表示的垂直分辨率 4字节

unsigned int biClrUsed; //位图使用的颜色索引数 4字节

unsigned int biClrImportant; //对图象显示有重要影响的颜色索引的数目 4字节

} BMP;

int line_byte;

unsigned char *imagedata;

extern BMP bmp;

extern int line_byte;

extern unsigned char *imagedata;

#endif

//image_rw.c文件

#includestdio.h

#includestdlib.h

#include"image.h"

void image_info(FILE *file)

{

int times=3; //输入文件名次数。

char bmp_name[10]; //文件名

printf("\nplease enter a file name for reading:");

do

{

if (times3)

{

printf("\nplease enter a file name for reading again:");

}

fflush(stdin);

gets(bmp_name);

//printf("\n%s",bmp_name);

file=fopen(bmp_name,"rb+"); //打开一个文件进行读写操作。

--times;

if (file==NULL)

{

printf("\nerror opening %s for reading! ",bmp_name);

}

else

{

break;

}

}

while(times!=0);

if (times==0)

{

printf("\nsorry, shutdown!");

exit(1);

}

//读取图像信息

fseek(file,0L,0); //读取图像文件类型

fread(bmp,sizeof(BMP),1,file);

printf("\n bmp tpye: %u",bmp.bfType);

printf("\n bmp size: %u",bmp.bfSize);

printf("\n bmp reserved1: %u",bmp.bfReserved1);

printf("\n bmp reserved2: %u",bmp.bfReserved2);

printf("\n bmp offBits: %u",bmp.bfOffBits);

printf("\n bmp bisize: %u",bmp.biSize);

printf("\n bmp biWidth: %d",bmp.biWidth);

printf("\n bmp biHeight: %d",bmp.biHeight);

printf("\n bmp biplans: %u",bmp.biPlanes);

printf("\n bmp biBitCount: %u",bmp.biBitCount);

printf("\n bmp biCompression: %u",bmp.biCompression);

printf("\n bmp biSizeImage: %u",bmp.biSizeImage);

printf("\n bmp biXPelsPerMeter: %d",bmp.biXPelsPerMeter);

printf("\n bmp biYPelsPerMeter: %d",bmp.biYPelsPerMeter);

printf("\n bmp biClrUsed: %u",bmp.biClrUsed);

printf("\n bmp biClrImportant: %u\n",bmp.biClrImportant);

line_byte=(bmp.biWidth*bmp.biBitCount/8+3)/4*4; //获得图像数据每行的数据个数

//printf("dfsa%u",bmp.line_byte);

//bmp.imagedata=NULL;

imagedata=(unsigned char*)malloc(bmp.biSizeImage);

fseek(file,(long)bmp.bfOffBits,0);

fread(imagedata,sizeof(unsigned char),bmp.biSizeImage,file);

fclose(file);

}

//保存图像

void image_save(FILE *file)

{

int times=3; //输入文件名次数。

char bmp_name[10]; //文件名

//int i; //记录数据区个数

printf("\nplease enter a file name for writeing:");

do

{

if (times3)

{

printf("\nplease enter a file name for writeing again:");

}

fflush(stdin);

gets(bmp_name);

printf("\n%s",bmp_name);

file=fopen(bmp_name,"wb+"); //打开一个文件进行读写操作。

--times;

if (file==NULL)

{

printf("\nerror opening %s for writing",bmp_name);

}

else

{

break;

}

}

while(times!=0);

if (times==0)

{

printf("\nsorry, shutdown!");

exit(1);

}

//写文件头

printf("\n%s",bmp_name);

fseek(file,0L,0); //图像文件类型

fwrite((bmp.bfType),sizeof(short),1,file);

printf("\n bmp tpye: %d",bmp.bfType);

fseek(file,2L,0); //图像文件大小

fwrite((bmp.bfSize),sizeof(int),1,file);

printf("\n bmp size: %d",bmp.bfSize);

fseek(file,6L,0); //图像文件保留字1

fwrite((bmp.bfReserved1),sizeof(short),1,file);

printf("\n bmp reserved1: %d",bmp.bfReserved1);

fseek(file,8L,0); //图像文件保留字2

fwrite((bmp.bfReserved2),sizeof(short),1,file);

printf("\n bmp reserved2: %d",bmp.bfReserved2);

fseek(file,10L,0);//数据区的偏移量

fwrite((bmp.bfOffBits),sizeof(short),1,file);

printf("\n bmp offBits: %d",bmp.bfOffBits);

fseek(file,14L,0);//文件头结构大小

fwrite((bmp.biSize),sizeof(int),1,file);

printf("\n bmp bisize: %d",bmp.biSize);

fseek(file,18L,0);//图像的宽度

fwrite((bmp.biWidth),sizeof(int),1,file);

printf("\n bmp biWidth: %d",bmp.biWidth);

fseek(file,22L,0);//图像的高度

fwrite((bmp.biHeight),sizeof(int),1,file);

printf("\n bmp biHeight: %d",bmp.biHeight);

fseek(file,24L,0);//图像的面数

fwrite((bmp.biPlanes),sizeof(short),1,file);

printf("\n bmp biplans: %d",bmp.biPlanes);

fseek(file,28L,0);//图像一个像素的字节数

fwrite((bmp.biBitCount),sizeof(short),1,file);

printf("\n bmp biBitCount: %d",bmp.biBitCount);

fseek(file,30L,0);//图像压缩信息

fwrite((bmp.biCompression),sizeof(short),1,file);

printf("\n bmp biCompression: %d",bmp.biCompression);

fseek(file,34L,0);//图像数据区的大小

fwrite((bmp.biSizeImage),sizeof(int),1,file);

printf("\n bmp biSizeImage: %d",bmp.biSizeImage);

fseek(file,38L,0);//水平分辨率

fwrite((bmp.biXPelsPerMeter),sizeof(int),1,file);

printf("\n bmp biXPelsPerMeter: %d",bmp.biXPelsPerMeter);

fseek(file,42L,0);//垂直分辨率

fwrite((bmp.biYPelsPerMeter),sizeof(int),1,file);

printf("\n bmp biYPelsPerMeter: %d",bmp.biYPelsPerMeter);

fseek(file,46L,0);//颜色索引数

fwrite((bmp.biClrUsed),sizeof(int),1,file);

printf("\n bmp biClrUsed: %d",bmp.biClrUsed);

fseek(file,50L,0);//重要颜色索引数

fwrite((bmp.biClrImportant),sizeof(int),1,file);

printf("\n bmp biClrImportant: %d\n",bmp.biClrImportant);

fseek(file,(long)(bmp.bfOffBits),0);

fwrite(imagedata,sizeof(unsigned char),bmp.biSizeImage,file);

fclose(file);

}

//pixProcess.c文件

#includestdio.h

#includestdlib.h

#includemath.h

#include"image.h"

//灰度化

void image_gray()

{

int i,j;

unsigned char tmp;

for (i=0;ibmp.biHeight;i++)

{

for (j=0;jline_byte/3;j++)

{

tmp=0.11*(*(imagedata+i*line_byte+j*3+0))+0.59*(*(imagedata+i*line_byte+j*3+1))+0.3*(*(imagedata+i*line_byte+j*3+2));

imagedata[i*line_byte+j*3+0]=tmp;

imagedata[i*line_byte+j*3+1]=tmp;

imagedata[i*line_byte+j*3+2]=tmp;

//printf("\nnidsfh%d %d",i,j);

}

}

}

//二值化

void image_binarization()

{

int i,j;

for (i=0;ibmp.biHeight;i++)

{

for (j=0;jline_byte;j++)

{

if ((*(imagedata+i*line_byte+j))128)

{

imagedata[i*line_byte+j]=0;

}

else

{

imagedata[i*line_byte+j]=255;

}

}

}

}

void image_opposite() //反相

{

int i,j;

for (i=0;ibmp.biHeight;i++)

{

for (j=0;jline_byte;j++)

{

imagedata[i*line_byte+j]=abs(255-imagedata[i*line_byte+j]);

}

}

}

void image_channel() //抽取RGB通道

{

int i,j;

char rgb;

printf("\nplease enter a char(r/g/b): ");

fflush(stdin);

scanf("%c",rgb);

if (rgb=='b')

{

for (i=0;ibmp.biHeight;i++)

{

for (j=0;jline_byte/3;j++)

{

imagedata[i*line_byte+3*j+1]=0;

imagedata[i*line_byte+3*j+2]=0;

}

}

}

else if(rgb=='g')

{

for (i=0;ibmp.biHeight;i++)

{

for (j=0;jline_byte/3;j++)

{

imagedata[i*line_byte+3*j]=0;

imagedata[i*line_byte+3*j+2]=0;

}

}

}

else

{

for (i=0;ibmp.biHeight;i++)

{

for (j=0;jline_byte/3;j++)

{

imagedata[i*line_byte+3*j]=0;

imagedata[i*line_byte+3*j+1]=0;

}

}

}

}

void image_bright()//改变图像亮度

{

int level;

int i,j;

printf("\n please enter the level of brightness[-255 to 255] :");

fflush(stdin);

scanf("%d",level);

for (i=0;ibmp.biHeight;i++)

{

for (j=0;jline_byte;j++)

{

if (level=0)

{

if ((imagedata[i*line_byte+j]+level)255)

imagedata[i*line_byte+j]=255;

else

imagedata[i*line_byte+j]+=level;

}

else

{

if ((imagedata[i*line_byte+j]-abs(level))0)

imagedata[i*line_byte+j]=0;

else

imagedata[i*line_byte+j]+=level;

}

}

}

}

//void image_create() //创建一幅24位BMP图像文件。

//{

//main.c文件

#includestdio.h

#includestdlib.h

#includestring.h

#includeconio.h

#include"image.h"

BMP bmp;

int main()

{

FILE *file=NULL;

int choose;

char gono;

do

{

image_info(file); //imagedata已经分配了动态内存,但是没有释放

printf("\n 1.image_opposite");

printf("\n 2.image_gray");

printf("\n 3.image_binarization");

printf("\n 4.image_channel");

printf("\n 5.image_brightness");

//printf("6.image_opposite");

//printf("7.image_opposite");

printf("\nchoose your options:");

fflush(stdin);

scanf("%d",choose);

switch(choose)

{

case 1:

image_opposite();

image_save(file);

free(imagedata);

break;

case 2:

image_gray();

image_save(file);

free(imagedata);

break;

case 3:

image_binarization();

image_save(file);

free(imagedata);

break;

case 4:

image_channel();

image_save(file);

free(imagedata);

break;

case 5:

image_bright();

image_save(file);

free(imagedata);

break;

default:

printf("\n wrong choose!");

}

printf("\nlet's go on?(y/n):");

fflush(stdin);

scanf("%c",gono);

if (gono=='n')

{

printf("\nbye bye!");

break;

}

}

while(1);

return 0;

}

c语言的图形函数库有哪些

图形和图像函数包含在graphics.h里面(一) 像素函数56. putpiel() 画像素点函数57. getpixel()返回像素色函数(二) 直线和线型函数58. line() 画线函数59. lineto() 画线函数60. linerel() 相对画线函数61. setlinestyle() 设置线型函数62. getlinesettings() 获取线型设置函数63. setwritemode() 设置画线模式函数(三)、多边形函数64. rectangle() 画矩形函数65. bar() 画条函数66. bar3d() 画条块函数67. drawpoly() 画多边形函数(四)、 圆、弧和曲线函数68. getaspectratio()获取纵横比函数69. circle()画圆函数70. arc() 画圆弧函数71. ellipse()画椭圆弧函数72. fillellipse() 画椭圆区函数73. pieslice() 画扇区函数74. sector() 画椭圆扇区函数75. getarccoords()获取圆弧坐标函数(五)、 填充函数76. setfillstyle() 设置填充图样和颜色函数77. setfillpattern() 设置用户图样函数78. floodfill() 填充闭域函数79. fillpoly() 填充多边形函数80. getfillsettings() 获取填充设置函数81. getfillpattern() 获取用户图样设置函数(六)、图像函数82. imagesize() 图像存储大小函数83. getimage() 保存图像函数84. putimage() 输出图像函数四、图形和图像函数对许多图形应用程序,直线和曲线是非常有用的。但对有些图形只能靠操作单个像素才能画出。当然如果没有画像素的功能,就无法操作直线和曲线的函数。而且通过大规模使用像素功能,整个图形就可以保存、写、擦除和与屏幕上的原有图形进行叠加。(一) 像素函数56. putpixel() 画像素点函数功能: 函数putpixel() 在图形模式下屏幕上画一个像素点。用法: 函数调用方式为void putpixel(int x,int y,int color);说明: 参数x,y为像素点的坐标,color是该像素点的颜色,它可以是颜色符号名,也可以是整型色彩值。此函数相应的头文件是graphics.h返回值: 无例: 在屏幕上(6,8)处画一个红色像素点:putpixel(6,8,RED);57. getpixel()返回像素色函数功能: 函数getpixel()返回像素点颜色值。用法: 该函数调用方式为int getpixel(int x,int y);说明: 参数x,y为像素点坐标。函数的返回值可以不反映实际彩色值,这取决于调色板的设置情况(参见setpalette()函数)。这个函数相应的头文件为graphics.h返回值: 返回一个像素点色彩值。例: 把屏幕上(8,6)点的像素颜色值赋给变量color。color=getpixel(8,6);

如何用C语言(C++)读取位图的像素点RGB信息

pData里面保存的就是一个一个的COLORREF结构,你只需要通过BITMAPINFOHEADER中的宽高等信息,计算位移,就可以读取某个点的RGB值了。

还有一个简单的办法,你之前已经有memBitmap这个CBitmap了,通过这个做更方便。通过SelectObject将memBitmap放到一个CDC中,直接使用函数GetPixel函数就可以获取指定某个点的RGB值了,这个不需要计算和位移。

C/C++中显示像素点的函数

SetPixel

函数原型:COLORREF

SetPixel(HDC

hdc,

int

X,

int

Y,

COLORREF

crColor);

参数:

hdc:设备环境句柄。

X:指定要设置的点的X轴坐标,按逻辑单位表示坐标。

Y:指定要设置的点的Y轴坐标,按逻辑单位表示坐标。

crColor:指定要用来绘制该点的颜色。

头文件:wingdi.h:

库文件:gdi32.lib


网页题目:c语言怎么获取像素点函数 c语言像素画
链接分享:http://cdkjz.cn/article/dohdohd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220