已有一个bmp图片用标准c获取其任意点(给定坐标)像素信息?

要用标准c来写,对不起了各位兄弟。
2024-12-19 02:02:57
推荐回答(5个)
回答1:

/*
你还是听听高先生的建议吧,
前面我给你的回答除了使用了c++的‘
//’注释以外,使用纯C语言编写的。
你如果仅仅是想完你说的目的,
虽然一小段代码可以就完成但是却不太容易读懂 。
图像右下角坐标为(0,0)
*/

#include
int main()
{
int width,height,x,y;
unsigned short bitCount;
int offbits;
int bitPerLine;
unsigned char data;
FILE* bmpfp = fopen("E:\\风景\\风景1.bmp","rb");
fseek(bmpfp,18,SEEK_SET);
fread(&width,sizeof(int),1,bmpfp);
fread(&height,sizeof(int),1,bmpfp);
printf("width : %d , height : %d\n",width,height);
fseek(bmpfp,2,SEEK_CUR);
fread(&bitCount,sizeof(bitCount),1,bmpfp);
fseek(bmpfp,10,SEEK_SET);
fread(&offbits,sizeof(int),1,bmpfp);

if(bitCount==24){
bitPerLine = ( (width*3)%4==0 ) ? width*3 : ( (width*3)/4 )*4 + 4;
while(1){
printf("请输出坐标:");
scanf("%d%d",&x,&y);
if(x>width||y>height) return 0;
fseek(bmpfp, 18 + offbits + bitPerLine * y + 3*x , SEEK_SET);
fread(&data,sizeof(data),1,bmpfp);
printf("该点蓝色分量:%d",data);
fread(&data,sizeof(data),1,bmpfp);
printf("该点绿色分量:%d",data);
fread(&data,sizeof(data),1,bmpfp);
printf("该点红色分量:%d\n",data);
}
}else{
printf("不是真彩位图!");
}
}

/*

运行结果:

width : 700 , height : 382
请输出坐标:0 0
该点蓝色分量:68该点绿色分量:82该点红色分量:80

*/

回答2:

你是要获取其中任意点像素的颜色信息吗?
你可以用你的位图建立一个Bitmap对象,然后用其GetPixel函数即可,详情请参考...

Bitmap::GetPixel(x, y, color)
The GetPixel method gets the color of a specified pixel in this bitmap.

Status GetPixel(
INT x,
INT y,
Color* color
);
Parameters
x
[in] Integer that specifies the x-coordinate (column) of the pixel.
y
[in] Integer that specifies the y-coordinate (row) of the pixel.
color
[out] Pointer to a Color object that receives the color of the specified pixel.
Return Values
If the method succeeds, it returns Ok, which is an element of the Status enumeration.

If the method fails, it returns one of the other elements of the Status enumeration.

Remarks
Depending on the format of the bitmap, GetPixel might not return the same value as was set by SetPixel. For example, if you call SetPixel on a Bitmap object whose pixel format is 32bppPARGB, the pixel's RGB components are premultiplied. A subsequent call to GetPixel might return a different value because of rounding. Also, if you call SetPixel on a Bitmap object whose color depth is 16 bits per pixel, information could be lost during the conversion from 32 to 16 bits, and a subsequent call to GetPixel might return a different value.

Example Code [C++]
The following example creates a Bitmap object based on a JPEG file. The code calls the GetPixel method to obtain the color of a pixel in the bitmap and then fills a rectangle with the retrieved color.

VOID Example_GetPixel(HDC hdc)
{
Graphics graphics(hdc);

// Create a Bitmap object from a JPEG file.
Bitmap myBitmap(L"Climber.jpg");

// Get the value of a pixel from myBitmap.
Color pixelColor;
myBitmap.GetPixel(25, 25, &pixelColor);

// Fill a rectangle with the pixel color.
SolidBrush brush(pixelColor);
graphics.FillRectangle(&brush, Rect(0, 0, 100, 100));
}

Header: Declared in Gdiplusheaders.h; include Gdiplus.h.

回答3:

楼主,还是先把bmp的格式,搞清楚吧

那样的话,你自己都可以写出这个功能的函数了啊。

补充:
楼上的回答,都是用c语言写的啊!

回答4:

msdn中看Device-Independent Bitmaps部分
既.bmp文件格式

回答5:

24位?256位?16位?还是全都要?