把图片存入DAT。调用的时候报错。大侠们帮我看看(付我的代码) ( 积分: 50 )

  • 主题发起人 主题发起人 huapoint
  • 开始时间 开始时间
H

huapoint

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,JPEG;

type
TAddDat = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
AddDat: TAddDat;
BufBit :array [0..255]of Byte;
F,E :File;
Stream,s :TMemoryStream;
NumRead :Integer;
JPEGImage :TJPEGImage;
implementation

{$R *.dfm}

procedure TAddDat.Button1Click(Sender: TObject);
begin
if fileexists(extractfilepath(application.exename)+'1.jpg') then
begin
AssignFile(F,extractfilepath(application.exename)+'1.jpg'); //打开JPG文件
Reset(F,1);
Seek(F,0);
Stream:=TMemoryStream.Create; //MemoryStream初始化
Stream.SetSize(FileSize(F));
Stream.Seek(0,soFromBeginning);
repeat //读入到内存
BlockRead(F,BufBit,sizeof(BufBit),NumRead);
Stream.Write(BufBit,sizeof(BufBit));
until NumRead = 0;
try
if fileexists(extractfilepath(application.exename)+'JPEG.dat') then
begin
Stream.loadfromfile(extractfilepath(application.exename)+'JPEG.dat');
Stream.seek(0,soFromBeginning);
end
else
Stream.position:=0;
Stream.write(F, sizeof(F));
Stream.savetofile(extractfilepath(application.exename)+'JPEG.dat');
finally
CloseFile(F);
Stream.Clear;
Stream.free;
end;
showmessage('生成DAT成功!');
end
else
showmessage('文件不存在.DAT文件生成失败');
end;

procedure TAddDat.Button2Click(Sender: TObject);
begin
if fileexists(extractfilepath(application.exename)+'JPEG.dat') then
begin
Reset(F,1);
Seek(F,0);
s:=TMemoryStream.Create;
s.loadfromfile(extractfilepath(application.exename)+'Jpg.dat');
s.Seek(0,soFromBeginning);
repeat //读入到内存
BlockRead(F,BufBit,sizeof(BufBit),NumRead);
s.Write(BufBit,sizeof(BufBit));
until NumRead = 0;
CloseFile(F);
s.Position:=0;
JPEGImage := TJPEGImage.Create; //建立JPEG对象
JPEGImage.LoadFromStream(s); //复制内存到JPEG对象
s.Clear;
s.Free; //清理内存
self.Canvas.StretchDraw(self.Canvas.ClipRect,JPEGImage); //显示在Form布景上
JPEGImage.Free; //释放JPEG对象
end;
end;

procedure TAddDat.Button3Click(Sender: TObject);
begin
Stream.Clear;
s.Clear;
s.Free;
CloseFile(F);
Stream.Free;
JPEGImage.Free;
end;

end.
 
既然用了流,又何必用传统的文件类型?一个TFileStream就可以搞定。
 
我的目的就是采用流。文件流的话该如何做。具体能不能出确切的修改方式。。
 
后退
顶部