delphi流操作心得

來源:互聯網
上載者:User

對檔案或圖片進行流操作,下面是一個範例程式碼:

 1unit Unit1;
 2
 3interface
 4
 5uses
 6  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 7  Dialogs, StdCtrls, ExtCtrls,types;
 8
 9type
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
23var
24  Form1: TForm1;
25 BitmapmemoryStream:TMemoryStream;
26       Bitmap1:TBitmap;
27       bt:TByteDynArray;
28implementation
29
30{$R *.dfm}
31
32procedure 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
50procedure TForm1.Button3Click(Sender: TObject);
51var ms:TMemoryStream ;
52begin
53ms:=TMemoryStream.Create;
54if 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;
69end.
70

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.