资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

jpg读取函数c语言,c读文件函数

C语言中怎样获得jpg图片的宽和高?

lt;%

创新互联专业为企业提供长葛网站建设、长葛做网站、长葛网站设计、长葛网站制作等企业网站建设、网页设计与制作、长葛企业网站模板建站服务,10多年长葛做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

Class ImgWHInfo '获取图片宽度和高度的类,支持JPG,GIF,PNG,BMP

Dim ASO

Private Sub Class_Initialize

Set ASO=Server.CreateObject("ADODB.Stream")

ASO.Mode=3

ASO.Type=1

ASO.Open

End Sub

Private Sub Class_Terminate

Err.Clear

Set ASO=Nothing

End Sub

Private Function Bin2Str(Bin)

Dim I, Str

For I=1 To LenB(Bin)

clow=MidB(Bin,I,1)

If ASCB(clow)128 Then

Str = Str Chr(ASCB(clow))

Else

I=I+1

If I = LenB(Bin) Then Str = Str Chr(ASCW(MidB(Bin,I,1)clow))

End If

Next

Bin2Str = Str

End Function

Private Function Num2Str(Num,Base,Lens)

Dim Ret

Ret = ""

While(Num=Base)

Ret = (Num Mod Base) Ret

Num = (Num - Num Mod Base)/Base

Wend

Num2Str = Right(String(Lens,"0") Num Ret,Lens)

End Function

Private Function Str2Num(Str,Base)

Dim Ret,I

Ret = 0

For I=1 To Len(Str)

Ret = Ret *base + Cint(Mid(Str,I,1))

Next

Str2Num=Ret

End Function

Private Function BinVal(Bin)

Dim Ret,I

Ret = 0

For I = LenB(Bin) To 1 Step -1

Ret = Ret *256 + AscB(MidB(Bin,I,1))

Next

BinVal=Ret

End Function

Private Function BinVal2(Bin)

Dim Ret,I

Ret = 0

For I = 1 To LenB(Bin)

Ret = Ret *256 + AscB(MidB(Bin,I,1))

Next

BinVal2=Ret

End Function

Private Function GetImageSize(filespec)

Dim bFlag

Dim Ret(3)

ASO.LoadFromFile(filespec)

bFlag=ASO.Read(3)

Select Case Hex(binVal(bFlag))

Case "4E5089":

ASO.Read(15)

ret(0)="PNG"

ret(1)=BinVal2(ASO.Read(2))

ASO.Read(2)

ret(2)=BinVal2(ASO.Read(2))

Case "464947":

ASO.read(3)

ret(0)="gif"

ret(1)=BinVal(ASO.Read(2))

ret(2)=BinVal(ASO.Read(2))

Case "535746":

ASO.read(5)

binData=ASO.Read(1)

sConv=Num2Str(ascb(binData),2 ,8)

nBits=Str2Num(left(sConv,5),2)

sConv=mid(sConv,6)

While(len(sConv)nBits*4)

binData=ASO.Read(1)

sConv=sConvNum2Str(AscB(binData),2 ,8)

Wend

ret(0)="SWF"

ret(1)=Int(Abs(Str2Num(Mid(sConv,1*nBits+1,nBits),2)-Str2Num(Mid(sConv,0*nBits+1,nBits),2))/20)

ret(2)=Int(Abs(Str2Num(Mid(sConv,3*nBits+1,nBits),2)-Str2Num(Mid(sConv,2*nBits+1,nBits),2))/20)

Case "FFD8FF":

Do

Do: p1=binVal(ASO.Read(1)): Loop While p1=255 And Not ASO.EOS

If p1191 And p1196 Then Exit Do Else ASO.read(binval2(ASO.Read(2))-2)

Do:p1=binVal(ASO.Read(1)):Loop While p1255 And Not ASO.EOS

Loop While True

ASO.Read(3)

ret(0)="JPG"

ret(2)=binval2(ASO.Read(2))

ret(1)=binval2(ASO.Read(2))

Case Else:

If left(Bin2Str(bFlag),2)="BM" Then

ASO.Read(15)

ret(0)="BMP"

ret(1)=binval(ASO.Read(4))

ret(2)=binval(ASO.Read(4))

Else

ret(0)=""

End If

End Select

ret(3)="width=""" ret(1) """ height=""" ret(2) """"

getimagesize=ret

End Function

Public Function imgW(IMGPath)

Dim FSO,IMGFile,FileExt,Arr

Set FSO = Server.CreateObject("Scripting.FileSystemObject")

If (FSO.FileExists(IMGPath)) Then

Set IMGFile = FSO.GetFile(IMGPath)

FileExt=FSO.GetExtensionName(IMGPath)

Select Case FileExt

Case "gif","bmp","jpg","png":

Arr=GetImageSize(IMGFile.Path)

imgW = Arr(1)

End Select

Set IMGFile=Nothing

Else

imgW = 0

End If

Set FSO=Nothing

End Function

Public Function imgH(IMGPath)

Dim FSO,IMGFile,FileExt,Arr

Set FSO = server.CreateObject("Scripting.FileSystemObject")

If (FSO.FileExists(IMGPath)) Then

Set IMGFile = FSO.GetFile(IMGPath)

FileExt=FSO.GetExtensionName(IMGPath)

Select Case FileExt

Case "gif","bmp","jpg","png":

Arr=getImageSize(IMGFile.Path)

imgH = Arr(2)

End Select

Set IMGFile=Nothing

Else

imgH = 0

End If

Set FSO=Nothing

End Function

End Class

IMGPath="Test.jpg"

Set PP = New ImgWHInfo

W = PP.imgW(Server.Mappath(IMGPath))

H = PP.imgH(Server.Mappath(IMGPath))

Set pp = Nothing

Response.Write("img src='"IMGPath"' border=0br宽:"W";高:"H)

%

C语言jpg解码问题(文件读取)

mgdecmp库是WINCE 系统提供一个解码BMP,GIF,PNG,JPG的文件的库,这个库的缺陷是不能解出MASK 图片.具体用法如下:

1.把imgdecmp.h加入工程,imgdecmp.h文件内容如下:

/*---------------------------------------------------------------------------*/

*

* (c) Copyright Microsoft Corp. 1997-98 All Rights Reserved

*

* module: imgdecmp.h

* date:

* author: jaym

*

* purpose:

*

/*---------------------------------------------------------------------------*/

#ifndef __IMGDECMP_H__

#define __IMGDECMP_H__

#ifdef __cplusplus

extern "C" {

#endif // __cplusplus

#include "imgrendr.h"

typedef void (CALLBACK *PROGRESSFUNC)(IImageRender *pRender, BOOL bComplete, LPARAM lParam);

typedef DWORD (CALLBACK *GETDATAFUNC)(LPSTR szBuffer, DWORD dwBufferMax, LPARAM lParam);

typedef struct tagDecompressImageInfo {

DWORD dwSize; // Size of this structure

LPBYTE pbBuffer; // Pointer to the buffer to use for data

DWORD dwBufferMax; // Size of the buffer

DWORD dwBufferCurrent; // The amount of data which is current in the buffer

HBITMAP * phBM; // Pointer to the bitmap returned (can be NULL)

IImageRender ** ppImageRender; // Pointer to an IImageRender object (can be NULL)

int iBitDepth; // Bit depth of the output image

LPARAM lParam; // User parameter for callback functions

HDC hdc; // HDC to use for retrieving palettes

int iScale; // Scale factor (1 - 100)

int iMaxWidth; // Maximum width of the output image

int iMaxHeight; // Maxumum height of the output image

GETDATAFUNC pfnGetData; // Callback function to get more data

PROGRESSFUNC pfnImageProgress; // Callback function to notify caller of progress decoding the image

COLORREF crTransparentOverride; // If this color is not (UINT)-1, it will override the

// transparent color in the image with this color. (GIF ONLY)

} DecompressImageInfo;

#define IMGDECOMP_E_NOIMAGE 0x800b0100

COLORREF *

GetHalftonePalette();

COLORREF *

Get332Palette();

HRESULT

DecompressImageIndirect(DecompressImageInfo *pParams);

#ifdef __cplusplus

};

#endif // __cplusplus

#endif // !__IMGDECMP_H__

2.按照下面的方法使用.

HDC hdc = CreateCompatibleDC(NULL);

DecompressImageInfo dii;

BYTE szBuffer[1024] = {0};

HANDLE hFile = INVALID_HANDLE_VALUE;

// Fill in the 'DecompressImageInfo' structure

dii.dwSize = sizeof( DecompressImageInfo ); // Size of this structure

dii.pbBuffer = szBuffer; // Pointer to the buffer to use for data

dii.dwBufferMax = 1024; // Size of the buffer

dii.dwBufferCurrent = 0; // The amount of data which is current in the buffer

dii.phBM = hBitmap; // Pointer to the bitmap returned (can be NULL)

dii.ppImageRender = NULL; // Pointer to an IImageRender object (can be NULL)

dii.iBitDepth = GetDeviceCaps(hdc,BITSPIXEL); // Bit depth of the output image

dii.lParam = ( LPARAM ) iCeBMPFile;//hFile; // User parameter for callback functions

dii.hdc = hdc; // HDC to use for retrieving palettes

dii.iScale = 100; // Scale factor (1 - 100)

dii.iMaxWidth = 10000; // Maximum width of the output image

dii.iMaxHeight = 10000; // Maxumum height of the output image

dii.pfnGetData = GetImageData; // Callback function to get image data

dii.pfnImageProgress = ImageProgress; // Callback function to notify caller of progress decoding the image

dii.crTransparentOverride = ( UINT ) -1; // If this color is not (UINT)-1, it will override the

// transparent color in the image with this color. (GIF ONLY)

// Process and decompress the image data

typedef HRESULT (CALLBACK* ULPRET)( DecompressImageInfo* pParams );

HINSTANCE hLib;

ULPRET lpfnDLLProc;

hLib = LoadLibrary ( L"Imgdecmp.dll" );

if ( hLib )

{

lpfnDLLProc = (ULPRET) GetProcAddress ( hLib, L"DecompressImageIndirect" );

hr = (*lpfnDLLProc) ( dii );

FreeLibrary ( hLib );

}

// Clean up

DeleteDC( hdc );

用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语言怎样打开JPG文件??

一般来讲是生成

ppm

or

pnm

处理图片数据的,

但是如果你非要读取

JPG

格式的数据,

我想下面的

Library

可以帮助到你.

如何用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语言实现,简单的告诉我,怎么读取当前目录下的一个jpg图像,并且显示出来

FILE *fp;

if((fp = fopen("sb.jpg","r") == NULL)) {

printf("\nerror");

exit(0);

}

以上仅仅能打开jpg文件,想要显示需要窗口编程。win32或者MFC来实现。


名称栏目:jpg读取函数c语言,c读文件函数
本文网址:http://cdkjz.cn/article/hcshhp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220