c語言接收發送圖片,c語言調用圖片文件

本文目錄一覽:

如何用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 for (int j = 0; j if (iw – 3 || jh – 3)

image1[Twoto1(i,j,w)] = 0;

else

{

float temp = 0;

for (int m = 0; m5; m++)

for (int n = 0; n5; n++)

{

temp += (image[Twoto1(i-2+m,j-2+n,w)] moban[m][n]);

}

if (temp255) image1[Twoto1(i, j, w)] = 255;

else if (temp0) 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)//獲取文件名為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。使用fread函數將變數中的數據寫入剛剛創建的BMP文件中

怎樣在C語言中實現用socket傳送圖片

每個包有大小限制的,圖片可能是太大發送失敗。

建議你在每個數據包里加一個類似序號的結構。然後圖片數據傳過去了用序號重新組合,如果缺少哪個序號,還可以用序號重新請求。

包結構:序號(2位元組)+包長度(4位元組)+部分圖片數據

圖片分成N份

傳輸方;有個合成新包的函數。有個處理缺包請求的函數。

接受方:有個檢驗碎片是否完全傳過來的函數。有個組合圖片碎片的函數。還應該有發送缺包請求功能的函數。

C語言怎麼調用圖片?

圖片也是一個文件,

1.你是要打開圖片嗎?(把圖像顯示出來?)

2.還是只需要圖片文件。

如果是1,那麼你需要看.bmp的編碼方式和c庫的圖像類函數

如果是2,那麼你就可以用fopen,fread,fwrite,fprintf,fscanf等調用即可。

用c語言如何讀取和保存jpg圖片文件?

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

}

c語言http,server 如何發圖片到瀏覽器(已經可以發送HTML代碼了)

在HTML頁面用用img標籤,或者直接瀏覽器網址訪問圖片地址就好了

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/276551.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-17 19:38
下一篇 2024-12-19 13:20

相關推薦

發表回復

登錄後才能評論