怎样用C语言将png图像读入数组并显示?

Python015

怎样用C语言将png图像读入数组并显示?,第1张

c语言读取图片原理:通过文件流的方式读入到Byte的二进制数组中,之后,使用图像分析算法将图像显示到屏幕上,要将数组中的值转换为像素。

参考代码如下:

//function definition

void ImageRead(AnsiString name,int &width,int &height,int *r,int *g,int *b)

{

//read image

FILE *fp

if((fp=fopen(name.c_str(),"rb"))==NULL) {

printf("cannot open bmp.name\n")

return

}

fread(&bfType,sizeof(WORD),1,fp)

if(bfType!=0x4d42) {//该值必需是0x4D42,也就是字符'BM'

printf("the input map is not bmp type")

return

}

fread(&bfSize,sizeof(DWORD),1,fp)

fread(&bfReserved1,sizeof(WORD),1,fp)

fread(&bfReserved2,sizeof(WORD),1,fp)

fread(&bfOffBits,sizeof(DWORD),1,fp)

fread(&bih,sizeof(BITMAPINFOHEADER),1,fp)

width=bih.biWidth

#include <graphics.h>

#include <stdio.h>

int main()

{

int driver=0,mode=0

initgraph(&driver,&mode,"")

IMAGE img_1

loadimage(&img_1, _T("1.jpg"))

putimage(20, 20,&img_1)

getchar()

closegraph()

}