如何保存图片到数据库中 ( 积分: 300 )

  • 主题发起人 主题发起人 rgwj_2001
  • 开始时间 开始时间
R

rgwj_2001

Unregistered / Unconfirmed
GUEST, unregistred user!
我需要将图像保存到数据库中,第一次采用插入,后面采用更新的方式
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, Grids, DBGrids, Buttons, ExtCtrls, StdCtrls, ADODB,Jpeg,QGraphics;

type
TForm1 = class(TForm)
ADOQuery1: TADOQuery;
ADOConnection1: TADOConnection;
Button1: TButton;
OpenDialog1: TOpenDialog;
Edit1: TEdit;
Image1: TImage;
Image2: TImage;
BitBtn1: TBitBtn;
DBGrid1: TDBGrid;
DataSource1: TDataSource;
procedure Button1Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
s:string;
s1:string;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
//bitmap1:tbitmap;
pjpg:tjpegimage;
pic:TMemoryStream;
begin
pjpg:=tjpegimage.Create;
if opendialog1.Execute then
s:=opendialog1.FileName ;
s1:=extractfileext(s);
if (uppercase(s1) ='.JPG' ) or (uppercase(s1)='.JPEG') THEN // 图片格式一定要大写
begin
pjpg.LoadFromFile(s);
pic:=TMemoryStream.Create;
pjpg.SaveToStream(pic);
image1.Picture.Bitmap.Assign(pjpg);
pic.Position:=0;
adoquery1.Insert ;
adoquery1.FieldByName('borj').VALUE := 2;
TBlobField(adoquery1.FieldByName('pic')).LoadFromStream(pic);
adoquery1.post;
pic.Free;
pjpg.free;
end ;
if (uppercase(s1)='.BMP') then
begin
edit1.Text :=uppercase(s1);
image1.Picture.LoadFromFile(s);
adoquery1.Insert ;
adoquery1.FieldByName('pic').Assign(image1.Picture.Graphic);
adoquery1.FieldByName('borj').VALUE := 3;
adoquery1.Post ;
end;
end;


procedure TForm1.BitBtn1Click(Sender: TObject);
var
jpeg:tjpegimage;
begin
image1.Picture.Graphic :=nil;
with adoquery1 do
begin
if adoquery1.FieldByName('borj').Value =3 then // bmp
image2.Picture.Bitmap.Assign(fieldbyname('pic')) ;
if adoquery1.FieldByName('borj').Value =2 then//jpge;
begin
jpeg:=tjpegimage.Create ;
try
jpeg.Assign(adoquery1.FieldByName('pic') );
image2.Picture.Graphic:=jpeg;
finally
jpeg.Free ;
end;
end;
end;
end;

end.
 
接受答案了.
 
后退
顶部