这段程序已通过编译,但运行的时候老出错,文件总是打不开unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtDlgs, ExtCtrls;
type
TForm1 = class(TForm)
OpenPictureDialog1: TOpenPictureDialog;
BitBtn1: TBitBtn;
Image1: TImage;
BitBtn2: TBitBtn;
SavePictureDialog1: TSavePictureDialog;
BitBtn3: TBitBtn;
procedure BitBtn1Click(Sender: TObject);//打开图象按钮;
procedure BitBtn2Click(Sender: TObject);//保存图象按钮;
procedure BitBtn3Click(Sender: TObject);//退出; private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
function LoadBmpFile(BmpFile: String): Boolean;
var
HimgData:array of array of Integer;//定义二维动态数组
hf:file;
bf: bitmapfileheader ;文件头
bi: bitmapinfoheader ;文件信息头
widthbytes:integer;图像宽度
Nullcount:integer;
ImgSize:integer;图像大小
LineBytes:integer;
numcolors:integer;
begin
AssignFile(hf,BmpFile);
try
Reset(hf,1);
Except
On Einouterror do
MessageDlg(‘file I/O error.’,mterror,[mbOk],0);
End;
BlockRead(hf,bf,SizeOf(BITMAPFILEHEADER));
BlockRead(hf,bi,SizeOf(BitMapInfoHeader));//以下是为将图读入内存做的一些处理。
widthBytes := trunc(((8-(bi.biWidth*bi.biBitCount mod 8)) mod 8 + bi.biWidth*bi.biBitCount)/8); LineBytes := (4-(widthbytes mod 4)) mod 4 + widthbytes; NullCount := LineBytes - WidthBytes;
ImgSize:= LineBytes*bi.biHeight;
if bi.biClrUsed <>0 then
NumColors := bi.biClrUsed
else
begin
case bi.biBitCount of
1:NumColors := 2;
4:NumColors := 16;
8:NumColors := 256;
24:NumColors := 0;
else
begin
ShowMessage('Invalid color numbers!');
CloseFile(hf);
Result := False;
Exit;
end;
end;
end;
Setlength(hImgData,ImgSize);
BlockRead(hf,hImgData[0],ImgSize);
CloseFile(hf);
Result := True;
end;//
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
form1.OpenPictureDialog1.Title:=’请选择一个位图文件打开(*.bmp)';
form1.OpenPictureDialog1.InitialDir:='my pictures';
if form1.OpenPictureDialog1.Execute then
loadBmpFile(openpicturedialog1.name);
end;
......
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtDlgs, ExtCtrls;
type
TForm1 = class(TForm)
OpenPictureDialog1: TOpenPictureDialog;
BitBtn1: TBitBtn;
Image1: TImage;
BitBtn2: TBitBtn;
SavePictureDialog1: TSavePictureDialog;
BitBtn3: TBitBtn;
procedure BitBtn1Click(Sender: TObject);//打开图象按钮;
procedure BitBtn2Click(Sender: TObject);//保存图象按钮;
procedure BitBtn3Click(Sender: TObject);//退出; private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
function LoadBmpFile(BmpFile: String): Boolean;
var
HimgData:array of array of Integer;//定义二维动态数组
hf:file;
bf: bitmapfileheader ;文件头
bi: bitmapinfoheader ;文件信息头
widthbytes:integer;图像宽度
Nullcount:integer;
ImgSize:integer;图像大小
LineBytes:integer;
numcolors:integer;
begin
AssignFile(hf,BmpFile);
try
Reset(hf,1);
Except
On Einouterror do
MessageDlg(‘file I/O error.’,mterror,[mbOk],0);
End;
BlockRead(hf,bf,SizeOf(BITMAPFILEHEADER));
BlockRead(hf,bi,SizeOf(BitMapInfoHeader));//以下是为将图读入内存做的一些处理。
widthBytes := trunc(((8-(bi.biWidth*bi.biBitCount mod 8)) mod 8 + bi.biWidth*bi.biBitCount)/8); LineBytes := (4-(widthbytes mod 4)) mod 4 + widthbytes; NullCount := LineBytes - WidthBytes;
ImgSize:= LineBytes*bi.biHeight;
if bi.biClrUsed <>0 then
NumColors := bi.biClrUsed
else
begin
case bi.biBitCount of
1:NumColors := 2;
4:NumColors := 16;
8:NumColors := 256;
24:NumColors := 0;
else
begin
ShowMessage('Invalid color numbers!');
CloseFile(hf);
Result := False;
Exit;
end;
end;
end;
Setlength(hImgData,ImgSize);
BlockRead(hf,hImgData[0],ImgSize);
CloseFile(hf);
Result := True;
end;//
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
form1.OpenPictureDialog1.Title:=’请选择一个位图文件打开(*.bmp)';
form1.OpenPictureDialog1.InitialDir:='my pictures';
if form1.OpenPictureDialog1.Execute then
loadBmpFile(openpicturedialog1.name);
end;
......
你要根据第一个字节先看他的BMP头长度分析出这到底是
OS/2上的图像还是WIN上的BMP(这两种的颜色表是不一样的后面的多一个BYTE)
然后假如是后一种的话还会有好几种格式的.
然后要分析有没有用颜色表有没有MASK位
还有这个BMP是从上到下的还是从下到上的.
然后就要读入他的BITS了
然后是文件到底有多大(在头里面的一个字节或者自己算算应该有多大)
反正不是你这几行代码能搞定的,
我用C++前一段时间刚把这个类搞定了.