首页 > 编程知识 正文

关于c语言boor图片的信息

时间:2024-04-24 11:43:23 阅读:334170 作者:YWVO

本文目录一览:

c语言,怎样读取一个BMP图片?

#ifndef IMAGE_H

#define IMAGE_H

void image_info(FILE* file);

void image_save(FILE *file);

void image_gray();

void image_binarization();

void image_opposite();

void image_channel(); //抽取RGB通道

void image_bright();//改变图像亮度

typedef struct BMP

{

//14字节

unsigned short bfType; //文件标识 2字节 必须为BM

unsigned int bfSize; //文件大小 4字节

unsigned short bfReserved1; //保留,每字节以"00"填写 2字节

unsigned short bfReserved2; //同上 2字节

unsigned int bfOffBits; //记录图像数据区的起始位置(图象数据相对于文件头字节的偏移量)。 4字节

//40字节

unsigned int biSize; //表示本结构的大小 4字节

int biWidth; //位图的宽度 4字节

int biHeight; //位图的高度 4字节

unsigned short biPlanes; //永远为1 , 2字节

unsigned short biBitCount; //位图的位数 分为1 4 8 16 24 32 2字节

unsigned int biCompression; //压缩说明 4字节

unsigned int biSizeImage; //表示位图数据区域的大小以字节为单位 4字节

int biXPelsPerMeter; //用象素/米表示的水平分辨率 4字节

int biYPelsPerMeter; //用象素/米表示的垂直分辨率 4字节

unsigned int biClrUsed; //位图使用的颜色索引数 4字节

unsigned int biClrImportant; //对图象显示有重要影响的颜色索引的数目 4字节

} BMP;

int line_byte;

unsigned char *imagedata;

extern BMP bmp;

extern int line_byte;

extern unsigned char *imagedata;

#endif

//image_rw.c文件

#includestdio.h

#includestdlib.h

#include"image.h"

void image_info(FILE *file)

{

int times=3; //输入文件名次数。

char bmp_name[10]; //文件名

printf("nplease enter a file name for reading:");

do

{

if (times3)

{

printf("nplease enter a file name for reading again:");

}

fflush(stdin);

gets(bmp_name);

//printf("n%s",bmp_name);

file=fopen(bmp_name,"rb+"); //打开一个文件进行读写操作。

--times;

if (file==NULL)

{

printf("nerror opening %s for reading! ",bmp_name);

}

else

{

break;

}

}

while(times!=0);

if (times==0)

{

printf("nsorry, shutdown!");

exit(1);

}

//读取图像信息

fseek(file,0L,0); //读取图像文件类型

fread(bmp,sizeof(BMP),1,file);

printf("n bmp tpye: %u",bmp.bfType);

printf("n bmp size: %u",bmp.bfSize);

printf("n bmp reserved1: %u",bmp.bfReserved1);

printf("n bmp reserved2: %u",bmp.bfReserved2);

printf("n bmp offBits: %u",bmp.bfOffBits);

printf("n bmp bisize: %u",bmp.biSize);

printf("n bmp biWidth: %d",bmp.biWidth);

printf("n bmp biHeight: %d",bmp.biHeight);

printf("n bmp biplans: %u",bmp.biPlanes);

printf("n bmp biBitCount: %u",bmp.biBitCount);

printf("n bmp biCompression: %u",bmp.biCompression);

printf("n bmp biSizeImage: %u",bmp.biSizeImage);

printf("n bmp biXPelsPerMeter: %d",bmp.biXPelsPerMeter);

printf("n bmp biYPelsPerMeter: %d",bmp.biYPelsPerMeter);

printf("n bmp biClrUsed: %u",bmp.biClrUsed);

printf("n bmp biClrImportant: %un",bmp.biClrImportant);

line_byte=(bmp.biWidth*bmp.biBitCount/8+3)/4*4; //获得图像数据每行的数据个数

//printf("dfsa%u",bmp.line_byte);

//bmp.imagedata=NULL;

imagedata=(unsigned char*)malloc(bmp.biSizeImage);

fseek(file,(long)bmp.bfOffBits,0);

fread(imagedata,sizeof(unsigned char),bmp.biSizeImage,file);

fclose(file);

}

//保存图像

void image_save(FILE *file)

{

int times=3; //输入文件名次数。

char bmp_name[10]; //文件名

//int i; //记录数据区个数

printf("nplease enter a file name for writeing:");

do

{

if (times3)

{

printf("nplease enter a file name for writeing again:");

}

fflush(stdin);

gets(bmp_name);

printf("n%s",bmp_name);

file=fopen(bmp_name,"wb+"); //打开一个文件进行读写操作。

--times;

if (file==NULL)

{

printf("nerror opening %s for writing",bmp_name);

}

else

{

break;

}

}

while(times!=0);

if (times==0)

{

printf("nsorry, shutdown!");

exit(1);

}

//写文件头

printf("n%s",bmp_name);

fseek(file,0L,0); //图像文件类型

fwrite((bmp.bfType),sizeof(short),1,file);

printf("n bmp tpye: %d",bmp.bfType);

fseek(file,2L,0); //图像文件大小

fwrite((bmp.bfSize),sizeof(int),1,file);

printf("n bmp size: %d",bmp.bfSize);

fseek(file,6L,0); //图像文件保留字1

fwrite((bmp.bfReserved1),sizeof(short),1,file);

printf("n bmp reserved1: %d",bmp.bfReserved1);

fseek(file,8L,0); //图像文件保留字2

fwrite((bmp.bfReserved2),sizeof(short),1,file);

printf("n bmp reserved2: %d",bmp.bfReserved2);

fseek(file,10L,0);//数据区的偏移量

fwrite((bmp.bfOffBits),sizeof(short),1,file);

printf("n bmp offBits: %d",bmp.bfOffBits);

fseek(file,14L,0);//文件头结构大小

fwrite((bmp.biSize),sizeof(int),1,file);

printf("n bmp bisize: %d",bmp.biSize);

fseek(file,18L,0);//图像的宽度

fwrite((bmp.biWidth),sizeof(int),1,file);

printf("n bmp biWidth: %d",bmp.biWidth);

fseek(file,22L,0);//图像的高度

fwrite((bmp.biHeight),sizeof(int),1,file);

printf("n bmp biHeight: %d",bmp.biHeight);

fseek(file,24L,0);//图像的面数

fwrite((bmp.biPlanes),sizeof(short),1,file);

printf("n bmp biplans: %d",bmp.biPlanes);

fseek(file,28L,0);//图像一个像素的字节数

fwrite((bmp.biBitCount),sizeof(short),1,file);

printf("n bmp biBitCount: %d",bmp.biBitCount);

fseek(file,30L,0);//图像压缩信息

fwrite((bmp.biCompression),sizeof(short),1,file);

printf("n bmp biCompression: %d",bmp.biCompression);

fseek(file,34L,0);//图像数据区的大小

fwrite((bmp.biSizeImage),sizeof(int),1,file);

printf("n bmp biSizeImage: %d",bmp.biSizeImage);

fseek(file,38L,0);//水平分辨率

fwrite((bmp.biXPelsPerMeter),sizeof(int),1,file);

printf("n bmp biXPelsPerMeter: %d",bmp.biXPelsPerMeter);

fseek(file,42L,0);//垂直分辨率

fwrite((bmp.biYPelsPerMeter),sizeof(int),1,file);

printf("n bmp biYPelsPerMeter: %d",bmp.biYPelsPerMeter);

fseek(file,46L,0);//颜色索引数

fwrite((bmp.biClrUsed),sizeof(int),1,file);

printf("n bmp biClrUsed: %d",bmp.biClrUsed);

fseek(file,50L,0);//重要颜色索引数

fwrite((bmp.biClrImportant),sizeof(int),1,file);

printf("n bmp biClrImportant: %dn",bmp.biClrImportant);

fseek(file,(long)(bmp.bfOffBits),0);

fwrite(imagedata,sizeof(unsigned char),bmp.biSizeImage,file);

fclose(file);

}

//pixProcess.c文件

#includestdio.h

#includestdlib.h

#includemath.h

#include"image.h"

//灰度化

void image_gray()

{

int i,j;

unsigned char tmp;

for (i=0;ibmp.biHeight;i++)

{

for (j=0;jline_byte/3;j++)

{

tmp=0.11*(*(imagedata+i*line_byte+j*3+0))+0.59*(*(imagedata+i*line_byte+j*3+1))+0.3*(*(imagedata+i*line_byte+j*3+2));

imagedata[i*line_byte+j*3+0]=tmp;

imagedata[i*line_byte+j*3+1]=tmp;

imagedata[i*line_byte+j*3+2]=tmp;

//printf("nnidsfh%d %d",i,j);

}

}

}

//二值化

void image_binarization()

{

int i,j;

for (i=0;ibmp.biHeight;i++)

{

for (j=0;jline_byte;j++)

{

if ((*(imagedata+i*line_byte+j))128)

{

imagedata[i*line_byte+j]=0;

}

else

{

imagedata[i*line_byte+j]=255;

}

}

}

}

void image_opposite() //反相

{

int i,j;

for (i=0;ibmp.biHeight;i++)

{

for (j=0;jline_byte;j++)

{

imagedata[i*line_byte+j]=abs(255-imagedata[i*line_byte+j]);

}

}

}

void image_channel() //抽取RGB通道

{

int i,j;

char rgb;

printf("nplease enter a char(r/g/b): ");

fflush(stdin);

scanf("%c",rgb);

if (rgb=='b')

{

for (i=0;ibmp.biHeight;i++)

{

for (j=0;jline_byte/3;j++)

{

imagedata[i*line_byte+3*j+1]=0;

imagedata[i*line_byte+3*j+2]=0;

}

}

}

else if(rgb=='g')

{

for (i=0;ibmp.biHeight;i++)

{

for (j=0;jline_byte/3;j++)

{

imagedata[i*line_byte+3*j]=0;

imagedata[i*line_byte+3*j+2]=0;

}

}

}

else

{

for (i=0;ibmp.biHeight;i++)

{

for (j=0;jline_byte/3;j++)

{

imagedata[i*line_byte+3*j]=0;

imagedata[i*line_byte+3*j+1]=0;

}

}

}

}

void image_bright()//改变图像亮度

{

int level;

int i,j;

printf("n please enter the level of brightness[-255 to 255] :");

fflush(stdin);

scanf("%d",level);

for (i=0;ibmp.biHeight;i++)

{

for (j=0;jline_byte;j++)

{

if (level=0)

{

if ((imagedata[i*line_byte+j]+level)255)

imagedata[i*line_byte+j]=255;

else

imagedata[i*line_byte+j]+=level;

}

else

{

if ((imagedata[i*line_byte+j]-abs(level))0)

imagedata[i*line_byte+j]=0;

else

imagedata[i*line_byte+j]+=level;

}

}

}

}

//void image_create() //创建一幅24位BMP图像文件。

//{

//main.c文件

#includestdio.h

#includestdlib.h

#includestring.h

#includeconio.h

#include"image.h"

BMP bmp;

int main()

{

FILE *file=NULL;

int choose;

char gono;

do

{

image_info(file); //imagedata已经分配了动态内存,但是没有释放

printf("n 1.image_opposite");

printf("n 2.image_gray");

printf("n 3.image_binarization");

printf("n 4.image_channel");

printf("n 5.image_brightness");

//printf("6.image_opposite");

//printf("7.image_opposite");

printf("nchoose your options:");

fflush(stdin);

scanf("%d",choose);

switch(choose)

{

case 1:

image_opposite();

image_save(file);

free(imagedata);

break;

case 2:

image_gray();

image_save(file);

free(imagedata);

break;

case 3:

image_binarization();

image_save(file);

free(imagedata);

break;

case 4:

image_channel();

image_save(file);

free(imagedata);

break;

case 5:

image_bright();

image_save(file);

free(imagedata);

break;

default:

printf("n wrong choose!");

}

printf("nlet's go on?(y/n):");

fflush(stdin);

scanf("%c",gono);

if (gono=='n')

{

printf("nbye bye!");

break;

}

}

while(1);

return 0;

}

c语言中如何导入图片?

具体操作步骤如下:

一、首先现在图片取模软件找到软件快捷方式,点击打开软件。

二、现在进入到了取模软件,点击“载入图片”,现在就可以进行添加图片了。

三、选择需要添加的图片,点击选择图片,然后单击“打开”。

四、现在点击“设置”进入图片参数设置。

五、现在有输出格式,取模方式,图片截取范围一些参数设置进行设置。

六、如果确认无误,直接单击参数确认就可以了。

七、然后进行数据保存,点击数据保存。

八、选择文件保存路径,点击“保存”就可以了。

C语言是一门面向过程、抽象化的通用程序设计语言,广泛应用于底层开发。C语言是仅产生少量的机器语言以及不需要任何运行环境支持便能运行的高效率程序设计语言。

但仍然保持着跨平台的特性,以一个标准规格写出的C语言程序可在包括一些类似嵌入式处理器以及超级计算机等作业平台的许多计算机平台上进行编译。C语言是一门面向过程的计算机编程语言,与C++、Java等面向对象编程语言有所不同。

C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、仅产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。C语言描述问题比汇编语言迅速,工作量小、可读性好,易于调试、修改和移植,而代码质量与汇编语言相当。

参考资料来源:百度百科-c语言

怎么样在c语言中显示bmp图片,我要完整正确的程序,急!

lz  你好

c语言要显示bmp位图需要使用win32的api , 具体如下:

BOOL BitBlt(

  HDC hdcDest, // 位图显示目标设备环境中

  int nXDest,  // 位图显示在客户区的x坐标

  int nYDest,  // 位图显示在客户区的y坐标

  int nWidth,  // 位图显示的宽度

  int nHeight, // 位图显示的长度

  HDC hdcSrc,  // 源设备环境(包含需要显示的bmp位图)

  int nXSrc,   // 在当前位图中显示的开始x位置

  int nYSrc,   // 在当前位图中显示的开始y位置

  DWORD dwRop  // 映射模式

);

以下是源代码:

//显示bmp位图

#includewindows.h

#include"resource.h"

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

void DrawBrick(); 

int WINAPI WinMain(HINSTANCE hInstance,

   HINSTANCE hPrevInstance,

                   PSTR szCmdLine,

   int iCmdShow)

{

static TCHAR szAppName[] = TEXT("Bmp");

HWND hwnd;

MSG msg;

WNDCLASS wndclass;

wndclass.style = CS_HREDRAW | CS_VREDRAW;

wndclass.lpfnWndProc = WndProc;

wndclass.cbClsExtra = 0;

wndclass.cbWndExtra = 0;

wndclass.hInstance = hInstance;

wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);

wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);

wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);

wndclass.lpszMenuName = NULL;

wndclass.lpszClassName = szAppName;

if(!RegisterClass(wndclass))

{

MessageBox(NULL, TEXT("This program requires Windows NT!"),

szAppName, MB_ICONERROR);

return 0;

}

hwnd = CreateWindow(szAppName,

TEXT("Bmp Demo"),

WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT,

CW_USEDEFAULT,

754,

566,

NULL,

NULL,

hInstance,

NULL);

ShowWindow(hwnd, iCmdShow);

UpdateWindow(hwnd);

while(GetMessage(msg, NULL, 0, 0))

{

TranslateMessage(msg);

DispatchMessage(msg);

}

return msg.wParam;

}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)

{

static HBITMAP hBitmap; //位图句柄  标示位图

static int cxBitmap, cyBitmap; //位图的长宽

BITMAP bitmap;

HDC hdc, hdcMem;

HINSTANCE hInstance;

PAINTSTRUCT ps;

switch(message)

{

case WM_CREATE:

hInstance = ((LPCREATESTRUCT)lParam)-hInstance; //获取窗口的实例句柄

hBitmap = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP1)); //将位图加载到内存中

GetObject(hBitmap, sizeof(BITMAP), bitmap);

cxBitmap = bitmap.bmWidth;//获取位图的长

cyBitmap = bitmap.bmHeight;//获取位图的宽

return 0 ;

case WM_PAINT:

hdc = BeginPaint(hwnd, ps);

hdcMem = CreateCompatibleDC(hdc);//创建一个兼容于hdc设备环境描述表的hdcMem  主要是用于在内存中截图

SelectObject(hdcMem, hBitmap);   //将位图选到hdcMem中

BitBlt(hdc, -1, -1, cxBitmap, cyBitmap, hdcMem, 0, 0, SRCCOPY);//绘制bmp位图

DeleteDC(hdcMem);

EndPaint(hwnd, ps);

return 0;

case WM_DESTROY:

DeleteObject(hBitmap);

PostQuitMessage(0);

return 0;

}

return DefWindowProc(hwnd, message, wParam, lParam);

}

程序运行效果:

希望能帮助你哈

ps:

附件是整个工程 , 用vs2008创建的项目 , 里面包含相应资源

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。