用c语言如何读取和保存jpg图片文件?

用c语言如何读取和保存jpg图片文件?,第1张

#include <stdio.h>

#include <stdlib.h>

#include <windows.h>

int file_size(char* filename)//获取文件名为filename的文件大小。

{

FILE *fp = fopen(filename, "rb")//打开文件。

int size

if(fp == NULL) // 打开文件失败

return -1

fseek(fp, 0, SEEK_END)//定位文件指针到文件尾。

size=ftell(fp)//获取文件指针偏移量,即文件大小。

fclose(fp)//关闭文件。

return size

}

int main ()

{

int size=0

size=file_size("qw")

printf("%d\n",size)

FILE * pFile,*qw

char *buffer=(char*)malloc(sizeof(char)*size)

qw   =fopen("qw","r")

pFile = fopen ( "qwe" , "wb" )

printf("%d==\n",pFile)

printf("%d\n",size)

fread(buffer,1,size,qw)

fwrite (buffer , sizeof(byte), size , pFile )

fclose (pFile)

rename("qwe","Groot.jpg")

return 0

}

扩展资料:

c语言读取TXT文件:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX_LINE 1024

int main()

{

char buf[MAX_LINE]  /*缓冲区*/

FILE *fp            /*文件指针*/

int len             /*行字符个数*/

if((fp = fopen("test.txt","r")) == NULL)

{

perror("fail to read")

exit (1)

}

while(fgets(buf,MAX_LINE,fp) != NULL)

{

len = strlen(buf)

buf[len-1] = '\0'  /*去掉换行符*/

printf("%s %d \n",buf,len - 1)

}

return 0

}

创建一个BtnShow按钮,然后添加以下代码: private void BtnShow_Click(object sender, System.EventArgs e) {SqlConnection conn=new SqlConnection(ConnectString) string strSql="SELECT Photo from Picture where StudentID="1: SqlCommand cmd=new SqlCommand(strSql,conn) conn.Open() SqlDataReader reader=cmd.ExecuteReader() reader.Read() MemoryStream ms=new MemoryStream((byte[])reader["Photo"]) //创建一个内存 读取数据流,将读取的数据库的图片以二进制的byte[]流存入内存中。 reader.Close() conn.Close() StudentPhoto.Image=Image.FromStream(ms,true)//从刚才存储到内存的数据流中创建Image对象。 StudentPhoto.SizeMode=PictureBoxSizeMode.CenterIma}---------还有一个更好的方法,请参考: http://www.diybl.com/course/4_webprogram/asp.net/netjs/2008410/109033.html

把picturebox上的图片转换成数据流,然后存到数据库的二进制类型字段中;

从数据库的二进制字段中读取流,再将数据流转换成图片,再赋给picture box即可。

过程是这么个过程,代码手机就不码了


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/6667027.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-03-26
下一篇2023-03-26

发表评论

登录后才能评论

评论列表(0条)

    保存