目的:把图片存入数据库,在调出来 不知道下面代码哪里错了????
是图片格式的原因吗? sql数据库中的字段,我设的 ‘image ’类型 对吗?
procedure TForm1.BitBtn1Click(Sender: TObject);
var
MyJPEG : TJPEGImage; ///这里的存入图片好像没报错。。
MS: TMemoryStream;
begin
MyJPEG := TJPEGImage.Create;
try
with MyJPEG do
begin
Assign(Image1.Picture.Graphic);
MS:=TMemoryStream.create;
SaveToStream(MS);
MS.Position:=0;
end;
finally
MyJPEG.Free;
try
adoquery1.insert ;
TBlobField(adoquery1.FieldbyName('Image')).LoadFromStream(MS);
adoquery1.Post;
finally
ms.Free
end;
end;
end;procedure TForm1.BitBtn2Click(Sender: TObject);
begin
if not openpicturedialog1.Execute then exit;
image1.Picture.LoadFromFile(openpicturedialog1.FileName);end;procedure TForm1.BitBtn3Click(Sender: TObject); //这里的调出图片就出错了
Var
Ms:TMemoryStream;
MyJPEG:Tjpegimage;
begin
Ms:=TMemoryStream.Create;
TBlobField(ADOquery1.FieldByName('image')).SaveToStream(Ms);
Ms.Position :=0; MyJPEG.LoadFromStream(Ms); ///错误指在这一行。
Image2.Picture.Assign(MyJPEG);
Ms.Free;
MyJPEG.free;
end; ///错误警告是:Project project1.exe raised exception class EAccessViolation with message 'Access violation at address 004b621b in module 'Project1.exe'.Read of address 0000bd65'.Process stopped.u
是图片格式的原因吗? sql数据库中的字段,我设的 ‘image ’类型 对吗?
procedure TForm1.BitBtn1Click(Sender: TObject);
var
MyJPEG : TJPEGImage; ///这里的存入图片好像没报错。。
MS: TMemoryStream;
begin
MyJPEG := TJPEGImage.Create;
try
with MyJPEG do
begin
Assign(Image1.Picture.Graphic);
MS:=TMemoryStream.create;
SaveToStream(MS);
MS.Position:=0;
end;
finally
MyJPEG.Free;
try
adoquery1.insert ;
TBlobField(adoquery1.FieldbyName('Image')).LoadFromStream(MS);
adoquery1.Post;
finally
ms.Free
end;
end;
end;procedure TForm1.BitBtn2Click(Sender: TObject);
begin
if not openpicturedialog1.Execute then exit;
image1.Picture.LoadFromFile(openpicturedialog1.FileName);end;procedure TForm1.BitBtn3Click(Sender: TObject); //这里的调出图片就出错了
Var
Ms:TMemoryStream;
MyJPEG:Tjpegimage;
begin
Ms:=TMemoryStream.Create;
TBlobField(ADOquery1.FieldByName('image')).SaveToStream(Ms);
Ms.Position :=0; MyJPEG.LoadFromStream(Ms); ///错误指在这一行。
Image2.Picture.Assign(MyJPEG);
Ms.Free;
MyJPEG.free;
end; ///错误警告是:Project project1.exe raised exception class EAccessViolation with message 'Access violation at address 004b621b in module 'Project1.exe'.Read of address 0000bd65'.Process stopped.u
Image2.Picture.Assign(MyJPEG);
Ms.Free;
------------------>>>>>>>>>>>>>>>>>>....
MyJpeg建立了吗?没Create就使用,当然要出错。
保存Image1中的图象至数据库 :
var
Ms:TmemoryStream;
jpg:Tjpegimage;
begin
ms:=TmemoryStream.Create;
Jpg.Assign(Image1.Picture.Graphic);
Jpg.SaveToStream(Ms) ;
Ms.Position :=0;
ADOquery1.append;
TBlobField(ADOquery1.FieldByName('img')).LoadFromStream(Ms);
ADOquery1.Post;
Ms.Free ;
jpg.free;
end;
从数据库中读取图象到image2中:
Var
Ms:TStringStream;
jpg:Tjpegimage;
begin
Ms:=TstringStream.Create('');
TBlobField(ADOquery1.FieldByName('img')).SaveToStream(Ms);
Ms.Position :=0;
Jpg.LoadFromStream(Ms);
Image2.Picture.Assign(Jpg);
Ms.Free;
jpg.free;
end;////////