大家看看我这段图像存取有什么问题?(30分)

  • 主题发起人 主题发起人 caochen
  • 开始时间 开始时间
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;
 
错的太多了!!!
function Image2DbJpg(myImage: TImage; sFieldID: string = '';
sFieldName: string = ''): Boolean;
var
jpg: TJpegImage;
Strm: TMemoryStream;
begin
// Result := False;
Jpg := TJpegImage.Create;
Jpg.Assign(myImage.Picture.Graphic);
Jpg.CompressionQuality := 75;
Jpg.Compress;
//Application.ProcessMessages;
Strm := TMemoryStream.Create;
Jpg.SaveToStream(Strm);
if Strm.Size > 220000 then
ShowMessage('图象文件太大,请减少文件大小'#13 + ' 操作取消!!!')
else if Strm.Size = 0 then
ShowMessage('没有图象文件,请先选择文件'#13 + ' 操作取消!!!')
else
begin
Strm.Position := 0;
with DataModule1 do
begin
if Length(sFieldID) = 9 then
begin
if not atblPicture.Active then
atblPicture.Active := True;
if atblPicture.Locate('NAME', sFieldID, []) then
atblPicture.Edit
else
atblPicture.Append;
end
else
atblPicture.Edit;
if length(sFieldID) > 0 then
atblPicture.FieldValues['NAME'] := sFieldID;
if length(sFieldName) > 0 then
atblPicture.FieldValues['CNAME'] := sFieldName;
atblPicturePicture.LoadFromStream(strm);
atblPicture.Post;
atblPicture.Next;
end;
end;
Strm.Free;
Jpg.Free;
Result := True;
end;

function DbJpg2Image(myImage: TImage; sFieldID: string = '';
sFieldName: string = '') : Boolean;
var
Strm: TMemoryStream;
jpg: TJpegImage;
begin
// Result := False;
if Length(sFieldID) = 9 then
begin
if not atblPicture.Active then
atblPicture.Active := True;
if not (atblPicture.FieldValues['NAME'] = sFieldID) then
begin
if not atblPicture.Locate('NAME', sFieldID, []) then
begin
atblPicture.Append;
atblPicture.FieldValues['NAME'] := sFieldID;
atblPicture.FieldValues['CNAME'] := sFieldName;
atblPicture.Post;
end;
end;
end;
jpg := TJpegImage.Create;
Strm := TMemoryStream.Create();
atblPicturePicture.SaveToStream(Strm);
if not (strm.Size = 0) then
begin
Strm.Position := 0;
Jpg.LoadFromStream(Strm);
myImage.Picture.Graphic := jpg;
end
else
begin
MyImage.Picture := nil;
myImage.Canvas.TextOut((myImage.Width div 2) - 20, (myImage.Height div 2) -
10, '没有图片');
end;
Strm.Free;
Jpg.Free;
Result := True;
end;
 
//存入
Jpeg.LoadFromFile(ExtractFilePath(application.Name)+'Temp.Jpg');
//Image1.Picture.Bitmap.Assign(Jpeg);


Ado_Data.ADOConnection1.BeginTrans;
Try
with AdoCommand do
begin
CommandText:='Insert Into Table (Picture) Values (:Picture)';
//Jpeg.
//TBlobField(Parameters.ParamByName('Picture').Assign(Jpeg));

//Parameters.ParamByName('Picture').LoadFromFile(strFileName,ftBlob);
Parameters.ParamByName('Picture').DataType:=ftBlob;
Parameters.ParamByName('Picture').Assign(Jpeg);
//Image1.picture
Execute;
end;
Result:=True;
Ado_Data.ADOConnection1.CommitTrans;
Except
Result:=False;
Ado_Data.ADOConnection1.RollbackTrans;
end;


//读出
procedure TForm_Document_Info.Show_Data(Show_FormNO:Boolean);
Var
stream :TMemoryStream;
//StrNo:String;
begin
with Form_Document.ADOQuery_ProduceData do
begin

stream :=TMemoryStream.Create;
tblobfield(FieldByName('Picture')).SaveToStream(Stream);
Stream.Seek(0,soFromBeginning);
jpeg.loadfromstream(stream);
stream.Free;
//赋值
end;

image1.Picture.Bitmap.Assign(jpeg);

end;
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
571
import
I
I
回复
0
查看
566
import
I
I
回复
0
查看
709
import
I
后退
顶部