gpt4 book ai didi

delphi - 将位图写入/读取到 tfilestream

转载 作者:行者123 更新时间:2023-12-03 15:00:10 30 4
gpt4 key购买 nike

我搜索了又搜索,似乎找不到任何描述我想要在delphi代码中做什么的东西。这些解决方案有时很接近,但还不足以让我弄清楚。所以我在这里问..

我有很多位图,我正在从屏幕截图中检索它们。我一直在做的是保存到bitmaps_001.bmp,但是它需要很多存储空间,所以我升级了例程以另存为bitmaps_001.png,这样可以节省更大的空间,但现在我想保存到一个文件,一个 tfilestream,并使用 tprogressbar 从中读取,我可以根据图像在屏幕上显示的情况向左/向右拖动。

基本上,我正在尝试完成以下任务:

procedure SaveBMPtoStream(st: tfilestream; bmp: tbitmap);
procedure ReadBMPfrStream(st: tfilestream; bmp: tbitmap; bnum: integer);

到目前为止,代码(下面)按原样工作(它在按下 t 按钮时写入和读取一个位图图像),但我只能写入一个位图图像。我需要将每个 session 所需的尽可能多的图像实时写入 tfilestream,可能使用计时器控件并让它写入尽可能多的图像,直到我按下停止 t 按钮。我可以做什么来修改下面的代码来解决这个问题?谢谢。

我正在运行 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;

最佳答案

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;

关于delphi - 将位图写入/读取到 tfilestream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13898191/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com