对文件或图片进行流操作,下面是一个示例代码:
1
unit Unit1;
2
3
interface
4
5
uses
6
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7
Dialogs, StdCtrls, ExtCtrls,types;
8
9
type
10
TForm1 = class(TForm)
11
Open: TOpenDialog;
12
Button3: TButton;
13
Panel1: TPanel;
14
Image1: TImage;
15
procedure Button3Click(Sender: TObject);
16
procedure Image1Click(Sender: TObject);
17
private
18
{ Private declarations }
19
public
20
{ Public declarations }
21
end;
22
23
var
24
Form1: TForm1;
25
BitmapmemoryStream:TMemoryStream;
26
Bitmap1:TBitmap;
27
bt:TByteDynArray;
28
implementation
29
30
{$R *.dfm}
31
32
procedure TForm1.Image1Click(Sender: TObject);
33
var
34
files:TFileStream;
35
begin
36
BitmapmemoryStream:=TMemoryStream.Create;// file://建立MemoryStream
37
Bitmap1:=TBitmap.Create;
38
if open.Execute then
39
begin
40
files:= TFileStream.Create(open.FileName,fmShareDenyWrite);
41
try
42
Bitmap1.LoadFromStream(files);
43
image1.Picture.Bitmap:=Bitmap1;
44
except
45
ShowMessage('^-^,错了');
46
end;
47
end;
48
end;
49
50
procedure TForm1.Button3Click(Sender: TObject);
51
var ms:TMemoryStream ;
52
begin
53
ms:=TMemoryStream.Create;
54
if Assigned(Bitmap1) then
55
Bitmap1.SaveToStream(BitmapmemoryStream);
56
if BitmapMemoryStream<>nil then
57
begin
58
try
59
// BitmapmemoryStream.SaveToFile('Bitmap1.txt'); //file://内存流保存,大小与
60
SetLength(bt,BitmapmemoryStream.Size); // .InstanceSize
61
BitmapmemoryStream.Read(bt,BitmapmemoryStream.Size); // file://Bitmap1.bmp一样
62
ms.Write(bt,length(bt));
63
ms.SaveToFile('F:\\1.txt');
64
except
65
showmessage('error on access memory!');
66
end;
67
end;
68
end;
69
end.
70
unit Unit1;2

3
interface4

5
uses6
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,7
Dialogs, StdCtrls, ExtCtrls,types;8

9
type10
TForm1 = class(TForm)11
Open: TOpenDialog;12
Button3: TButton;13
Panel1: TPanel;14
Image1: TImage;15
procedure Button3Click(Sender: TObject);16
procedure Image1Click(Sender: TObject);17
private18
{ Private declarations }19
public20
{ Public declarations }21
end;22

23
var24
Form1: TForm1;25
BitmapmemoryStream:TMemoryStream;26
Bitmap1:TBitmap;27
bt:TByteDynArray;28
implementation29

30
{$R *.dfm}31

32
procedure TForm1.Image1Click(Sender: TObject);33
var34
files:TFileStream;35
begin36
BitmapmemoryStream:=TMemoryStream.Create;// file://建立MemoryStream37
Bitmap1:=TBitmap.Create;38
if open.Execute then39
begin40
files:= TFileStream.Create(open.FileName,fmShareDenyWrite);41
try42
Bitmap1.LoadFromStream(files);43
image1.Picture.Bitmap:=Bitmap1;44
except45
ShowMessage('^-^,错了');46
end;47
end;48
end;49

50
procedure TForm1.Button3Click(Sender: TObject);51
var ms:TMemoryStream ;52
begin53
ms:=TMemoryStream.Create;54
if Assigned(Bitmap1) then55
Bitmap1.SaveToStream(BitmapmemoryStream);56
if BitmapMemoryStream<>nil then57
begin58
try59
// BitmapmemoryStream.SaveToFile('Bitmap1.txt'); //file://内存流保存,大小与60
SetLength(bt,BitmapmemoryStream.Size); // .InstanceSize61
BitmapmemoryStream.Read(bt,BitmapmemoryStream.Size); // file://Bitmap1.bmp一样62
ms.Write(bt,length(bt));63
ms.SaveToFile('F:\\1.txt');64
except65
showmessage('error on access memory!');66
end;67
end;68
end;69
end.70


浙公网安备 33010602011771号