• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

delphi图片存数据库的代码

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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.


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Delphi通过IE窗口句柄获取网页接口(IWebBrowser2)good发布时间:2022-07-18
下一篇:
Delphi系统变量:IsMultiThread对MM的影响发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap