一个获取图像范围的问题
要取得这个图片中非黑像素范围,下面的程序为什么总是不对?
tbm:=tbitmap.Create;
tbm.LoadFromFile('1.bmp');
shang:=tbm.Height+1;
you:=-1;
xia:=-1;
zuo:=tbm.Width+1;
for h:=41 to tbm.Height-1 do
begin
//row:=tbm.ScanLine[h];
for w:=0 to tbm.Width-1 do
begin
if tbm.Canvas.Pixels[h,w] <>tbm.Canvas.Pixels[50,50] then
begin
if w <zuo then
zuo:=w;
if w>you then //找出非黑的最大最小像素
you:=w;
if h>xia then
xia:=h;
if h <shang then
shang:=h;
end;
end;
end;
ShowMessage( IntToStr( shang ) + ',' + IntToStr( xia ) + ',' +
IntToStr( zuo ) + ',' + IntToStr( you ) );
[解决办法]
tbm.Canvas.Pixels[h, w]
括号里是先写横坐标, 再写纵坐标, 所以应该改成:
tbm.Canvas.Pixels[w, h]