procedure TForm4.Button4Click(Sender: TObject); //保存图片
var
Streaa:TMemoryStream;
Streab:TMemoryStream;
Streac:TMemoryStream;
Stread:TMemoryStream;beginif Application.MessageBox('确认保存信息?', '提示!', MB_OKCANCEL) = IDOK then try
Streaa := TMemoryStream.Create;
Streab := TMemoryStream.Create;
Streac := TMemoryStream.Create;
Stread := TMemoryStream.Create; // 创建内存流 if image1.Picture.Graphic<>nil then begin
Image1.Picture.Graphic.SaveToStream(Streaa);
end;
if image2.Picture.Graphic<>nil then begin
Image2.Picture.Graphic.SaveToStream(Streab);
end;
if image3.Picture.Graphic<>nil then begin
Image3.Picture.Graphic.SaveToStream(Streac);
end;
if image4.Picture.Graphic<>nil then begin
Image4.Picture.Graphic.SaveToStream(Stread); // 将图片保存到内存流中
end; adoquery1.Close;
adoquery1.SQL.Clear;
adoQuery1.SQL.Add('Insert into X_foster values (:foster_name,:foster_labour,:foster_date,:foster_post,:foster_stipend,:foster_content,:foster_circs,:foster_result,:factory_image,:degree_image,:community_image,:exuberance_image)');
ADOQuery1.Parameters.ParamByName('foster_name').Value := Edit1.Text;
ADOQuery1.Parameters.ParamByName('foster_labour').Value := Edit2.Text;
ADOQuery1.Parameters.ParamByName('foster_date').Value := wwDBDateTimePicker1.Text;
ADOQuery1.Parameters.ParamByName('foster_post').Value := Edit4.Text;
ADOQuery1.Parameters.ParamByName('foster_stipend').Value := Edit5.Text;
ADOQuery1.Parameters.ParamByName('foster_content').Value := memo1.Text;
ADOQuery1.Parameters.ParamByName('foster_circs').Value := memo2.Text;
ADOQuery1.Parameters.ParamByName('foster_result').Value := memo3.Text; adoQuery1.Parameters.ParamByName('factory_image').LoadFromStream(Streaa,ftBlob);
adoQuery1.Parameters.ParamByName('degree_image').LoadFromStream(Streab,ftBlob);
adoQuery1.Parameters.ParamByName('community_image').LoadFromStream(Streac,ftBlob);
adoQuery1.Parameters.ParamByName('exuberance_image').LoadFromStream(Stread,ftBlob); // 读取保存的内存图
adoquery1.ExecSQL; showmessage('保存成功'); finally
Streaa.Free;
Streab.Free;
Streac.Free;
Stread.Free;
// 释放内存流
end;
end;
这样保存图片,不会有错误,但是再读取的时候就有问题了。
procedure TForm4.Button5Click(Sender: TObject); //查找图片
var
Streama:TMemoryStream;
Streamb:TMemoryStream;
Streamc:TMemoryStream;
Streamd:TMemoryStream; Jpg:TjpegImage;
begin ADOQuery1.Close;
ADOQuery1.SQL.Text:='SELECT foster_name,foster_labour,foster_date,foster_post,foster_stipend,foster_content,foster_circs,foster_result,factory_image,degree_image,community_image,exuberance_image FROM X_foster where foster_labour =''' + Edit2.Text + ''''; // 查询图片
ADOQuery1.Open;
if not ADOQuery1.FieldByName('foster_labour').IsNull then
begin
Edit1.Text:= ADOQuery1.FieldByName('foster_name').AsString;
Edit2.Text:= ADOQuery1.FieldByName('foster_labour').AsString;
wwDBDateTimePicker1.Date:= ADOQuery1.FieldByName('foster_date').AsDateTime;
Edit4.Text:= ADOQuery1.FieldByName('foster_post').AsString;
Edit5.Text:= ADOQuery1.FieldByName('foster_stipend').AsString;
Memo1.Text:= ADOQuery1.FieldByName('foster_content').AsString;
Memo2.Text:= ADOQuery1.FieldByName('foster_circs').AsString;
Memo3.Text:= ADOQuery1.FieldByName('foster_result').AsString; if not ADOQuery1.FieldByName('factory_image').IsNull then
begin
Streama:=TMemoryStream.Create ;
Jpg:=TjpegImage.Create ;
TBlobField(ADOQuery1.FieldByName('factory_image')).SaveToStream(Streama); // 显示的转换为BlobField并保存到内存流
Streama.Position :=0;
jpg.LoadFromStream(Streama); // 加载图片
image1.Picture.Assign(Jpg);
end
else
begin
image1.Picture :=nil;
end; if not ADOQuery1.FieldByName('degree_image').IsNull then
begin
Streamb:=TMemoryStream.Create ;
Jpg:=TjpegImage.Create ;
TBlobField(ADOQuery1.FieldByName('degree_image')).SaveToStream(Streamb); // 显示的转换为BlobField并保存到内存流
Streamb.Position :=0;
jpg.LoadFromStream(Streamb); // 加载图片 degree_image,community_image,exuberance_image
image2.Picture.Assign(Jpg);
end
else
begin
image2.Picture :=nil;
end; if not ADOQuery1.FieldByName('community_image').IsNull then
begin
Streamc:=TMemoryStream.Create ;
Jpg:=TjpegImage.Create ;
TBlobField(ADOQuery1.FieldByName('community_image')).SaveToStream(Streamc); // 显示的转换为BlobField并保存到内存流
Streamc.Position :=0;
jpg.LoadFromStream(Streamc); // 加载图片
image3.Picture.Assign(Jpg);
end
else
begin
image3.Picture :=nil;
end; if not ADOQuery1.FieldByName('exuberance_image').IsNull then
begin
Streamd:=TMemoryStream.Create ;
Jpg:=TjpegImage.Create;
TBlobField(ADOQuery1.FieldByName('exuberance_image')).SaveToStream(Streamd); // 显示的转换为BlobField并保存到内存流
Streamd.Position :=0;
jpg.LoadFromStream(Streamd); // 加载图片
image4.Picture.Assign(Jpg);
end
else
begin
image4.Picture :=nil;
end;
end
else
begin
showmessage('没有此员工的培训信息!');
end; Streama.Free;
Streamb.Free;
Streamc.Free;
Streamd.Free;
Jpg.Free;
end;这里我在上面判断了如果没有就不显示,但是数据库里面这个字段它会有个二进制的符号在那。
0x这个,那么程序就会默认为有图片但是他进行显示的时候又不能显示就会报错,请高手教教这里应该怎么弄?
var
Streaa:TMemoryStream;
Streab:TMemoryStream;
Streac:TMemoryStream;
Stread:TMemoryStream;beginif Application.MessageBox('确认保存信息?', '提示!', MB_OKCANCEL) = IDOK then try
Streaa := TMemoryStream.Create;
Streab := TMemoryStream.Create;
Streac := TMemoryStream.Create;
Stread := TMemoryStream.Create; // 创建内存流 if image1.Picture.Graphic<>nil then begin
Image1.Picture.Graphic.SaveToStream(Streaa);
end;
if image2.Picture.Graphic<>nil then begin
Image2.Picture.Graphic.SaveToStream(Streab);
end;
if image3.Picture.Graphic<>nil then begin
Image3.Picture.Graphic.SaveToStream(Streac);
end;
if image4.Picture.Graphic<>nil then begin
Image4.Picture.Graphic.SaveToStream(Stread); // 将图片保存到内存流中
end; adoquery1.Close;
adoquery1.SQL.Clear;
adoQuery1.SQL.Add('Insert into X_foster values (:foster_name,:foster_labour,:foster_date,:foster_post,:foster_stipend,:foster_content,:foster_circs,:foster_result,:factory_image,:degree_image,:community_image,:exuberance_image)');
ADOQuery1.Parameters.ParamByName('foster_name').Value := Edit1.Text;
ADOQuery1.Parameters.ParamByName('foster_labour').Value := Edit2.Text;
ADOQuery1.Parameters.ParamByName('foster_date').Value := wwDBDateTimePicker1.Text;
ADOQuery1.Parameters.ParamByName('foster_post').Value := Edit4.Text;
ADOQuery1.Parameters.ParamByName('foster_stipend').Value := Edit5.Text;
ADOQuery1.Parameters.ParamByName('foster_content').Value := memo1.Text;
ADOQuery1.Parameters.ParamByName('foster_circs').Value := memo2.Text;
ADOQuery1.Parameters.ParamByName('foster_result').Value := memo3.Text; adoQuery1.Parameters.ParamByName('factory_image').LoadFromStream(Streaa,ftBlob);
adoQuery1.Parameters.ParamByName('degree_image').LoadFromStream(Streab,ftBlob);
adoQuery1.Parameters.ParamByName('community_image').LoadFromStream(Streac,ftBlob);
adoQuery1.Parameters.ParamByName('exuberance_image').LoadFromStream(Stread,ftBlob); // 读取保存的内存图
adoquery1.ExecSQL; showmessage('保存成功'); finally
Streaa.Free;
Streab.Free;
Streac.Free;
Stread.Free;
// 释放内存流
end;
end;
这样保存图片,不会有错误,但是再读取的时候就有问题了。
procedure TForm4.Button5Click(Sender: TObject); //查找图片
var
Streama:TMemoryStream;
Streamb:TMemoryStream;
Streamc:TMemoryStream;
Streamd:TMemoryStream; Jpg:TjpegImage;
begin ADOQuery1.Close;
ADOQuery1.SQL.Text:='SELECT foster_name,foster_labour,foster_date,foster_post,foster_stipend,foster_content,foster_circs,foster_result,factory_image,degree_image,community_image,exuberance_image FROM X_foster where foster_labour =''' + Edit2.Text + ''''; // 查询图片
ADOQuery1.Open;
if not ADOQuery1.FieldByName('foster_labour').IsNull then
begin
Edit1.Text:= ADOQuery1.FieldByName('foster_name').AsString;
Edit2.Text:= ADOQuery1.FieldByName('foster_labour').AsString;
wwDBDateTimePicker1.Date:= ADOQuery1.FieldByName('foster_date').AsDateTime;
Edit4.Text:= ADOQuery1.FieldByName('foster_post').AsString;
Edit5.Text:= ADOQuery1.FieldByName('foster_stipend').AsString;
Memo1.Text:= ADOQuery1.FieldByName('foster_content').AsString;
Memo2.Text:= ADOQuery1.FieldByName('foster_circs').AsString;
Memo3.Text:= ADOQuery1.FieldByName('foster_result').AsString; if not ADOQuery1.FieldByName('factory_image').IsNull then
begin
Streama:=TMemoryStream.Create ;
Jpg:=TjpegImage.Create ;
TBlobField(ADOQuery1.FieldByName('factory_image')).SaveToStream(Streama); // 显示的转换为BlobField并保存到内存流
Streama.Position :=0;
jpg.LoadFromStream(Streama); // 加载图片
image1.Picture.Assign(Jpg);
end
else
begin
image1.Picture :=nil;
end; if not ADOQuery1.FieldByName('degree_image').IsNull then
begin
Streamb:=TMemoryStream.Create ;
Jpg:=TjpegImage.Create ;
TBlobField(ADOQuery1.FieldByName('degree_image')).SaveToStream(Streamb); // 显示的转换为BlobField并保存到内存流
Streamb.Position :=0;
jpg.LoadFromStream(Streamb); // 加载图片 degree_image,community_image,exuberance_image
image2.Picture.Assign(Jpg);
end
else
begin
image2.Picture :=nil;
end; if not ADOQuery1.FieldByName('community_image').IsNull then
begin
Streamc:=TMemoryStream.Create ;
Jpg:=TjpegImage.Create ;
TBlobField(ADOQuery1.FieldByName('community_image')).SaveToStream(Streamc); // 显示的转换为BlobField并保存到内存流
Streamc.Position :=0;
jpg.LoadFromStream(Streamc); // 加载图片
image3.Picture.Assign(Jpg);
end
else
begin
image3.Picture :=nil;
end; if not ADOQuery1.FieldByName('exuberance_image').IsNull then
begin
Streamd:=TMemoryStream.Create ;
Jpg:=TjpegImage.Create;
TBlobField(ADOQuery1.FieldByName('exuberance_image')).SaveToStream(Streamd); // 显示的转换为BlobField并保存到内存流
Streamd.Position :=0;
jpg.LoadFromStream(Streamd); // 加载图片
image4.Picture.Assign(Jpg);
end
else
begin
image4.Picture :=nil;
end;
end
else
begin
showmessage('没有此员工的培训信息!');
end; Streama.Free;
Streamb.Free;
Streamc.Free;
Streamd.Free;
Jpg.Free;
end;这里我在上面判断了如果没有就不显示,但是数据库里面这个字段它会有个二进制的符号在那。
0x这个,那么程序就会默认为有图片但是他进行显示的时候又不能显示就会报错,请高手教教这里应该怎么弄?
TblobField(ADOQuery1.FieldByName('X')).Clear
else
TblobField(ADOQuery1.FieldByName('X')).LoadFromStream();读取的时候就可以这样判断了
if not ADOQuery1.FieldByName('X').IsNull then
Image4.Picture.Graphic.SaveToStream(Stread); // 将图片保存到内存流中
end;
我是这样判断的 。没有图片就不赋值了。
保存的时候是 adoquery1.Close;
adoquery1.SQL.Clear;
adoQuery1.SQL.Add('Insert into X_foster values (:foster_name,:foster_labour,:foster_date,:foster_post,:foster_stipend,:foster_content,:foster_circs,:foster_result,:factory_image,:degree_image,:community_image,:exuberance_image)');
ADOQuery1.Parameters.ParamByName('foster_name').Value := Edit1.Text;
ADOQuery1.Parameters.ParamByName('foster_labour').Value := Edit2.Text;
ADOQuery1.Parameters.ParamByName('foster_date').Value := wwDBDateTimePicker1.Text;
ADOQuery1.Parameters.ParamByName('foster_post').Value := Edit4.Text;
ADOQuery1.Parameters.ParamByName('foster_stipend').Value := Edit5.Text;
ADOQuery1.Parameters.ParamByName('foster_content').Value := memo1.Text;
ADOQuery1.Parameters.ParamByName('foster_circs').Value := memo2.Text;
ADOQuery1.Parameters.ParamByName('foster_result').Value := memo3.Text; adoQuery1.Parameters.ParamByName('factory_image').LoadFromStream(Streaa,ftBlob);
adoQuery1.Parameters.ParamByName('degree_image').LoadFromStream(Streab,ftBlob);
adoQuery1.Parameters.ParamByName('community_image').LoadFromStream(Streac,ftBlob);
adoQuery1.Parameters.ParamByName('exuberance_image').LoadFromStream(Stread,ftBlob); // 读取保存的内存图
adoquery1.ExecSQL;
这样
Image4.Picture.Graphic.SaveToStream(Stread); // 将图片保存到内存流中
end;同意>>>>>>>>>>>>>>
如果是这个类型,那就是你保存时在设置参数时出现的问题。检查以下变量的默认值.....
adoQuery1.Parameters.ParamByName('factory_image').LoadFromStream(Streaa,ftBlob);
adoQuery1.Parameters.ParamByName('degree_image').LoadFromStream(Streab,ftBlob);
adoQuery1.Parameters.ParamByName('community_image').LoadFromStream(Streac,ftBlob);
adoQuery1.Parameters.ParamByName('exuberance_image').LoadFromStream(Stread,ftBlob);
if image4.Picture.Graphic<>nil then begin
Stread := TMemoryStream.Create; // 创建内存流
Image4.Picture.Graphic.SaveToStream(Stread); // 将图片保存到内存流中
adoQuery1.Parameters.ParamByName('exuberance_image').LoadFromStream(Stread,ftBlob); // 读取保存的内存图
end;
这样就不会了。谢谢。