将BMP图像数据存入数组 ( 积分: 100 )

  • 主题发起人 主题发起人 nethero
  • 开始时间 开始时间
N

nethero

Unregistered / Unconfirmed
GUEST, unregistred user!
各位GGJJDDMM,紧急求助!我想把BMP图像的数据存入一个数组里,如函数所示:
function MakeBmp(pImgData[128][128] char,*filename:string):boolean;这个函数完成的功能就是输入一个BMP文件名,返回它的数据区指针。
有劳各位了!100分奉送。
 
各位GGJJDDMM,紧急求助!我想把BMP图像的数据存入一个数组里,如函数所示:
function MakeBmp(pImgData[128][128] char,*filename:string):boolean;这个函数完成的功能就是输入一个BMP文件名,返回它的数据区指针。
有劳各位了!100分奉送。
 
用这么麻烦吗?
如果存在一个bmp,直接可以取到它的数据指针呀。
直接可以操作它的数据。
 
function TFrmMain.GetBmpDataPoint(Bmp: TBitmap): Pointer;
var
bh: ^windows.Tbitmap;
lHandle: THandle;
begin
Bmp.Handletype := bmDIB; //必须为设备无关位图才有内存指针
try
New(bh);
lHandle := Bmp.Handle;
getObject(lHandle,Sizeof(Windows.Tbitmap), bh);
result := bh.bmBits; //得到位置,注意为逆行序存放的.
finally
dispose(bh);
end;
end;
 
如果你想把BMP数据存入数组,
可用ScanLine。速度很快。
 
谢谢还是朋友。用Scanline能把图像数据存入我说的image[128][128]数组,你能给段具体的代码吗?万分感谢,qq号是25923678,可以多多交流,对图像这块我不太熟。
 
nethero兄弟,
关于图像,你得先看一些资料才行呀。

图像数据存入我说的image[128][128]数组,存倒是可以存,
如果图像是RGB图,要看图像是几位的,如果是8位图,当然可以。如果是24
位,就需要三个Image数组的长度。

而且如果是索引图,转成RGB好了,呵。因为用ScanLine得到的不是象素值,而是索引
值。
 
8位:
var
i, j: Integer;
p: PByteArray;
begin
for i := 0 to Bmp.Height - 1 do
begin
p := Bmp.ScanLine;
for j := 0 to Bmp.Width - 1 do
begin
Image[j] := p[j];
end;
end;
end;
 
还是朋友,我这里有一段c的例子,你看看,我觉得能满足我的要求(我的图像是黑白的),不过,怎么把它翻译成delphi呢:
int MakeBmp(unsigned char pImgData[128][128], char *filename)
{
unsigned char head[1078]={
//file header
0x42,0x4d,//file type
0x36,0x44,0x00,0x00, //file size***
0x00,0x00, //reserved
0x00,0x00,//reserved
0x36,0x4,0x00,0x00,//head byte***
//infoheader
0x28,0x00,0x00,0x00,//struct size
0x80,0x00,0x00,0x00,//map width***
0x80,0x00,0x00,0x00,//map height***
0x01,0x00,//must be 1
0x08,0x00,//color count***
0x00,0x00,0x00,0x00, //compressionx
0x00,0x40,0x00,0x00,//data size***
0x00,0x00,0x00,0x00, //dpix
0x00,0x00,0x00,0x00, //dpiy
0x00,0x00,0x00,0x00,//color usyumed
0x00,0x00,0x00,0x00,//color important
};

int i;
int j=0;
for (i=54;i<1078;i=i+4) //调色板数据
{
head=head[i+1]=head[i+2]=j;
head[i+3]=0;
j++;
}
CFile ff;
int succ=ff.Open(filename,CFile::modeCreate | CFile::modeWrite );
if( !succ )
return ERR_OPERATION;;//
ff.Write( head,1078*sizeof(char) );
ff.Close();
succ=ff.Open(filename,CFile::shareDenyWrite | CFile::modeReadWrite );
if( !succ )
return ERR_OPERATION;//;
for( i=0;i<128; i++ )
{
ff.Seek( 1078*sizeof(char)+(127-i)*128,CFile::begin );
ff.Write( &amp;pImgData[0],128*sizeof(char) );
}
ff.Close();
return FR_OK;//;
}
 
你的C的代码太麻烦了。
你把我写的函数稍微改改不就得了?

type TMyArray = array[0..127, 0..127] of Byte;
function MakeBmp(pImgData: TMyArray; FileName: string): Integer;
var
i, j: Integer;
p: PByteArray;
Bmp: TBitmap;
begin
Bmp := TBitmap.Create;
Bmp.LoadFromFile(FileName);
Bmp.PixelFormat := pf8Bit;
for i := 0 to Bmp.Height - 1 do
begin
p := Bmp.ScanLine;
for j := 0 to Bmp.Width - 1 do
begin
pImgData[j] := p[j];
end;
end;
Bmp.Free;
end;

bmp的width与height可别超过128,否则会出错。
 
看了相关资料,明白了一些,正在测试。
我是朋友,能交个朋友吗?我的email是yumq@163.com,qq是25923678。
 
接受答案了.
 
可以,呵呵。。有事mail吧。
我公司不让聊天。

我对图像处理也比较感兴趣。

有一本delphi的专门图像处理的书,你可以下回来看看。
园地里就有。

zhwdan@sina.com
 
后退
顶部