图象颜色 ( 积分: 200 )

  • 主题发起人 主题发起人 jingtao
  • 开始时间 开始时间
J

jingtao

Unregistered / Unconfirmed
GUEST, unregistred user!
有一24BIT的BMP图片.如何根据文件内容取的该图片某点的颜色?
 
有一24BIT的BMP图片.如何根据文件内容取的该图片某点的颜色?
 
这个是取得当前光标处的颜色
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
 DC:HDC;
 X,Y:Integer;
begin
 X:=Mouse.CursorPos.X;
 Y:=Mouse.CursorPos.Y;
 if Key<>VK_Return then Exit;
  DC:=GetDC(0);
 Color:=GetPixel(DC,X,Y);
end;
 
这个点是固定的还是根据内容而变化的?

下面是获得一个bmp图象1,1像素的RGB颜色
var
bmp:Tbitmap;
pcolor:Tcolor;
r,g,b:byte;
begin
pcolor:=bmp.Canvas.Pixels[1,1];
r :=GetRValue(pcolor);
g :=GetGValue(pcolor);
b :=GetBValue(pcolor);
end;
 
我的意思是根据文件内容.不能使用GetPixel函数.bmp.Canvas.Pixels其实也是间接使用了GetPixel.
 
......没看懂你是什么意思。。。。举个例子
 
就是根据BMP文件格式和内容计算出某点的颜色.
 
http://www.2ccc.com/article.asp?articleid=2062
《Delphi数字图像处理及高级应用》

看看这个呢[:)][:)]
 
从bmp文件中读取某点的颜色值, 随手写的, 也许有错, 不过思路还是可以看懂的:

type
TBmInfo = packed record
InfoHead: TBitmapInfoHeader;
ColorTbl: array [0..255] of TRGBQuad;
end;

function ReadPixelFromFile(AFileName: string; X, Y: Integer): TColor;
const
Bits: array [0..7] of Byte = ($80, $40, $20, $10, $8, $4, $2, $1);
var
FHead: TBitmapFileHeader; // bmp文件头
BmInfo: TBmInfo; // bmp图片头, 包含配色表
Offset: Int64; // x,y对应文件中的偏移
Fid: Integer; // 文件句柄
Len: Integer; // 每行占用字节数
XOff: Integer; // x所处字节离行首偏移量
Dt: array [0..3] of Byte; // 读取到的一个点的数据, 最长4字节
begin
if not FileExists(AFileName) then
begin
result := clNone;
exit;
end;
Fid := FileOpen(AFileName, fmOpenRead);
FileRead(Fid, FHead, sizeof(FHead));
FileRead(Fid, BmInfo, sizeof(BmInfo));
{计算x, y点在文件中的起始位置}
Len := BytesPerScanline(bminfo.InfoHead.biWidth, bminfo.InfoHead.biBitCount, 32);
XOff := x * bminfo.InfoHead.biBitCount div 8;
if BmInfo.InfoHead.biHeight < 0 then // 倒置的bmp格式, 0, 0点对应图片左上角
offset := FHead.bfOffBits + y * Len + XOff
else // 正常bmp格式, 0,0对应图片左下角
offset := FHead.bfOffBits+ (BmInfo.InfoHead.biHeight - y - 1) * Len + XOff;
{读取颜色信息}
FileSeek(Fid, offset, 0);
FileRead(Fid, Dt, (bminfo.InfoHead.biBitCount + 7) div 8); // 根据图片分辨率读取1~4字节
FileClose(Fid);
{转换颜色信息->TColor}
case bminfo.InfoHead.biBitCount of
1: // 单色
if Dt[0] and Bits[x mod 8] = 0 then
with bminfo.ColorTbl[0] do
result := RGB(rgbRed, rgbGreen, rgbBlue)
else
with bminfo.ColorTbl[1] do
result := RGB(rgbRed, rgbGreen, rgbBlue);
4: // 16色
with bminfo.ColorTbl[Dt[0] shr (4 * (1 - x mod 2)) and $F] do
result := RGB(rgbRed, rgbGreen, rgbBlue);
8: // 256色
with bminfo.ColorTbl[Dt[0]] do
result := RGB(rgbRed, rgbGreen, rgbBlue);
16: // 16位色, 每种颜色5bit
result := RGB(Dt[0] shr 2 and $1F, (Dt[0] and 3) shl 3 + (Dt[1] shr 5) and $1F, Dt[1] and $1F);
24, 32: // 真彩
result := RGB(Dt[2], Dt[1], Dt[0]);
end;
end;
 
谢谢Another_eYes.
我现在是想写一个代替GetPixel的函数.目前的思路是先把屏幕保存下来,然后计算出某点坐标.不知道您是否有更好的建议?
 
哎 超出我的能力范围了,只能关注关注^_^
 
多人接受答案了。
 
后退
顶部