C
caochen
Unregistered / Unconfirmed
GUEST, unregistred user!
好像不行,感觉没什么错误,帮我看看:
procedure SaveImage(ADOTable:TADOTable;Image:TImage);
Var
BlobStream:TADOBlobStream;
JpegImage:TJpegImage;
begin
try
JpegImage:=TJpegImage.Create;
BlobStream:=TADOBlobStream.Create(ADOTable.FieldByName('goods_pic') as tblobfield,bmWrite);
BlobStream.Position:=0;
BlobStream.Truncate;
JpegImage:=TJpegImage(Image.Picture.Graphic);
JpegImage.SaveToStream(BlobStream);
finally
if Not Assigned(BlobStream) then BlobStream.Free;
if Not Assigned(JpegImage) then JpegImage.Free;
end;
end;
//读取图片
procedure LoadImage(ADOTable:TADOTable;Image:TImage);
Var
BlobStream:TADOBlobStream;
JpegImage:TJpegImage;
begin
try
BlobStream:=TADOBlobStream.Create(ADOTable.FieldByName('goods_pic') as tblobfield,bmRead);
if BlobStream.Size>0 then
begin
JpegImage:=TJpegImage.Create;
JpegImage.LoadFromStream(BlobStream);
Image.Picture.Assign(JpegImage);
end
else
Image.Picture:=nil;
Finally
if Not Assigned(BlobStream) then BlobStream.Free;
if Not Assigned(JpegImage) then JpegImage.Free;
End;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenPictureDialog1.Execute then
begin
Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('select * from depot_goods_picture');
ADOQuery1.SQL.Add('where goods_id='+''''+DBEdit1.Text+'''');
ADOQuery1.Open;
if not ADOQuery1.IsEmpty then
begin
ADOTable2.Edit;
SaveImage(ADOTable2,Image1);
end
else
begin
ADOTable2.Append;
SaveImage(ADOTable2,Image1);
end;
end;
end;
procedure TForm1.ADOTable1AfterScroll(DataSet: TDataSet);
begin
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('select * from depot_goods_picture');
ADOQuery1.SQL.Add('where goods_id='+''''+DBEdit1.Text+'''');
ADOQuery1.Open;
if not ADOQuery1.IsEmpty then
begin
if ADOTable2goods_pic.BlobSize>0 then
LoadImage(ADOTable2,Image1);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ADOConnection1.Connected:=True;
ADOTable1.Open;
ADOTable1.First;
ADOTable2.Open;
Image1.Picture.RegisterFileFormat('JPG','JPG',TJPEGImage);
Image1.Picture.RegisterFileFormat('JPEG','JPG',TJPEGImage);
end;
procedure TForm1.DataSource2DataChange(Sender: TObject; Field: TField);
begin
if ADOTable2.State in [dsInsert,dsEdit] then
Button2.Enabled:=True
else
Button2.Enabled:=False;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ADOTable1.Close;
ADOTable2.Close;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Close;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Try
ADOTable2.Post;
except
end;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
ADOTable2.Edit;
ADOTable2.FieldByName('goods_pic').AsString:='';
ADOTable2.Post;
end;
procedure SaveImage(ADOTable:TADOTable;Image:TImage);
Var
BlobStream:TADOBlobStream;
JpegImage:TJpegImage;
begin
try
JpegImage:=TJpegImage.Create;
BlobStream:=TADOBlobStream.Create(ADOTable.FieldByName('goods_pic') as tblobfield,bmWrite);
BlobStream.Position:=0;
BlobStream.Truncate;
JpegImage:=TJpegImage(Image.Picture.Graphic);
JpegImage.SaveToStream(BlobStream);
finally
if Not Assigned(BlobStream) then BlobStream.Free;
if Not Assigned(JpegImage) then JpegImage.Free;
end;
end;
//读取图片
procedure LoadImage(ADOTable:TADOTable;Image:TImage);
Var
BlobStream:TADOBlobStream;
JpegImage:TJpegImage;
begin
try
BlobStream:=TADOBlobStream.Create(ADOTable.FieldByName('goods_pic') as tblobfield,bmRead);
if BlobStream.Size>0 then
begin
JpegImage:=TJpegImage.Create;
JpegImage.LoadFromStream(BlobStream);
Image.Picture.Assign(JpegImage);
end
else
Image.Picture:=nil;
Finally
if Not Assigned(BlobStream) then BlobStream.Free;
if Not Assigned(JpegImage) then JpegImage.Free;
End;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenPictureDialog1.Execute then
begin
Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('select * from depot_goods_picture');
ADOQuery1.SQL.Add('where goods_id='+''''+DBEdit1.Text+'''');
ADOQuery1.Open;
if not ADOQuery1.IsEmpty then
begin
ADOTable2.Edit;
SaveImage(ADOTable2,Image1);
end
else
begin
ADOTable2.Append;
SaveImage(ADOTable2,Image1);
end;
end;
end;
procedure TForm1.ADOTable1AfterScroll(DataSet: TDataSet);
begin
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('select * from depot_goods_picture');
ADOQuery1.SQL.Add('where goods_id='+''''+DBEdit1.Text+'''');
ADOQuery1.Open;
if not ADOQuery1.IsEmpty then
begin
if ADOTable2goods_pic.BlobSize>0 then
LoadImage(ADOTable2,Image1);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ADOConnection1.Connected:=True;
ADOTable1.Open;
ADOTable1.First;
ADOTable2.Open;
Image1.Picture.RegisterFileFormat('JPG','JPG',TJPEGImage);
Image1.Picture.RegisterFileFormat('JPEG','JPG',TJPEGImage);
end;
procedure TForm1.DataSource2DataChange(Sender: TObject; Field: TField);
begin
if ADOTable2.State in [dsInsert,dsEdit] then
Button2.Enabled:=True
else
Button2.Enabled:=False;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ADOTable1.Close;
ADOTable2.Close;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Close;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Try
ADOTable2.Post;
except
end;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
ADOTable2.Edit;
ADOTable2.FieldByName('goods_pic').AsString:='';
ADOTable2.Post;
end;