| Provides indexed access to each line of pixels. property ScanLine[Row: Integer]: Pointer; Des cription ScanLine is used only with DIBs (Device Independent Bitmaps) for image editing tools that do low-level pixel work. |
让我们再看看ScanLine[0]、ScanLine[1]的关系:
| procedure TForm1.Button1Click(Sender: TObject); var BitMap: TBitmap; S: String; begin BitMap := TBitmap.Create; try BitMap.PixelFormat := pf24bit; //24位色,每像素点3个字节 BitMap.Width := 1000; BitMap.Height := 2; FmtStr(S, 'ScanLine[0]:%8x'#13'ScanLine[1]:%8x'#13'ScanLine[1]-ScanLine[0]:%d' , [Integer(BitMap.ScanLine[0]), Integer(BitMap.ScanLine[1]) , Integer(BitMap.ScanLine[1]) - Integer(BitMap.ScanLine[0])]); MessageBox(Handle, PChar(S), 'ScanLine', MB_OK); finally if Assigned(BitMap) then FreeAndNil(BitMap); end; end; |
下面是运行结果:
前两个结果因机器不同而不同,第三个结果很特别,ScanLine[0]与ScanLine[1]之间相差3000=1000像素宽×3字节这很容易理解,但为什么是负数呢?因为BMP图像数据是“按行存放,每行按双字对齐,行按倒序方式存放”的,也就是说屏幕显示的第一行存放在最后,屏幕显示的最后一行存放在前面,所以用ACDSee等看图软件查看尺寸较大的位图时先从下部开始显示就是这个道理。
从上面的结果可以看出TBitmap的图像数据在内存中是按行倒序连续存放的,通过TBitmap.ScanLine[TBitmap.Height-1]可以取得首地址即图像缓冲区地址。接着我们来实践一下,通过直接对图像缓冲区的读写将图像淡出到黑色:
| ScanLine[0]: E90BB8 ScanLine[1]: E90000 ScanLine[1]-ScanLine[0]:-3000 |
| procedure TForm1.Button1Click(Sender: TObject); const FADEOUT_STEP = 24; //淡出衰减值 FIX_WIDTH = 320; FIX_HEIGHT = 200; var BitMap: TBitmap; hWinDC: HDC; flagAgein: Boolean; lpBuffer: PByte; //图像缓冲区指针 begin BitMap := TBitmap.Create; if not Assigned(BitMap) then Exit; try //设置位图格式、宽度、高度 BitMap.PixelFormat := pf24bit; BitMap.Width := FIX_WIDTH; BitMap.Height := FIX_HEIGHT; //设置Form的宽充、高度,便于显示结果 Button1.Visible := false; ClientWidth := FIX_WIDTH; ClientHeight := FIX_HEIGHT; //拷贝图像到Bitmap中 hWinDC := GetDC(0); if (hWinDC<>NULL) then BitBlt(Bitmap.Canvas.Handle, 0, 0, FIX_WIDTH, FIX_HEIGHT, hWinDC, 0, 0, SRCCOPY) else BitBlt(Bitmap.Canvas.Handle, 0, 0, FIX_WIDTH, FIX_HEIGHT, Canvas.Handle, 0, 0, SRCCOPY); repeat MessageBox(Handle, 'Done', 'Fadeout', MB_OK); |
最后补充说明一下:
1、Bitmap图像缓冲区是双节对齐的,如果把例1中的图像宽度改为999,一个像素行还是占3000个字节。
2、目前Bitmap.PixelFormat有pfDevice、pf1bit、pf4bit、pf8bit、pf15bit、pf16bit、pf24bit、pf32bit、pfCustom共9种,不同格式每个像素所占字节数不同,其中pf4bit和pf8bit格式的图像缓冲区保存的为颜色索引号,真正的颜色值在调色板中,pf15bit、pf16bit格式中RGB所占的位数(Bit)不一定是等长的。有兴趣的可查阅相关资料。
浙公网安备 33010602011771号