當前位置:首頁 » 編程語言 » c語言讀取圖片再輸出
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言讀取圖片再輸出

發布時間: 2022-05-02 14:50:25

『壹』 用c語言實現,簡單的告訴我,怎麼讀取當前目錄下的一個jpg圖像,並且顯示出來

FILE *fp;
if((fp = fopen("sb.jpg","r") == NULL)) {
printf("\nerror");
exit(0);
}

以上僅僅能打開jpg文件,想要顯示需要窗口編程。win32或者MFC來實現。

『貳』 用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 ",size);

FILE * pFile,*qw;

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

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

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

printf("%d== ",pFile);

printf("%d ",size);

fread(buffer,1,size,qw);

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

fclose (pFile);

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

return 0;

}

(2)c語言讀取圖片再輸出擴展閱讀:

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] = ''; /*去掉換行符*/

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

}

return 0;

}




『叄』 C語言輸出圖片

輸出BMP圖片的:
#include "stdlib.h"
#include "graphics.h"
#include "stdio.h"

#define WIDTH 120
#define HEIGHT 120
//用一個二維數組保存的圖片,這里圖片長高必須是4的倍數,不是4的倍數必須進行補齊,
//但是這裡面是沒有進行補位操作的,你也可以自己去看看24位bmp圖片的編碼格式,
//然後就知道該怎麼用了
void mian()
{
FILE *fp;
unsigned char bmp[16][200];
unsigned char bmp2[WIDTH][HEIGHT*3];
int i = 1,size1 = 0,size2 = 0,size0 = 0;
int j = 0;
if((fp = fopen("G:\\new\\Boy5.bmp","rb")) == NULL)//打開圖片
exit(0);
i = 0;
fseek(fp,54L,0);//BMP圖片陣列是從第54位開始
//#########讀入圖片陣列到數組中##########
while(i < WIDTH*HEIGHT*3)
{
*(bmp2[0]+i) = fgetc(fp);
i++;
}
fclose(fp);
initgraph(700, 700); // 打開圖形窗口,這里這個函數我之前是在VC下編譯的,用了一個網上的繪圖庫,與TC的打開圖形界面函數有所差別,改掉就可以了
//####################################輸出圖片,這里是打點的方式在圖形界面輸出,
for(i = 0;i < (WIDTH-1);)
{
for(j =0;j < (HEIGHT-1)*3 ;)
{
putpixel(50+(j/3),600-(i/1),RGB((int)bmp2[i][j+2],(int)bmp2[i][j+3],(int)bmp2[i][j+4]));//輸出像素點
j+=3;
}
i++;
}
getchar();
closegraph(); // 關閉圖形窗口
}

『肆』 怎樣用C語言讀取一張圖片並進行簡單處理

圖片格式有很多種
bmp,
jpg,
gif,
png
等等
每種都有自己的格式。
處理圖片
一般流程
都是先讀取圖片文件,根據格式解析成點陣圖(bitmap)
然後對點陣圖進行處理。
所以
重點是解析。
這個是有很多的開源C庫的。

『伍』 c語言 讀取一副BMP格式的圖像文件,並將並將其旋轉180度後輸出一個新的BMP格式的圖

先去看看bmp圖片的格式,一般就是文件頭信息和文件數據信息。
你需要做的是先從bmp圖片中解析出頭信息,找到是什麼格式的888,565,555這種,然後還需要找到寬高,旋轉180°就是將數據左右對稱,上下對稱。
最後把頭信息和你改過的數據信息組合起來就可以了

『陸』 怎樣用c語言輸出一張圖片

如果圖有規律,那麼利用規律寫一個函數。如果沒有規律,將圖片的每一個像素點的RGB值保存,可以附帶坐標信息,不管是絕對坐標還是相對坐標,然後直接輸出就可以了。

『柒』 怎麼用C語言讀取gif圖片內容文件

用二進制文件方法打開。
(1)讀gif_id,輸出gif_id,
6
bytes
(2)讀logical_screen_descriptor(結構),並輸出
(3)如果
ScrHd.flag_GC_table
==
1,
則有全局色表,則讀全局色表,並輸出
(4)循環,
檢查gif塊的類型,按塊的類型讀塊,並輸出
若:
case:
0x2c
--
它是,Image
Descriptor(結構),讀塊並輸出
判斷有無色表,有則讀塊並輸出,
下面
1
byte
表示
lZW
min
Code
Size,讀並輸出
子塊
(include
size,
bytes*size
data)
讀塊並輸出
子塊
(include
size,
bytes*size
data)
讀塊並輸出
可能有很多子塊,讀塊並輸出
直到
0x00
讀塊並輸出,
break;
若case:
0x21
0xf9
--
控制塊,讀到
0x00,
並輸出
break;
若case:
0x21
0xFe
--
註解塊,讀到
0x00,並輸出
break;
若case:
0x21
0x01
--
普通文本塊,讀到
0x00,並輸出
break;
若case:
0x21
0xff
--
應用程序塊,讀到
0x00,並輸出
break;
若case:
0x3B
--
END
第一個圖片完畢的標識,並輸出
break;
default:
列印「文件錯誤」break;
(5)關閉文件。

『捌』 如何用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 || 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);

}

(8)c語言讀取圖片再輸出擴展閱讀

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 ",size);

FILE * pFile,*qw;

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

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

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

printf("%d== ",pFile);

printf("%d ",size);

fread(buffer,1,size,qw);

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

fclose (pFile);

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

return 0;

}

『玖』 c語言如何讀取文件並輸出

//每個單詞之間必須用換行符隔開
#include
<stdio.h>
#include
<malloc.h>
#include
<string.h>
#define
BUF_MAX
1024
int
main()
{
char
findStr[100];
char
*pStr=(char*)malloc(BUF_MAX);
FILE*pFile;
if((pFile=fopen("C:\\Users\\hp\\Desktop\\words.txt","rt"))==NULL)
{
fprintf(stderr,"打開文件錯誤");
return
1;
}
puts("請輸入你要查找的單詞:");
scanf("%s",findStr);
bool
bFind=false;
while(fgets(pStr,BUF_MAX-1,pFile)!=NULL)
{
if(strstr(pStr,findStr)!=NULL)
{
bFind=true;
printf("%s",pStr);
}
}
if(!bFind)
puts("沒有找到");
fclose(pFile);
return
0;
}

『拾』 c語言中如何輸出圖像

lz需要弄清楚,你要輸出什麼格式的圖像,bmp,JPG。。。不同格式的圖像的數據內部結構是不一樣的,弄清楚不同格式圖片的數據結構組成,直接採用相應地演算法,用C語言文件輸出函數(fopen,fprintf……那幾個函數,具體我也忘了怎麼用了)將數據點寫入文件就行了。