Bitmap如何用scanline读取每一个像素的颜色(TColor型),并保存到一个数组中???
解决方案 »
- 如何将richedit里面排好版的文字信息保存为位图文件?参与有分!!!
- 请教:Delphi 6.0怎么调用VS 2003的WebServices
- SQL中字段定义varchar(4000),为什么在qrery中,加入字段时size只有255
- 问一个关于虚方法的问题。自己做作业,不过有错,请大家帮我看看啊
- Locate气死我了
- 如何设置QReport只能预览不能打印,即将打印的按钮隐去?
- fastreport如何用按纽进行预览和打印?
- 9527请进
- 我发现delphi开发跟数据库有关的应用程序时,他必须要在已有数据库并建立数据源的机器上开发
- 求教,在form:=Tform.create(application) 时报'form resource not found'
- 一个窗体的小问题???
- 关于USB问题请教 急!在线等
x,y : Integer;
BitMap1,BitMap2 : TBitMap;
P : PByteArray;
mArray: array of byte;
mRect: TRect;
begin
BitMap1 := TBitMap.create;
BitMap2 := TBitMap.Create;
try
BitMap1.LoadFromFile('d:\test.bmp'); SetLength(mArray,BitMap1.Height * BitMap1.Width * 4);
//从bitmap中取至数组
for y := 0 to BitMap1.Height -1 do
begin
P := BitMap1.ScanLine[y];
for x := 0 to (BitMap1.Width -1)*4 do
mArray[y*BitMap1.Width*4+x] := P[x];
end; //从数组中写到另一bitmap
BitMap2.Height := BitMap1.Height;
BitMap2.Width := BitMap1.Width;
BitMap2.PixelFormat := BitMap1.PixelFormat;
for y := 0 to BitMap1.Height -1 do
begin
p := BitMap2.ScanLine[y];
for x := 0 to (BitMap2.Width -1)*4 do
p[x] := mArray[y*BitMap1.Width*4+x];
end; //输出原图至窗口的左上角
Canvas.CopyRect(BitMap1.Canvas.ClipRect,BitMap1.Canvas,BitMap1.Canvas.ClipRect); //在原图下方输出生成图
mRect := BitMap2.Canvas.ClipRect;
mRect.Top := mRect.Top + BitMap1.Height;
mRect.Bottom := mRect.Bottom + BitMap1.Height;
Canvas.CopyRect(mRect,BitMap2.Canvas,BitMap2.Canvas.ClipRect);
finally
BitMap1.Free;
BitMap2.Free;
end;
end;
type
PRGBArray = ^TRGBArray;
TRGBArray = array[0..PixelCountMax-1] of TRGBTriple;
var Bitmap:TBitmap;
p : PRGBArray;
x, y: integer;
c : array of array of TColor;
begin
Bitmap := TBitmap.Create;
try
Bitmap.LoadFromFile('c:\1.bmp');
SetLength(c, Bitmap.Width, Bitmap.Height);
Bitmap.PixelFormat := pf24bit;
for y:=0 to Bitmap.Height-1 do
begin
p := PRGBArray(Bitmap.ScanLine[y]);
for x:=0 to Bitmap.Width-1 do
begin
//保存在数组c内
c[x,y]:= RGB(p[x].rgbtRed,p[x].rgbtGreen,p[x].rgbtBlue);
end;
end;
for x:=0 to bitmap.Width-1 do
for y:=0 to Bitmap.Height-1 do
Canvas.Pixels[x, y]:=c[x,y];
finally
Bitmap.Free;
end;
end;
Procedure ReadBMPData(BMPFile:String; ABMP:TBitMap; Px,Py:LongInt);
Var AFile:File Of Byte;
FileHead:TBITMAPFILEHEADER;
InfoHead:TBITMAPINFOHEADER;
BytesPerLine:LongInt; Bw,Bh:Integer; //源图像大小
W,H:Integer; //目标图像大小
X1,Y1,X2,Y2:Integer; //复制区域 I:Integer;
Begin
If ABMP=Nil Then Exit;
If Not FileExists(BMPFile) Then Exit; {$I-}
AssignFile(AFile,BMPFile);
FileMode := 0;
Reset(AFile); BlockRead(AFile,FileHead,SizeOf(TBITMAPFILEHEADER));
BlockRead(AFile,InfoHead,SizeOf(TBITMAPINFOHEADER));
If ((InfoHead.biBitCount<>24) OR (InfoHead.biCompression<>0)) Then
Begin
//必须是 24 位 非压缩文件格式
CloseFile(AFile);
Exit;
End; //每条扫描线数据长度
BytesPerLine:=(InfoHead.biWidth* InfoHead.biBitCount+31) Div 8;
BytesPerLine:=(BytesPerLine Div 4)*4; //目标相关信息
Bw:=InfoHead.biWidth; Bh:=InfoHead.biHeight;
W:=ABmp.Width; H:=ABmp.Height; //坐标变换
X1:=Px; If X1<0 Then X1:=0 Else If X1>Bw-1 Then X1:=Bw-1;
Y1:=Py; If Y1<0 Then Y1:=0 Else If Y1>Bh-1 Then Y1:=Bh-1;
X2:=Px+W-1; If X2<0 Then X2:=0 Else If X2>Bw-1 Then X2:=Bw-1;
Y2:=Py+H-1; If Y2<0 Then Y2:=0 Else If Y2>Bh-1 Then Y2:=Bh-1; //读取数据到 BMP 对象
For I:=Y1 To Y2 Do
Begin
Seek(AFile,FileHead.bfOffBits+I*BytesPerLine+X1*3);
BlockRead(AFile,ABMP.ScanLine[(H-1)-(I-Y1)]^,(X2-X1)*3);
End; CloseFile(AFile);
{$I+}
End;