以下程序是将一幅图读入内存并回显,奇怪的是文件一直打不开,是那里错了呢?
编译已通过,但运行的时候发生错误,显示“File I/O error."
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)); //计算一行象素中最少的Byte数
widthBytes := trunc(((8-(bi.biWidth*bi.biBitCount mod 8)) mod 8 + bi.biWidth*bi.biBitCount)/8); // 一行象素的Byte数应为4的整数倍
LineBytes := (4-(widthbytes mod 4)) mod 4 + widthbytes;//修正字节数 NullCount := LineBytes - WidthBytes; //每行字节数必须为4的倍数,不足补0
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;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
if form1.savePictureDialog1.Execute then
form1.Image1.Picture.SaveToFile(form1.SavePictureDialog1.FileName);
end;
procedure TForm1.BitBtn3Click(Sender: TObject);
begin
close;
end;end.
编译已通过,但运行的时候发生错误,显示“File I/O error."
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)); //计算一行象素中最少的Byte数
widthBytes := trunc(((8-(bi.biWidth*bi.biBitCount mod 8)) mod 8 + bi.biWidth*bi.biBitCount)/8); // 一行象素的Byte数应为4的整数倍
LineBytes := (4-(widthbytes mod 4)) mod 4 + widthbytes;//修正字节数 NullCount := LineBytes - WidthBytes; //每行字节数必须为4的倍数,不足补0
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;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
if form1.savePictureDialog1.Execute then
form1.Image1.Picture.SaveToFile(form1.SavePictureDialog1.FileName);
end;
procedure TForm1.BitBtn3Click(Sender: TObject);
begin
close;
end;end.
FileName
LoadBmpFile(OpenPictureDialog1.FileName);
project project1.exe raised exception class EAccessViolation with message“Access violation at adress 00405AF8 in module project1.exe”。write of Address 00FFFFF7。process stopped。Use Step or Run to continue.
f: file;
begin
assignfile(f,'c:\demo.bmp');
reset(f,1);我的代码就怎么没出错?
BlockRead(hf,bi,SizeOf(BitMapInfoHeader));都使从文件头开始读的呀,内容不对吧