var m_jpeg : TJpegImage; m_blob : TStream; begin //读取JPG图像 m_blob := adoDataSet.CreateBlobStream(adoDataSet.FieldByName('Picture'),bmRead); m_jpeg:=TJpegImage.Create; try if m_blob.Size<>0 then begin try m_jpeg.LoadFromStream(m_blob); imInput.Picture.Assign(m_jpeg); imInput.Refresh; except end; end else imInput.Picture.Assign(nil); finally m_jpeg.Free; m_blob.Free; end; end; //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~` begin //如果图像不为空,把图像以JPG格式导入到字段中 if Assigned(imInput.Picture.Graphic) then begin jpg := TJpegImage.Create; ms := TMemoryStream.Create; try jpg.Assign(imInput.Picture.Graphic); jpg.SaveToStream(ms); if SizeOf(ms) > 0 then (adoDataSet.FieldByName('PICTURE') as TBlobField).LoadFromStream(ms); finally ms.Free; jpg.Free; end; end; //注:imInput为TImage类型 end;
网上搜索下,现成的例子很多。注意数据库中bmp和jpg文件是不同的处理方式就可以了。
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, DB, ADODB, ExtCtrls, StdCtrls,Jpeg;type TForm1 = class(TForm) ADOConnection1: TADOConnection; ADOQuery1: TADOQuery; Button1: TButton; Label1: TLabel; Memo1: TMemo; Label2: TLabel; Memo2: TMemo; Im1: TImage; ADOQuery2: TADOQuery; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject); var picpath_src,picpath_sql:string; queid,picpath:string;//问题编号和图片路径 tmp:string; Tms:TMemoryStream; TJpg:TJpegImage; begin //原始图片路径 //PS:原库内已经含有'/img' picpath_src:=ExtractFilePath(Application.ExeName); //导出用于复查的图片 picpath_sql:=ExtractFilePath(Application.ExeName)+'picfromsqlserver\'; memo1.Clear; adoquery2.close; adoquery2.sql.text:='select questionid,picpath from que where picpath is not null'; adoquery2.open; if adoquery2.recordcount=0 then exit; while not adoquery2.eof do begin queid:=trim(adoquery2.fieldbyname('questionid').AsString); picpath:=picpath_src+trim(adoquery2.fieldbyname('picpath').AsString); //处理的原始文件名保存在memo1中,测试文件名用 memo1.Lines.Add(picpath); //保存到sql server中 with adoquery1 do begin close; sql.Text:='update que set havepic=''1'',pic=:xp where questionid=:queid'; TJpg:=TJpegImage.Create; try //显示照片 im1.Picture.LoadFromFile(picpath); update; //写入SQL server adoquery1.parameters.ParamByName('xp').LoadFromFile(picpath,ftBlob) ; adoquery1.parameters.ParamValues['queid']:=queid ; adoquery1.execsql; finally Tjpg.Free; end; end; adoquery2.next;//下一张 end; //从sql server中读出并写入jpeg文件以验证结果 with adoquery1 do begin close; sql.Text:='select questionid queid,pic from que where havepic=''1'''; open; if recordcount=0 then exit; memo2.Lines.Clear; while not eof do begin Tms:=TMemoryStream.Create; TJpg:=TJpegImage.Create; try setlength(tmp,fieldbyname('pic').DataSize); tmp:=fieldbyname('pic').AsString; queid:=trim(fieldbyname('queid').AsString); Tms.Write(tmp[1],length(tmp)); Tms.Position := 0; TJpg.LoadFromStream(Tms); TJpg.SaveToFile(picpath_sql+queid+'.jpg'); memo2.Lines.Add(picpath_sql+queid+'.jpg'); //显示照片 im1.Picture.LoadFromFile(picpath_sql+queid+'.jpg'); update; finally Tms.Free; Tjpg.Free; end; next; end; end;end;end.
http://topic.csdn.net/t/20030518/15/1800360.html
m_jpeg : TJpegImage;
m_blob : TStream;
begin
//读取JPG图像
m_blob := adoDataSet.CreateBlobStream(adoDataSet.FieldByName('Picture'),bmRead);
m_jpeg:=TJpegImage.Create;
try
if m_blob.Size<>0 then
begin
try
m_jpeg.LoadFromStream(m_blob);
imInput.Picture.Assign(m_jpeg);
imInput.Refresh;
except
end;
end
else
imInput.Picture.Assign(nil);
finally
m_jpeg.Free;
m_blob.Free;
end;
end;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
begin
//如果图像不为空,把图像以JPG格式导入到字段中
if Assigned(imInput.Picture.Graphic) then
begin
jpg := TJpegImage.Create;
ms := TMemoryStream.Create;
try
jpg.Assign(imInput.Picture.Graphic);
jpg.SaveToStream(ms);
if SizeOf(ms) > 0 then
(adoDataSet.FieldByName('PICTURE') as TBlobField).LoadFromStream(ms);
finally
ms.Free;
jpg.Free;
end;
end;
//注:imInput为TImage类型
end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, ExtCtrls, StdCtrls,Jpeg;type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
Button1: TButton;
Label1: TLabel;
Memo1: TMemo;
Label2: TLabel;
Memo2: TMemo;
Im1: TImage;
ADOQuery2: TADOQuery;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
picpath_src,picpath_sql:string;
queid,picpath:string;//问题编号和图片路径
tmp:string;
Tms:TMemoryStream;
TJpg:TJpegImage;
begin
//原始图片路径
//PS:原库内已经含有'/img'
picpath_src:=ExtractFilePath(Application.ExeName);
//导出用于复查的图片
picpath_sql:=ExtractFilePath(Application.ExeName)+'picfromsqlserver\'; memo1.Clear;
adoquery2.close;
adoquery2.sql.text:='select questionid,picpath from que where picpath is not null';
adoquery2.open;
if adoquery2.recordcount=0 then exit;
while not adoquery2.eof do
begin
queid:=trim(adoquery2.fieldbyname('questionid').AsString);
picpath:=picpath_src+trim(adoquery2.fieldbyname('picpath').AsString); //处理的原始文件名保存在memo1中,测试文件名用
memo1.Lines.Add(picpath); //保存到sql server中
with adoquery1 do
begin
close;
sql.Text:='update que set havepic=''1'',pic=:xp where questionid=:queid'; TJpg:=TJpegImage.Create;
try
//显示照片
im1.Picture.LoadFromFile(picpath);
update; //写入SQL server
adoquery1.parameters.ParamByName('xp').LoadFromFile(picpath,ftBlob) ;
adoquery1.parameters.ParamValues['queid']:=queid ;
adoquery1.execsql;
finally
Tjpg.Free;
end;
end; adoquery2.next;//下一张
end;
//从sql server中读出并写入jpeg文件以验证结果
with adoquery1 do begin
close;
sql.Text:='select questionid queid,pic from que where havepic=''1''';
open;
if recordcount=0 then exit;
memo2.Lines.Clear;
while not eof do
begin
Tms:=TMemoryStream.Create;
TJpg:=TJpegImage.Create;
try
setlength(tmp,fieldbyname('pic').DataSize);
tmp:=fieldbyname('pic').AsString;
queid:=trim(fieldbyname('queid').AsString);
Tms.Write(tmp[1],length(tmp));
Tms.Position := 0;
TJpg.LoadFromStream(Tms);
TJpg.SaveToFile(picpath_sql+queid+'.jpg');
memo2.Lines.Add(picpath_sql+queid+'.jpg');
//显示照片
im1.Picture.LoadFromFile(picpath_sql+queid+'.jpg');
update;
finally
Tms.Free;
Tjpg.Free;
end;
next;
end;
end;end;end.
你存的是什么文件?bmp还是jpg?
TBlobField(adoQuery1.FieldByName('PICTURE')).LoadFromFile(OpenPicDialog1.filename);
adoQuery1.FieldByName('fileName').asString := OpenPicDialog1.filename;
读出另存:
TBlobField(adoQuery1.FieldByName('PICTURE')).savetofile(adoQuery1.FieldByName('fileName').asString);
procedure TForm1.Button1Click(Sender: TObject);
var
Stream:TMemoryStream;
begin
ADOQuery1.Close;
ADOQuery1.SQL.Text:='SELECT top 1 imga from img';
ADOQuery1.Open;
try
Stream:=TMemoryStream.Create;
try
TBlobField(ADOQuery1.FieldByName('sign')).SaveToStream(Stream);
Stream.Position:=0;
Image1.Picture.Bitmap.LoadFromStream(Stream);
finally
Stream.Free;
end;
except
ShowMessage('Error');
end;
end;end.
================================
大哥都给源代码了,还不会......你要好好的再去读DELPHI书了.基础不牢.