📄 unit1.pas
字号:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtDlgs, DB, DBTables, ExtCtrls;
type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
Image1: TImage;
Button1: TButton;
Button2: TButton;
Table1: TTable;
OpenPictureDialog1: TOpenPictureDialog;
GroupBox2: TGroupBox;
Image2: TImage;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if openpicturedialog1.Execute then
image2.Picture.LoadFromFile(OpenPictureDialog1.FileName);
//将图像显示在image2中
button2.Enabled:=true;
button1.Enabled:=false;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
stream:tmemorystream;
begin
with table1 do
begin
insert;
fieldbyname('Name').asstring:=ExtractFileName((OpenPictureDialog1.FileName)); //提取文件名存储为表的Name字段值
stream:=TMemoryStream.Create(); //创建二进制数据流对象
stream.loadfromfile(OpenPictureDialog1.FileName); //载入图像文件
TBlobField(FieldByName('ImageData')).LoadFromStream(stream);
//调用BOLB字段对象的loadfromstream方法将图像文件存入数据库
Post;
end;
with table1 do
begin
listbox1.Clear;
{遍历table1中的name字段,将所有的字段值加入listbox中}
first;
while not eof do
begin
listbox1.Items.Add(fieldbyname('name').AsString);
next;
end;
button2.Enabled:=false;
button1.Enabled:=true;
end;
end;
procedure TForm1.ListBox1Click(Sender: TObject);
var
stream:tstream;
bmp:tbitmap;
begin
with table1 do
begin
locate('Name',ListBox1.Items[ListBox1.ItemIndex],[loPartialKey]);
//查找table1中的name字段值和当前listbox1中选中的项相符的记录
bmp:=tbitmap.Create; // 创建tbtmap对象
stream:=createblobstream(fieldbyname('ImageData'),bmread);
//将imagedata字段的图像信息读取到steam中
stream.Position:=0;
bmp.LoadFromStream(stream);
//将数据流中的信息读取抖啊tmitmap对象bmp中
image1.Picture.Assign(bmp); //将bmp对象显示在image1中
bmp.Free;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -