我找了又找,似乎找不到任何描述我在delphi代码中要做的事情的东西。解决方案有时很接近,但还不够接近,我无法弄清楚。所以我在这里请求..。
我有很多从截图中恢复的位图。我一直在做的是保存到bitmaps_001.bmp,但它占用了很多存储空间,所以我升级了例程,将其另存为bitmaps_001.png,这节省了更多的空间,但现在我想保存到一个文件,tfilestream,并使用tprogressbar条读取它,我可以像屏幕上的图像一样向左/向右拖动。
基本上,我试图完成以下几点:
procedure SaveBMPtoStream(st: tfilestream; bmp: tbitmap);
procedure ReadBMPfrStream(st: tfilestream; bmp: tbitmap; bnum: integer);到目前为止,代码(如下所示)按原样工作(它只需按下一个按钮即可写入和读取一个位图图像),但我只能写入一个位图图像。我需要在每个会话中实时地向tfilestream写入尽可能多的图像,可能是使用ttimer控件,并让它写入尽可能多的图像,直到我按下停止按钮。我如何修改下面的代码来解决这个问题呢?谢谢。
我正在运行windows xp,连接到带有NTFS文件系统的外部usb3.0 1tb驱动器。
type
TMS = TFileStream;
var
MS: TMS;
pos: int64; // bnum for 0-99,999 images.
sz: integer; // size of the image/stream ?
//write bitmaps to stream
procedure SaveBMPtoStream(ms: TMS; Bmp: TBitmap; bnum: integer);
begin
// create (or append to) stream
if fileexists('d:\streams\s.stm') then MS := TFileStream.Create('d:\streams\s.stm', fmOpenReadWrite)
else MS := TFileStream.Create('d:\streams\s.stm', fmCreate);
//sz:=MS.Size; pos:=ms.Position;
bmp.SaveToStream(MS);
// free stream
ms.free;
end;
//read bitmaps from stream
procedure ReadBMPfrStream(ms: TMS; Bmp: TBitmap; bnum: integer);
begin
// open stream.
MS := TFileStream.Create ('d:\streams\s.stm', fmOpenReadWrite);
// read in bitmap from stream
//sz:=MS.Size; pos:=ms.Position;
bmp.LoadFromStream(MS);
// free stream
ms.free;
end;发布于 2012-12-16 16:04:02
Function LoadBMPFromStream(const fn: String; Bmp: TBitmap; Nr: Integer):Boolean;
var // Nr is 0 based first Bitmap=0
fs: TFileStream;
ms: TMemoryStream;
intNr: Integer;
pos: Cardinal;
size: DWord;
begin
intNr := 0;
if fileexists(fn) then
begin
Result := true;
fs := TFileStream.Create(fn, fmOpenRead or fmShareDenyNone);
try
fs.Read(size, SizeOf(DWord)); // Read Size of first Bitmap
while (Nr > intNr) and (fs.Position < fs.size) do
begin
fs.Seek(size, soFromCurrent);
fs.Read(size, SizeOf(DWord)); // Read Size of Bitmap with intNr
inc(intNr);
end;
if fs.Position < fs.size then
begin
ms := TMemoryStream.Create;
try
ms.CopyFrom(fs, size);
ms.Position := 0;
Bmp.LoadFromStream(ms);
finally
ms.Free;
end;
end
else Result := false;
finally
fs.Free;
end;
end;
end;
procedure SaveBMPtoStream(const fn: String; Bmp: TBitmap);
var
fs: TFileStream;
ms: TMemoryStream;
pos: Cardinal;
size: DWord;
begin
if fileexists(fn) then
begin
fs := TFileStream.Create(fn, fmOpenReadWrite or fmShareDenyNone);
fs.Seek(0, soEnd);
end
else
begin
fs := TFileStream.Create(fn, fmCreate or fmShareDenyNone);
end;
try
ms := TMemoryStream.Create;
try
Bmp.SaveToStream(ms);
size := ms.size;
ms.Position := 0;
fs.Write(size, SizeOf(DWord)); // Write Size of next Bitmap first
fs.CopyFrom(ms, size);
finally
ms.Free;
end;
finally
fs.Free;
end;
end;
procedure TForm6.Button2Click(Sender: TObject);
begin
// load first Picture
LoadBMPFromStream('C:\temp\test.str', Image3.picture.bitmap, 0);
// load third picture
// LoadBMPFromStream('C:\temp\test.str', Image3.picture.bitmap, 2);
end;
procedure TForm6.Button1Click(Sender: TObject);
begin
SaveBMPtoStream('C:\temp\test.str', Image1.picture.bitmap);
SaveBMPtoStream('C:\temp\test.str', Image2.picture.bitmap);
SaveBMPtoStream('C:\temp\test.str', Image1.picture.bitmap);
end;发布于 2012-12-16 11:47:26
是否要将所有图像保存到同一文件?那么你最简单的选择就是使用一个压缩为0的7zip文件(只存储),这将非常容易地为你管理从存档中索引/保存/检索文件。
有一个很好的免费组件可以做到这一点:http://www.rg-software.de/rg/index.php?option=com_content&view=article&id=29&Itemid=51
你也可以再次尝试只使用位图,同时在7zip中使用快速压缩设置,这可能会达到比PNG更快的速度。
https://stackoverflow.com/questions/13898191
复制相似问题