c语言读取png

如何在c语言程序中导入png图片

#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();

}

c语言读取png

大侠,可以告诉我怎么用C语言以2进制读取png图片的宽和高吗?谢了!!!

添加62字节的头信息就可以了!

第一部分为位图文件头BITMAPFILEHEADER,是一个结构,其定义如下:

typedefstructtagBITMAPFILEHEADER{

WORD bfType;

DWORD bfSize;

WORD bfReserved1;

WORD bfReserved2;

DWORD bfOffBits;

} BITMAPFILEHEADER;

这个结构的长度是固定的,为14个字节(WORD为无符号16位整数,DWORD为无符号32位整数),各个域的说明如下:

bfType

指定文件类型,必须是0x424D,即字符串 “BM “,也就是说所有.bmp文件的头两个字节都是 “BM “

bfSize

指定文件大小,包括这14个字节

bfReserved1,bfReserved2

为保留字,不用考虑

bfOffBits

为从文件头到实际的位图数据的偏移字节数,即图3中前三个部分的长度之和。

第二部分为位图信息头BITMAPINFOHEADER,也是一个结构,其定义如下:

typedef struct tagBITMAPINFOHEADER{

DWORD biSize;

LONG biWidth;

LONG biHeight;

WORD biPlanes;

WORD biBitCount

DWORD biCompression;

DWORD biSizeImage;

LONG biXPelsPerMeter;

LONG biYPelsPerMeter;

DWORD biClrUsed;

DWORD biClrImportant;

} BITMAPINFOHEADER;

这个结构的长度是固定的,为40个字节(WORD为无符号16位整数,DWORD无符

号32位整数,LONG为32位整数),各个域的说明如下:

biSize

指定这个结构的长度,为40

biWidth

指定图象的宽度,单位是象素

biHeight

指定图象的高度,单位是象素

biPlanes

必须是1,不用考虑

biBitCount

指定表示颜色时要用到的位数,常用的值为1(黑白二色图),4(16色图),8(256色),24(真彩色图)(新的.bmp格式支持32位色,这里就不做讨论了)。

biCompression

指定位图是否压缩,有效的值为BI_RGB,BI_RLE8,BI_RLE4,BI_BITFIELDS(都是一些Windows定义好的常量)。要说明的是,Windows位图可以采用RLE4,和RLE8的压缩格式,但用的不多。我们今后所讨论的只有第一种不压缩的情况,即biCompression 为BI_RGB的情况。

biSizeImage

指定实际的位图数据占用的字节数,其实也可以从以下的公式中计算出来:

biSizeImage=biWidth ‘*biHeight

要注意的是:上述公式中的biWidth ‘必须是4的整倍数(所以不是biWidth,而是biWidth ‘,表示大于或等于biWidth的,离4最近的整倍数。举个例子,如果biWidth=240,则biWidth ‘=240;如果biWidth=241,biWidth ‘=244)如果biCompression为BI_RGB,则该项可能为零

biXPelsPerMeter

指定目标设备的水平分辨率,单位是每米的象素个数,关于分辨率的概念,我们将在打印部分详细介绍。

biYPelsPerMeter

指定目标设备的垂直分辨率,单位同上。

biClrUsed

指定本图象实际用到的颜色数,如果该值为零,则用到的颜色数为2的biBitCount次方。

biClrImportant

指定本图象中重要的颜色数,如果该值为零,则认为所有的颜色都是重要的。

第三部分为调色板(Palette),当然,这里是对那些需要调色板的位图文件而言的。有些位图,如真彩色图,前面已经讲过,是不需要调色板的,BITMAPINFOHEADER后直接是位图数据。

调色板实际上是一个数组,共有biClrUsed个元素(如果该值为零,则有2的biBitCount次方个元素)。数组中每个元素的类型是一个RGBQUAD结构,占4个字节,其定义如下:

typedef struct tagRGBQUAD{

BYTE rgbBlue; //该颜色的蓝色分量

BYTE rgbGreen; //该颜色的绿色分量

BYTE rgbRed; //该颜色的红色分量

BYTE rgbReserved; //保留值

} RGBQUAD;

第四部分就是实际的图象数据了。对于用到调色板的位图,图象数据就是该像素颜在调色板中的索引值,对于真彩色图,图象数据就是实际的R,G,B值。下面就2色,16色,256色位图和真彩色位图分别介绍。

对于2色位图,用1位就可以表示该像素的颜色(一般0表示黑,1表示白),所以一个字节可以表示8个像素。

对于16色位图,用4位可以表示一个像素的颜色,所以一个字节可以表示2个像素。

对于256色位图,一个字节刚好可以表示1个像素。

对于真彩色图,三个字节才能表示1个像素,哇噻,好费空间呀,没办法,谁叫你想让图的颜色显得更亮丽呢,有得必有失嘛。

要注意两点:

1.每一行的字节数必须是4的整倍数,如果不是,则需要补齐。这在前面介绍biSizeImage时已经提到了。

2.一般来说,.BMP文件的数据从下到上,从左到右的。也就是说,从文件中最先读到的是图象最下面一行的左边第一个像素,然后是左边第二个像素…接下来是倒数第二行左边第一个像素,左边第二个像素…依次类推,最后得到的是最上面一行的最右一个像素。

了解这些以后呢 可以通过以二进制的方式“rb”打开你想读取的png图片

FILE *fp

if(!(fp=open(“filename”,rb)))

printf(“open file error”);

打开成功后就可以通过文件指针读取文件内容了。

c/c++/java/c#读取png图片

给你一个java的,自己改改

import java.awt.*;

import java.awt.image.*;

import java.util.Random;

import java.io.*;

import javax.swing.*;

public class LoadImage {

/**

* @param args

*/

public static void main(String[] args) {

String myreadline = “”;

//定义一个String类型的变量,用来每次读取一行

try {

FileReader fr = new FileReader(“data/imagelist.txt”);//创建FileReader对象,用来读取字符流

BufferedReader br = new BufferedReader(fr); //缓冲指定文件的输入

FileWriter fw = new FileWriter(“data/17d_result.txt”);//创建FileWriter对象,用来写入字符流

BufferedWriter bw = new BufferedWriter(fw); //将缓冲对文件的输出

while (br.ready()) {

myreadline = br.readLine();//读取一行

BufferedImage image = toBufferedImage(new ImageIcon(“data/Image/”+myreadline).getImage());

int height = image.getHeight();

int width = image.getWidth();

int r1=0, r2=0, r3=0, r4=0, r5=0, r6=0, r7=0, r8=0, r9=0, r10=0, r11=0, r12=0, r13=0, r14=0, r15=0, r16=0, r17=0;

int g1=0, g2=0, g3=0, g4=0, g5=0, g6=0, g7=0, g8=0, g9=0, g10=0, g11=0, g12=0, g13=0, g14=0, g15=0, g16=0, g17=0;

int b1=0, b2=0, b3=0, b4=0, b5=0, b6=0, b7=0, b8=0, b9=0, b10=0, b11=0, b12=0, b13=0, b14=0, b15=0, b16=0, b17=0;

int rgb1=0, rgb2=0, rgb3=0, rgb4=0, rgb5=0, rgb6=0, rgb7=0, rgb8=0, rgb9=0, rgb10=0, rgb11=0, rgb12=0, rgb13=0, rgb14=0, rgb15=0, rgb16=0, rgb17=0;

//System.out.println(“Height=” + height + “, Width=” + width);

//Random ran = new Random();

//int x = ran.nextInt(width), y = ran.nextInt(height);

for (int y=0;yheight;y++) {

for (int x=0;xwidth;x++) {

Color color = new Color(image.getRGB(x, y));

if(color.getRed()=15)

r1++;

if(color.getRed()15 color.getRed()=30)

r2++;

if(color.getRed()30 color.getRed()=45)

r3++;

if(color.getRed()45 color.getRed()=60)

r4++;

if(color.getRed()60 color.getRed()=75)

r5++;

if(color.getRed()75 color.getRed()=90)

r6++;

if(color.getRed()90 color.getRed()=105)

r7++;

if(color.getRed()105 color.getRed()=120)

r8++;

if(color.getRed()120 color.getRed()=135)

r9++;

if(color.getRed()135 color.getRed()=150)

r10++;

if(color.getRed()150 color.getRed()=165)

r11++;

if(color.getRed()165 color.getRed()=180)

r12++;

if(color.getRed()180 color.getRed()=195)

r13++;

if(color.getRed()195 color.getRed()=210)

r14++;

if(color.getRed()210 color.getRed()=225)

r15++;

if(color.getRed()225 color.getRed()=240)

r16++;

if(color.getRed()240 color.getRed()=255)

r17++;

if(color.getGreen()=15)

g1++;

if(color.getGreen()15 color.getGreen()=30)

g2++;

if(color.getGreen()30 color.getGreen()=45)

g3++;

if(color.getGreen()45 color.getGreen()=60)

g4++;

if(color.getGreen()60 color.getGreen()=75)

g5++;

if(color.getGreen()75 color.getGreen()=90)

g6++;

if(color.getGreen()90 color.getGreen()=105)

g7++;

if(color.getGreen()105 color.getGreen()=120)

g8++;

if(color.getGreen()120 color.getGreen()=135)

g9++;

if(color.getGreen()135 color.getGreen()=150)

g10++;

if(color.getGreen()150 color.getGreen()=165)

g11++;

if(color.getGreen()165 color.getGreen()=180)

g12++;

if(color.getGreen()180 color.getGreen()=195)

g13++;

if(color.getGreen()195 color.getGreen()=210)

g14++;

if(color.getGreen()210 color.getGreen()=225)

g15++;

if(color.getGreen()225 color.getGreen()=240)

g16++;

if(color.getGreen()240 color.getGreen()=255)

g17++;

if(color.getBlue()=15)

b1++;

if(color.getBlue()15 color.getBlue()=30)

b2++;

if(color.getBlue()30 color.getBlue()=45)

b3++;

if(color.getBlue()45 color.getBlue()=60)

b4++;

if(color.getBlue()60 color.getBlue()=75)

b5++;

if(color.getBlue()75 color.getBlue()=90)

b6++;

if(color.getBlue()90 color.getBlue()=105)

b7++;

if(color.getBlue()105 color.getBlue()=120)

b8++;

if(color.getBlue()120 color.getBlue()=135)

b9++;

if(color.getBlue()135 color.getBlue()=150)

b10++;

if(color.getBlue()150 color.getBlue()=165)

b11++;

if(color.getBlue()165 color.getBlue()=180)

b12++;

if(color.getBlue()180 color.getBlue()=195)

b13++;

if(color.getBlue()195 color.getBlue()=210)

b14++;

if(color.getBlue()210 color.getBlue()=225)

b15++;

if(color.getBlue()225 color.getBlue()=240)

b16++;

if(color.getBlue()240 color.getBlue()=255)

b17++;

}

}

rgb1 = r1 + g1 + b1;

rgb2 = r2 + g2 + b2;

rgb3 = r3 + g3 + b3;

rgb4 = r4 + g4 + b4;

rgb5 = r5 + g5 + b5;

rgb6 = r6 + g6 + b6;

rgb7 = r7 + g7 + b7;

rgb8 = r8 + g8 + b8;

rgb9 = r9 + g9 + b9;

rgb10 = r10 + g10 + b10;

rgb11 = r11 + g11 + b11;

rgb12 = r12 + g12 + b12;

rgb13 = r13 + g13 + b13;

rgb14 = r14 + g14 + b14;

rgb15 = r15 + g15 + b15;

rgb16 = r16 + g16 + b16;

rgb17 = r17 + g17 + b17;

//System.out.println(“rect ” + rgb1 + ” ” + rgb2 + ” ” + rgb3);

bw.write(“rect ” + rgb1 + ” ” + rgb2 + ” ” + rgb3 + ” ” + rgb4 + ” ” + rgb5 + ” ” + rgb6 + ” ” + rgb7 + ” ” + rgb8 + ” ” + rgb9 + ” ” + rgb10 + ” ” + rgb11 + ” ” + rgb12 + ” ” + rgb13 + ” ” + rgb14 + ” ” + rgb15 + ” ” + rgb16 + ” ” + rgb17); //写入文件

bw.newLine();

//System.out.println(myreadline);//在屏幕上输出

}

bw.flush(); //刷新该流的缓冲

bw.close();

br.close();

fw.close();

br.close();

fr.close();

} catch (IOException e) {

e.printStackTrace();

}

}

// This method returns a buffered image with the contents of an image

public static BufferedImage toBufferedImage(Image image) {

if (image instanceof BufferedImage) {

return (BufferedImage) image;

}

// Determine if the image has transparent pixels; for this method’s

// implementation, see e661 Determining If an Image Has Transparent

// Pixels

boolean hasAlpha = hasAlpha(image);

// Create a buffered image with a format that’s compatible with the

// screen

BufferedImage bimage = null;

GraphicsEnvironment ge = GraphicsEnvironment

.getLocalGraphicsEnvironment();

try {

// Determine the type of transparency of the new buffered image

int transparency = Transparency.OPAQUE;

if (hasAlpha) {

transparency = Transparency.BITMASK;

}

// Create the buffered image

GraphicsDevice gs = ge.getDefaultScreenDevice();

GraphicsConfiguration gc = gs.getDefaultConfiguration();

bimage = gc.createCompatibleImage(image.getWidth(null), image

.getHeight(null), transparency);

} catch (HeadlessException e) {

// The system does not have a screen

}

if (bimage == null) {

// Create a buffered image using the default color model

int type = BufferedImage.TYPE_INT_RGB;

if (hasAlpha) {

type = BufferedImage.TYPE_INT_ARGB;

}

bimage = new BufferedImage(image.getWidth(null), image

.getHeight(null), type);

}

// Copy image to buffered image

Graphics g = bimage.createGraphics();

// Paint the image onto the buffered image

g.drawImage(image, 0, 0, null);

g.dispose();

return bimage;

}

// This method returns true if the specified image has transparent pixels

public static boolean hasAlpha(Image image) {

// If buffered image, the color model is readily available

if (image instanceof BufferedImage) {

BufferedImage bimage = (BufferedImage) image;

return bimage.getColorModel().hasAlpha();

}

// Use a pixel grabber to retrieve the image’s color model;

// grabbing a single pixel is usually sufficient

PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);

try {

pg.grabPixels();

} catch (InterruptedException e) {

}

// Get the image’s color model

ColorModel cm = pg.getColorModel();

return cm.hasAlpha();

}

}

如何在Qt中读取PNG文件,并写入.dat文件中

在c语言中,创建单链表需要使用到malloc函数动态申请内存;文件的读写需要首先使用fopen函数打开文件,然后使用fscanf,fgetc, fgets,fprintf,fputc,fputs等函数读写函数,最后读写完毕要使用fclose函数关闭函数。

下面的源程序展示了关于单链表如何从文件中读取数据和往文件里存入数据。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

#includea href=”;tn=44039180_cprfenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y4nyfLmHDYnWT3uWc3nvN-0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnH6drjRsPHf1″ target=”_blank” class=”baidu-highlight”stdio.h/a

#includea href=”;tn=44039180_cprfenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y4nyfLmHDYnWT3uWc3nvN-0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnH6drjRsPHf1″ target=”_blank” class=”baidu-highlight”stdlib.h/a

#includememory.h

typedef struct node {

int data;

struct node *next;

}node;

//从文件中读取数据存入链表

node *createlink()

{

node *head =(node*)malloc(sizeof(node));

int t;

node *p;

node *q;

p=q=head;

FILE * r= fopen(“input.txt”,”r”);

if(r==NULL)

{

printf(“打开文件失败!”);

return NULL;

}

while(fscanf(r,”%d”,t)!=EOF)

{

q= (node*)malloc(sizeof(node));

q-data=t;

p-next=q;

p=q;

}

p-next=NULL;

return head;

}

//输出链表到屏幕和文件output.txt

void outlink(node *head)

{

node *p=head-next;

FILE *w =fopen(“output.txt”,”w”);

if(w==NULL)

{

printf(“打开文件失败!”);

return;

}

while(p)

{

//输出链表节点数据到屏幕

printf(“%d “,p-data);

//输出链表节点数据到文件output.txt

fprintf(w,”%d “,p-data);

p=p-next;

}

printf(“\n”);

fprintf(w,”\n”);

fclose(w);

return;

}

int main()

{

node *head;

int n,m;

head=createlink();

outlink(head);

system(“pause”);

return 0;

}

如何用c语言编程读入一张png格式的图片。

使用GDI+(只有一行代码):

Image img(TEXT(PNG图片路径));

还有libpng等等库,如果要自己实现读取代码就比较繁琐了

本文来自投稿,不代表【】观点,发布者:【

本文地址: ,如若转载,请注明出处!

举报投诉邮箱:253000106@qq.com

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2024年3月28日 18:48:24
下一篇 2024年3月28日 18:54:33

相关推荐

  • c语言改写模式,c语言实现修改功能

    c语言程序修改? 1、这个程序有4个错误,我都加粗了,第一个是m没有赋初值,第二个是while表达式中的ch=getchar()需要括号括起来,第三个是m=m*10+ch-0中的0也需要用单引号括起来,第四个是第2个while中为m!=0。 2、define容易造成误会,因为不符合一般的编程习惯,false 0, true 1;scanf放在你的那个地方是达…

    2024年5月23日
    4100
  • c语言控制代码的换码序列,c语言交换代码

    求C语言编程大神解答一下下面这个编程代码? k==5,用5去除125余0,所以r=125%5中r为0。由于!0为1,所以执行while循环体:先打印出5(k的值),再n=n/k==125/5=25;由于251则再打印出*号。这一循环结果输出是5*。 下面是我的代码,三个函数分别对应三个问题。 在实现基本要求的前提下,拓展了可以从键盘输入的功能,以下为各题代码…

    2024年5月23日
    5800
  • c语言扫描io脚状态,c语言端口扫描

    求51单片机的上升沿和下降沿C语言检测程序列子,端口就是普通IO口。 上升沿触发是当信号有上升沿时的开关动作,当电位由低变高而触发输出变化的就叫上升沿触发。也就是当测到的信号电位是从低到高也就是上升时就触发,叫做上升沿触发。 单片机怎么计算1s内下降沿的个数的C语言程序或者计算两个下降沿的时间(检测脉冲频率)计算1s内下降沿的个数方法是,一个定时器设置定时1…

    2024年5月23日
    4500
  • c语言mallloc使用的简单介绍

    C语言中使用malloc必须加#includemallo.h? 1、在C语言中使用malloc函数进行动态内存分配。malloc的全称是memory allocation,中文叫动态内存分配。原型:extern void malloc(unsigned int num_bytes);功能:分配长度为num_bytes字节的内存块。 2、你可以看一下C语言那本…

    2024年5月23日
    4500
  • c语言三位小数,C语言三位小数

    怎样用C++语言输出精确到小数点后三位的数? 1、用C++语言输出精确到小数点后三位的数,可以参考下面给出的代码:coutsetiosflags(ios:fixed)setprecision(3)。其中 setiosflags中set是设置的意思。ios是iostream的缩写,即输入输出流。flags是标志的意思。 2、要精确到小数点后若干位,则数据类型为…

    2024年5月23日
    7500
  • c语言21点游戏,二十一点游戏代码c语言

    如何使用C语言编写简单小游戏? 1、数学知识:长方形的面积S=a*b 长方形周长L=2*(a+b)其中a b分别为长方形的宽和高。算法分析:长方形面积及周长均依赖于宽和高,所以先要输入宽高值,然后根据公式计算,输出结果即可。 2、/*也不知道你是什么级别的,我是一个新手,刚接触编程语言,以下是我自己变得一个小程序,在所有c语言的编译器(vc++0、turbo…

    2024年5月23日
    6500
  • c语言当中的null,C语言当中的符号

    C/C++中,NULL和null的区别是什么? nul 和 null要看编译器,不同的编译器有所区别。 所以C或者C++中都使用一个特殊定义NULL表示无效值,其本质就是未定义具体数据类型的0值。 null是是什么都没有的意思。在java中表示空对象。 本意是“空的;元素只有零的”意思。计算机中通常表示空值,无结果,或是空集合。\x0d\x0a在ASCII码…

    2024年5月23日
    4700
  • 包含c语言对txt文件命名的词条

    如何在C语言编程里面修改源文件名字 如果你是在WINDOWS的话,简单了,随便用个编辑器,比如记事本,然后写c源程序,保存到你想要保存的位置。如果你在DOS下,可以用edit,写好以后,按alt键,选择文件菜单,然后保存。 用open打开文件,注意操作模式使用“修改”或者“添加” 用write或者fprintf向文件中写入你的内容。 用close关闭文件。 …

    2024年5月23日
    5000
  • 学c语言编程,学c语言编程用什么软件

    编程开发必须要学C语言吗? 1、要学习。编程开发的学习内容主要包括c语言、python和c+语言。C语言作为一种简单灵活的高级编程语言,它是一个面向过程的语言,一般是作为计算机专业的基础入门语言课程。 2、C语言。对于刚接触编程的人来说,先学习C语言是非常重要的。C语言可以说是是计算机编程语言的鼻祖,其他的编程语言几乎全是由C语言变化衍生出来的。 3、不需要…

    2024年5月23日
    3500
  • c语言用string定义字符串,c语言中用string类型来处理字符串类型

    C++怎样定义定义字符串 1、第一是字符数组来表示字符串。用下面的语句声明:char a[10];C语言中字符数组与字符串的唯一区别是字符串末尾有一个结束符\0,而字符数组不需要。 2、在C中定义字符串有下列几种形式:字符串常量,char数组,char指针 字符串常量 即:位于一对双括号中的任何字符。双引号里的字符加上编译器自动提供的结束标志\0字符,作为 …

    2024年5月23日
    4500

发表回复

登录后才能评论



关注微信