你看这样行不行,速度比不上Another_eYes的方法快.但可以满足我的要求.
type
TFColor = record
b,g,r:Byte
end;
PFColor =^TFColor;
procedure Gray(srcBmp,DstBmp:TBitMap);
var
x,y: Integer;
Gr: Byte;
incRow,Gap: integer;
DstTmp,SrcTmp: PFColor;
SrcBmInfo,DstBmInfo: TBitmapInfo;
SrcBmHeader,DstBmHeader: TBitmapInfoHeader;
width,height,Size: integer;
SrcBits,DstBits: pointer;
mDC: Integer;
begin
Width:=SrcBmp.Width;
Height:=SrcBmp.Height;
Size:=((Width*3)+(Width mod 4))*Height;
incRow:=((Width*3)+(Width mod 4));
Gap:=Width mod 4;
DstBmp.Assign(SrcBmp);//这句可要可不要,看情况定
getMem(srcBits,size);
getMem(DstBits,size);
with SrcbmHeader do begin
biSize:=SizeOf(SrcbmHeader);
biWidth:=Width;
biHeight:=-Height;
biPlanes:=1;
biBitCount:=24;
biCompression:=BI_RGB;
end;
SrcbmInfo.bmiHeader:=srcbmHeader;
with DstbmHeader do begin
biSize:=SizeOf(DstbmHeader);
biWidth:=Width;
biHeight:=-Height;
biPlanes:=1;
biBitCount:=24;
biCompression:=BI_RGB;
end;
DstbmInfo.bmiHeader:=DstbmHeader;
try
mDC:=GetDC(0);
GetDIBits(mDC,srcBmp.Handle,0,Height,srcBits,srcbmInfo,DIB_RGB_COLORS);
DstTmp:=DstBits;
for y:=0 to Height-1 do begin
for x:=0 to Width-1 do begin
SrcTmp:=pointer(integer(SrcBits)+(y*IncRow+x*3));
Gr := HiByte(SrcTmp^.r * 77 + SrcTmp^.g * 151 + SrcTmp^.b * 28);
DstTmp^.r:=Gr;
DstTmp^.g:=Gr;
DstTmp^.b:=Gr;
Inc(DstTmp);
end;
DstTmp:=Pointer(Integer(DstTmp)+Gap);
end;
SetDIBits(mDC,DstBmp.Handle,0,Height,DstBits,DstbmInfo,DIB_RGB_COLORS);
ReleaseDC(0,mDC);
finally
freemem(SrcBits);
freemem(DstBits);
end;
end;
其中GetDIBBits和SetDIBBits函数可以看API帮助.这个方法与显存有关,
如果你的显存不够大,大图象就处理不,600X600的图象需要
600x600x3=1080000Bytes的显存.