注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

鑫淼梦园的博客

圆你的梦想 从这里开始

 
 
 

日志

 
 

2013年05月22日  

2013-05-22 08:21:44|  分类: delphi xe3 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
Delphi存取JPEG、BMP图像到数据库完整解决方案

时间:2011-9-3 14:57:35 点击: 357

  核心提示:unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Di...
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ExtDlgs, StdCtrls, ADODB, Grids, DBGrids, ExtCtrls,jpeg,
DBCtrls;

type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
ADOConnection1: TADOConnection;
ADOTable1: TADOTable;
selectimage: TButton;
savetodb: TButton;
OpenPictureDialog1: TOpenPictureDialog;
DataSource1: TDataSource;
DBNavigator1: TDBNavigator;
savetofile: TButton;
Label1: TLabel;
Label2: TLabel;
Button1: TButton;
Bevel1: TBevel;
Bevel2: TBevel;
GroupBox1: TGroupBox;
Image1: TImage;
Label3: TLabel;
Label4: TLabel;
DBImage1: TDBImage;
procedure selectimageClick(Sender: TObject);
procedure savetodbClick(Sender: TObject);
procedure ADOTable1AfterScroll(DataSet: TDataSet);
procedure savetofileClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ADOTable1BeforeScroll(DataSet: TDataSet);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
uses unit2;
{$R *.dfm}

procedure TForm1.selectimageClick(Sender: TObject); //选择图像
begin
if openpicturedialog1.Execute then
image1.Picture.LoadFromFile(openpicturedialog1.FileName );
end;

////如下保存方法only to sql and access'data
procedure TForm1.savetodbClick(Sender: TObject); //保存图像
var
strm:tmemorystream;
ext:string;
begin
if image1.picture.Graphic <> nil then //避免image1中无图像保存出错
begin
ext:=extractfileext(openpicturedialog1.FileName );
strm := tmemorystream.Create ;
try
image1.Picture.Graphic.SaveToStream(strm);
adotable1.Edit ;
strm.Position :=0;
DBImage1.dataField :=''; //dbimage只能显示BMP,否则myimage由BMP变为jpeg时会出错
tblobfield(adotable1.FieldByName('myimage')).LoadFromStream(strm);
//如需直接由文件保存 TBlobField(adotable1.FieldByName('myimage')).LoadFromFile(OpenPictureDialog1.FileName);
//以下记录保存到数据库的图像格式
if uppercase(ext) = '.BMP' then
begin
adotable1.FieldByName('isbmp').Value := 1;
dbimage1.dataField := 'myimage';
end
else if (uppercase(ext) = '.JPG') OR ( uppercase(ext) = '.JPEG') THEN
adotable1.FieldByName('isbmp').Value := 0;
adotable1.Post ;
finally
strm.Free ; //如果你选用TBLOBSTREAM类,程序运行到此语句会出错,可该语句前添入adotable1.edit
end;
end;
end;
///如下显示方法不适用于paradox中的graphic字段的显示。
procedure TForm1.adoTable1AfterScroll(DataSet: TDataSet); //显示图像
var
strm:tadoblobstream;
jpegimage:tjpegimage;
bitmap:tbitmap;
begin
strm := tadoblobstream.Create(tblobfield(adotable1.fieldbyname('MYIMAGE')),bmread);
try //try1
strm.position :=0;
image1.Picture.Graphic := nil;
DBIMAGE1.DataField := '';
//显示时,BMP、JPEG两种图像数据必需分别处理
if adotable1.fieldbyname('isbmp').asstring ='1' then
begin //begin11
bitmap := tbitmap.Create ;
try //try11
bitmap.LoadFromStream(strm);
image1.Picture.Graphic := bitmap;
DBIMAGE1.DataField := 'myimage';
finally
bitmap.Free;
end; //end try11
end //end begin11
else if adotable1.fieldbyname('isbmp').asstring ='0' then
begin //begin12
jpegimage := tjpegimage.Create ;
try //try12
jpegimage.LoadFromStream(strm);
image1.Picture.Graphic := jpegimage;
finally
jpegimage.Free ;
end; //end try12
end; //end begin12
finally
strm.Free ;
end; //end try1
end;

////显示时必须分bmp and jpeg 两种情况处理,而保存可统一。

procedure TForm1.savetofileClick(Sender: TObject);
var
tmpstr:string;
begin
if image1.Picture.Graphic <> nil then
begin
tmpstr := openpicturedialog1.Filter;
if adotable1.fieldbyname('isbmp').asstring ='1' then
begin
openpicturedialog1.Filter := 'Bitmaps (*.bmp)|*.bmp';
if openpicturedialog1.Execute then
image1.Picture.SaveToFile(openpicturedialog1.FileName+'.bmp');
end
else
begin
openpicturedialog1.Filter := 'JPEG Image File (*.jpg)|*.jpg';
if openpicturedialog1.Execute then
image1.Picture.SaveToFile(openpicturedialog1.FileName+'.jpg');
end;
openpicturedialog1.Filter := tmpstr;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
form2.Show;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
adoconnection1.Connected := true;
adoconnection1.LoginPrompt := false;
adotable1.Active := true;

end;

procedure TForm1.ADOTable1BeforeScroll(DataSet: TDataSet);
begin
dbimage1.dataField :=''; //这条语句不能遗漏,不信你试试
end;

end.
  评论这张
 
阅读(300)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017