当前位置:首页 » 图片知识 » 如何用c代码显示jpg图片
扩展阅读
女生和渣男搞笑图片 2023-08-31 22:07:09
嘻嘻长什么样图片 2023-08-31 22:06:10

如何用c代码显示jpg图片

发布时间: 2023-01-21 19:59:21

A. c\c++用于显示jpg格式的代码。 关键代码或函数。

1、如何从BMP图片读取每个像素,然后根据PNG图片的格式以及RGB色彩算法,将BMP像素转换成png图片,最后根据PNG格式输出到文件。
例程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

FILE *fp = NULL;
char buf[PNG_BYTES_TO_CHECK];
LPSTR lpszFileName = lpszPathName;
if(!lpszFileName) return false;
//打开<a href="https://www..com/s?wd=PNG%E6%96%87%E4%BB%B6&tn=44039180_cpr&fenlei=-bIi4WUvYETgN-TLwGUv3EnWc4nHcvnHc" target="_blank" class="-highlight">PNG文件</a>
if(NULL == (fp = fopen(lpszFileName,"rb")))
{
delete []lpszFileName;
lpszFileName = NULL;
return false;
}
//验证是否为<a href="https://www..com/s?wd=PNG%E6%96%87%E4%BB%B6&tn=44039180_cpr&fenlei=-bIi4WUvYETgN-TLwGUv3EnWc4nHcvnHc" target="_blank" class="-highlight">PNG文件</a>
if(fread(buf,1,PNG_BYTES_TO_CHECK,fp) != PNG_BYTES_TO_CHECK)
{
fclose(fp);
delete []lpszFileName;
lpszFileName = NULL;
return false;
}
if( 0 != png_sig_cmp((unsigned char*)buf,(png_size_t)0,PNG_BYTES_TO_CHECK))
{
fclose(fp);
delete []lpszFileName;
lpszFileName = NULL;
return false;
}
//以下代码读取<a href="https://www..com/s?wd=PNG%E6%96%87%E4%BB%B6&tn=44039180_cpr&fenlei=-bIi4WUvYETgN-TLwGUv3EnWc4nHcvnHc" target="_blank" class="-highlight">PNG文件</a>
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
unsigned int sig_read = PNG_BYTES_TO_CHECK;
int color_type = 0,interlace_type = 0;
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
if( NULL == png_ptr)
{
fclose(fp);
delete []lpszFileName;
lpszFileName = NULL;
return false;
}
info_ptr = png_create_info_struct(png_ptr);
if(NULL == info_ptr)
{
fclose(fp);
png_destroy_read_struct(&png_ptr,&info_ptr,png_infopp_NULL);
delete []lpszFileName;
lpszFileName = NULL;
return false;
}
png_init_io(png_ptr,fp);
png_set_sig_bytes(png_ptr,sig_read);
png_read_info(png_ptr,info_ptr);
if(info_ptr->bit_depth == 16)png_set_strip_16(png_ptr);
if(info_ptr->color_type == PNG_COLOR_TYPE_GRAY && info_ptr->pixel_depth < 8)
png_set_gray_1_2_4_to_8(png_ptr);
switch (info_ptr->pixel_depth)
{
case 32:
if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)png_set_bgr(png_ptr);
png_set_invert_alpha(png_ptr);
//当为32位图像是应该如何处理,例如
//pDib->CreateDIB(info_ptr->width, info_ptr->height,32);
break;
case 24:
{
png_color_16 my_background={0,255,255,255,0};
png_set_background(png_ptr,&my_background,
PNG_BACKGROUND_GAMMA_SCREEN,0,1.0);
if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)png_set_bgr(png_ptr);
//当为32位图像是应该如何处理,例如
//pDib->CreateDIB(info_ptr->width, info_ptr->height,24);
}
break;
case 8:
case 4:
//当为32位图像是应该如何处理,例如
//if(info_ptr->num_palette>0)
//{
// DWORD *pRQ=new DWORD[info_ptr->num_palette];
// for(int i=0;i<info_ptr->num_palette;i++)
// {
// pRQ[i]=RGB(info_ptr->palette[i].red,info_ptr->palette[i].green,info_ptr->palette[i].blue);
// }
// pDib->CreateDIB(info_ptr->width, info_ptr->height,info_ptr->pixel_depth,pRQ,info_ptr->num_palette);
// delete[] pRQ;
//}
break;
default:
return false;
}
png_bytepp prow_pointers = new png_bytep[info_ptr->height];
UINT nRowBytes = png_get_rowbytes(png_ptr,info_ptr);
UINT row;
for(row=0;row<info_ptr->height;row++)
prow_pointers[row]=new BYTE[nRowBytes];
png_read_image(png_ptr,prow_pointers);
png_read_end(png_ptr,info_ptr);
//拷贝转换后的图像数据
for(row=0;row<info_ptr->height;row++)
memcpy(pDib->m_lpImage+(info_ptr->height-row-1)*pDib->m_nByteWidth,
prow_pointers[row],pDib->m_nByteWidth);

2、更简单的方法:用windows自带的图片和传真查看器打开要转换的图片,点击右下角的保存按钮,弹出保存对话框,选择要保存的类型。笔者经实验可转换的类型为gif,bmp,png,jpg.最大的优点是方便且不失。

B. C语言怎样显示一张图片

1、如果有图片(例如 wzzx.jpg) 程序中插一句:
system("mspaint wzzx.jpg"); 就可以 在运行时显示这张图片。
用字符串变量调用也可以:
char pic_name[80]="wzzx.jpg";
char cmd[100];
sprintf(cmd,"mspaint %s",pic_name);
system(cmd); // 显示图片

2、system函数:
原型:int system(const char * command);
功能:执行 dos(windows系统) 或 shell(Linux/Unix系统) 命令,参数字符串command为命令名;
说明:在windows系统中,system函数直接在控制台调用一个command命令。在Linux/Unix系统中,system函数会调用fork函数产生子进程,由子进程来执行command命令,命令执行完后随即返回原调用的进程;
头文件:stdlib.h;
返回值:命令执行成功返回0,执行失败返回-1。

C. c语言dos下如何显示jpg图片

jpg 文件结构非常复杂,除非你安装和调用已开发好的库,自己从0开始写程序要花很多时间,一两个星期是攻不下来的。
简单地显示,可以调微软的Paint.
如果要显示 abc.jpg 用下面 C 程序。
#include <stdio.h>
main(){
system("mspaint abc.jpg");
return 0;
}
========
复杂一点,输入jpeg 文件名,组成system(命令):
char p[80],cm[120];
printf("please input jpeg name\n");
gets(p);
sprintf(cm,"mspaint %s",p);
system(cm);

D. 用C语言实现,简单的告诉我,怎么读取当前目录下的一个jpg图像,并且显示出来

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

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

E. 怎样用C语言显示一张jpg的图片呢

可以用函数OleLoadPicture从包含有图像数据的流中装载图像。

具体实现代码如下:

//在显示图像之前,首先要获取到图像文件的存放路径,这里采用标准的文件打开对话框来选取图像文件,文件名存放在CString型的变量m_sPath中:
CFileDialogdlg(TRUE,"jpg","*.jpg",
OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
"JPEG文件(*.jpg)|*.jpg|GIF文件(*.gif)|*.gif||",NULL);
if(dlg.DoModal()==IDOK)
{
m_sPath=dlg.GetPathName();
Invalidate();
}
//为简单计,图形显示的代码直接在视类中的OnDraw中编写,首先打开文件并判断文件的可用性,并把文件内容放到流接口IStream的对象pStm中:
IStream*pStm;
CFileStatusfstatus;
CFilefile;
LONGcb;
……
if(file.Open(m_Path,CFile::modeRead)&&file.GetStatus(m_Path,fstatus)&&((cb=fstatus.m_size)!=-1))
{
HGLOBALhGlobal=GlobalAlloc(GMEM_MOVEABLE,cb);
LPVOIDpvData=NULL;
if(hGlobal!=NULL)
{
if((pvData=GlobalLock(hGlobal))!=NULL)
{
file.ReadHuge(pvData,cb);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal,TRUE,&pStm);
}
}
}


//然后,就直接调用OleLoadPicture函数从流中装载图像:
IPicture*pPic;
……
OleLoadPicture(pStm,fstatus.m_size,TRUE,IID_IPicture,(LPVOID*)&pPic));


//由于该函数有时会导致失败,所以应当用SUCCEEDED宏来做一些适当的保护工作,只有在数据装载成功的前提下才能继续下面的图像显示工作:
if(SUCCEEDED(OleLoadPicture(pStm,fstatus.m_size,TRUE,IID_IPicture,(LPVOID*)&pPic)))
{
OLE_XSIZE_HIMETRIChmWidth;
OLE_YSIZE_HIMETRIChmHeight;
pPic->get_Width(&hmWidth);
pPic->get_Height(&hmHeight);
doublefX,fY;
……
fX=(double)pDC->GetDeviceCaps(HORZRES)*(double)hmWidth/((double)pDC->GetDeviceCaps(HORZSIZE)*100.0);
fY=(double)pDC->GetDeviceCaps(VERTRES)*(double)hmHeight/((double)pDC->GetDeviceCaps(VERTSIZE)*100.0);
if(FAILED(pPic->Render(*pDC,0,0,(DWORD)fX,(DWORD)fY,0,hmHeight,hmWidth,-hmHeight,NULL)))
AfxMessageBox("渲染图像失败!");
pPic->Release();
}
else
AfxMessageBox("从流中装载图像失败!");//其中,显示工作主要是由IPicture接口对象的Render函数来完成的,该函数主要用来将图片的指定部分画到指定的设备环境的指定位置。
原型如下:
HRESULTRender(HDChdc,//渲染图像用的设备环境句柄
longx,//在hdc上的水平坐标
longy,//在hdc上的垂直坐标
longcx,//图像宽度
longcy,//图像高度
OLE_XPOS_HIMETRICxSrc,//在源图像上的水平偏移
OLE_YPOS_HIMETRICySrc,//在源图像上的垂直偏移
OLE_XSIZE_HIMETRICcxSrc,//在源图像上水平拷贝的数量
OLE_YSIZE_HIMETRICcySrc,//在源图像上垂直拷贝的数量
LPCRECTprcWBounds//指向目标图元设备环境句柄的指针);

F. c++如何打开一张jpg图片

c++打开jpg方法如下:

1.图片也是属于文件类型的一种,图片属于二进制文件。使用fopen函数的二进制模式“rb”就可以打开。

2.例程:
#include <stdlib.h>
#include <stdio.h>
int main ()
{
FILE * fpPhoto, * fpText, * fpTarget ;
int iRead ;
char szBuf[100] ;

printf ("请输入第一个文件名(jpg):\n") ;
gets (szBuf) ;
fpPhoto = fopen (szBuf, "rb") ;
printf ("请输入第二个文件名(txt):\n") ;
gets (szBuf) ;
fpText = fopen (szBuf, "rb") ;
printf ("请输入目的文件名(jpg):\n") ;
gets (szBuf) ;
fpTarget = fopen (szBuf, "wb") ;

if (!fpPhoto || !fpText || !fpTarget)
{
printf ("打开文件失败!\n") ;
system("pause") ;
return -1 ;
}

while ((iRead = fread (szBuf, 1, sizeof (szBuf), fpPhoto)) > 0)
fwrite (szBuf, 1, iRead, fpTarget) ;
while ((iRead = fread (szBuf, 1, sizeof (szBuf), fpText)) > 0)
fwrite (szBuf, 1, iRead, fpTarget) ;

fclose (fpPhoto) ;
fclose (fpText) ;
fclose (fpTarget) ;
return 0 ;
}

G. 用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;

}

(7)如何用c代码显示jpg图片扩展阅读:

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;

}