5个回答
展开全部
/*
你还是听听高先生的建议吧,
前面我给你的回答除了使用了c++的‘
//’注释以外,使用纯C语言编写的。
你如果仅仅是想完你说的目的,
虽然一小段代码可以就完成但是却不太容易读懂 。
图像右下角坐标为(0,0)
*/
#include<stdio.h>
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
*/
你还是听听高先生的建议吧,
前面我给你的回答除了使用了c++的‘
//’注释以外,使用纯C语言编写的。
你如果仅仅是想完你说的目的,
虽然一小段代码可以就完成但是却不太容易读懂 。
图像右下角坐标为(0,0)
*/
#include<stdio.h>
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
*/
展开全部
你是要获取其中任意点像素的颜色信息吗?
你可以用你的位图建立一个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.
你可以用你的位图建立一个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.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
楼主,还是先把bmp的格式,搞清楚吧
那样的话,你自己都可以写出这个功能的函数了啊。
补充:
楼上的回答,都是用c语言写的啊!
那样的话,你自己都可以写出这个功能的函数了啊。
补充:
楼上的回答,都是用c语言写的啊!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
msdn中看Device-Independent Bitmaps部分
既.bmp文件格式
既.bmp文件格式
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
24位?256位?16位?还是全都要?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询