unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB, Mask, DBCtrls, ExtCtrls, jpeg, ExtDlgs;
type
TForm1 = class(TForm)
Image1: TImage;
DataSource1: TDataSource;
DBEdit1: TDBEdit;
DBEdit2: TDBEdit;
ADOConnection1: TADOConnection;
ADOTable1: TADOTable;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
OpenPictureDialog1: TOpenPictureDialog;
DBNavigator1: TDBNavigator;
Image2: TImage;
Button3: TButton;
DBEdit3: TDBEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure ADOTable1AfterScroll(DataSet: TDataSet);
private
ExtName: string;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//选择图片
procedure TForm1.Button1Click(Sender: TObject);
var
jpgImage: TJPEGImage;
begin
if openpicturedialog1.Execute then
begin
extName := ExtractFileExt(openpicturedialog1.FileName);
extName := Trim(LowerCase(extName));
Edit1.Text := ExtName;
if (ExtName = '.jpg') or (ExtName = '.jpeg') then
begin
jpgImage := TJPEGImage.Create;
jpgImage.LoadFromFile(openpicturedialog1.FileName);
image1.Picture.Graphic := jpgImage;
jpgImage.Free;
end
else if ExtName = '.bmp' then
begin
image1.Picture.LoadFromFile(openpicturedialog1.FileName);
end;
end;
end; //保存图片
procedure TForm1.Button2Click(Sender: TObject);
var
strm: TMemoryStream;
myjpeg: TJPEGImage;
begin
if (ExtName = '.jpg') or (ExtName = '.jpeg') or (ExtName = '.bmp') then
begin
adotable1.Edit;
if (ExtName = '.jpg') or (ExtName = '.jpeg') then
begin
strm := TMemoryStream.Create;
myjpeg := TJPEGImage.Create;
myjpeg.Assign(image1.Picture.Graphic);
myjpeg.SaveToStream(strm);
strm.Position := 0;
tblobfield(adotable1.FieldByName('img1')).LoadFromStream(strm);
strm.Free;
end;
if ExtName = '.bmp' then
begin
strm := TMemoryStream.Create;
image1.Picture.Bitmap.SaveToStream(strm);
strm.Position := 0;
tblobfield(adotable1.FieldByName('img1')).LoadFromStream(strm);
strm.Free;
end;
adotable1.FieldByName('extm').Value := ExtName;
adotable1.Post;
ShowMessage('保存图片成功!');
end
else
ShowMessage('保存图片失败!');
end;
//读取图像
procedure TForm1.Button3Click(Sender: TObject);
var
buf: TMemoryStream;
begin
if adotable1.FieldValues['img1'] <> 'NULL' then
begin
buf := TMemoryStream.Create;
try
buf.Position := 0;
tblobfield(adotable1.FieldByName('img1')).SaveToStream(buf);
buf.Position := 0;
Image1.Picture.Graphic := nil;
if (adotable1.FieldValues['extm'] = '.jpg') then
begin
Image1.Picture.Graphic := TJPEGImage.Create;
Image1.Picture.Graphic.LoadFromStream(buf);
end;
if (adotable1.FieldValues['extm'] = '.jpeg') then
begin
Image1.Picture.Graphic := TJPEGImage.Create;
Image1.Picture.Graphic.LoadFromStream(buf);
end;
if (adotable1.FieldValues['extm'] = '.bmp') then
begin
Image1.Picture.Bitmap := nil;
Image1.Picture.Bitmap.LoadFromStream(buf);
end;
buf.Free;
except
ShowMessage('载入图片不成功,请检查图片类型');
end;
end;
Image2.Picture := Image1.Picture;
end;
//显示图片
procedure TForm1.ADOTable1AfterScroll(DataSet: TDataSet);
var
buf: TMemoryStream;
begin
if adotable1.FieldValues['img1'] <> 'NULL' then
begin
buf := TMemoryStream.Create;
try
buf.Position := 0;
tblobfield(adotable1.FieldByName('img1')).SaveToStream(buf);
buf.Position := 0;
Image1.Picture.Graphic := nil;
if (adotable1.FieldValues['extm'] = '.jpg') then
begin
Image1.Picture.Graphic := TJPEGImage.Create;
Image1.Picture.Graphic.LoadFromStream(buf);
end;
if (adotable1.FieldValues['extm'] = '.jpeg') then
begin
Image1.Picture.Graphic := TJPEGImage.Create;
Image1.Picture.Graphic.LoadFromStream(buf);
end;
if (adotable1.FieldValues['extm'] = '.bmp') then
begin
Image1.Picture.Bitmap := nil;
Image1.Picture.Bitmap.LoadFromStream(buf);
end;
buf.Free;
except
ShowMessage('载入图片不成功,请检查是否是图片类型');
end;
end;
Image2.Picture := Image1.Picture;
end;
end.
|
请发表评论