c语言接收发送图片,c语言调用图片文件

发布时间:2022-11-30

本文目录一览:

  1. 如何用c语言读取图片
  2. c/c++ winsock 如何发送和接收图片bmp格式文件
  3. 怎样在C语言中实现用socket传送图片
  4. C语言怎么调用图片?
  5. 用c语言如何读取和保存jpg图片文件?
  6. c语言http,server 如何发图片到浏览器(已经可以发送HTML代码了)

如何用c语言读取图片

#include
using namespace std;
#define Twoto1(i,j,w) i*w+j
void createimage(unsigned char *img, int w, int h) {
    img = new unsigned char[w*h];
}
void delateimage(unsigned char*img) {
    delete []img;
}
void readimage(unsigned char*img, int w, int h, char *fname) {
    FILE *fp;
    fopen_s(fp,fname, "rb");
    if (fp == NULL) {
        cout << "error" << endl;
        return;
    }
    size_t result;
    result = fread(img, sizeof(unsigned char), w*h, fp);
    if (result != w*h) {
        cout << "Reading error" << endl;
        return;
    } else {
        cout << "Reading Ok!" << endl;
    }
    fclose(fp);
}
void mobanjuanji(unsigned char image, unsigned char*image1, int w, int h, float moban[5][5]) {
    for (int i = 0; i < w; i++) {
        for (int j = 0; j < h; j++) {
            if (i < w - 3 || j < h - 3) {
                image1[Twoto1(i,j,w)] = 0;
            } else {
                float temp = 0;
                for (int m = 0; m < 5; m++) {
                    for (int n = 0; n < 5; n++) {
                        temp += (image[Twoto1(i-2+m,j-2+n,w)] * moban[m][n]);
                    }
                }
                if (temp > 255) {
                    image1[Twoto1(i, j, w)] = 255;
                } else if (temp < 0) {
                    image1[Twoto1(i, j, w)] = 0;
                } else {
                    image1[Twoto1(i, j, w)] = temp;
                }
            }
        }
    }
}
void saveimage(unsigned char *img, int w, int h, char *fname) {
    FILE *fp;
    fopen_s(fp, fname, "wb");
    if (fp == NULL) {
        cout << "error" << endl;
        return;
    }
    size_t result;
    result = fwrite(img, sizeof(unsigned char), w*h, fp);
    if (result != w*h) {
        cout << "Write error" << endl;
        return;
    } else {
        cout << "Write Ok!" << endl;
    }
    fclose(fp);
}
void main() {
    unsigned char *img;
    unsigned char *img1;
    float moban[5][5] = { {0,0,0,0,0}, {0, -1, 0, 1, 0 }, { 0, -2, 0, 2, 0 }, { 0, -1, 0, 1, 0 }, { 0,0,0,0,0 } };
    // float moban[5][5] = { 0 };
    int w = 512, h = 512;
    createimage(img, w, h);
    createimage(img1, w, h);
    readimage(img, w, h, "E:\\ss.raw");
    mobanjuanji(img, img1, w, h, moban);
    saveimage(img, w, h, "E:\\ss_1.raw");
    saveimage(img1, w, h, "E:\\ss_2.raw");
    delateimage(img);
    delateimage(img1);
}

扩展资料

C语言实现一个图片的读出和写入

#include stdlib.h
#include windows.h
int file_size(char* 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/c++ winsock 如何发送和接收图片bmp格式文件

我以C语言的办法来简单描述下:

客户端:

  1. 发送连接申请到服务端
  2. 使用 fopen 打开要发送的 BMP 文件
  3. 使用 fread 函数读取数据存入变量中
  4. 使用 send 函数将变量中内容发送到服务端

服务端:

  1. 接受客户端连接申请
  2. 使用 fopen 函数创建一个 BMP 文件
  3. 使用 recv 函数来接收传送过来的数据存入变量中
  4. 使用 fwrite 函数将变量中的数据写入刚刚创建的 BMP 文件中

怎样在C语言中实现用socket传送图片

每个包有大小限制的,图片可能是太大发送失败。 建议你在每个数据包里加一个类似序号的结构。然后图片数据传过去了用序号重新组合,如果缺少哪个序号,还可以用序号重新请求。 包结构:序号(2字节)+包长度(4字节)+部分图片数据 图片分成 N 份 传输方:有个合成新包的函数。有个处理缺包请求的函数。 接受方:有个检验碎片是否完全传过来的函数。有个组合图片碎片的函数。还应该有发送缺包请求功能的函数。

C语言怎么调用图片?

图片也是一个文件:

  1. 你是要打开图片吗?(把图像显示出来?)
  2. 还是只需要图片文件。
    如果是 1,那么你需要看 .bmp 的编码方式和 C 库的图像类函数。 如果是 2,那么你就可以用 fopenfreadfwritefprintffscanf 等调用即可。

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

#include stdio.h
#include stdlib.h
#include windows.h
int file_size(char* 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;
}

c语言http,server 如何发图片到浏览器(已经可以发送HTML代码了)

在 HTML 页面用用 img 标签,或者直接浏览器网址访问图片地址就好了。