一个简单而又重要的问题(常见的) 分好说 ( 积分: 50 )

  • 主题发起人 主题发起人 tommy27149
  • 开始时间 开始时间
T

tommy27149

Unregistered / Unconfirmed
GUEST, unregistred user!
简单是因为有众多高手在,重要是因为经理天天盯着
各位弟兄,救救小弟吧!!
我编写了一个关于照片存取数据库的程序可是老是报错目前主要的错是以下几种:
存入时出现的stream read error! Bitmap image is not valid 读取时出现JPEG error #53
我的代码是这样的
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DBCtrls, DB, ADODB, Grids, DBGrids, ExtCtrls, StdCtrls,jpeg,
ExtDlgs;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Image1: TImage;
Image2: TImage;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
ADOQuery1: TADOQuery;
DBImage1: TDBImage;
OpenPictureDialog1: TOpenPictureDialog;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);//存入数据库
var
MyJPEG : TJPEGImage;
MS: TMemoryStream;
begin
if OpenPictureDialog1.Execute then

Image1.picture.loadfromfile(OpenPictureDialog1.FileName);
MyJPEG:=TJPEGImage.Create;
Try
MyJPEG.Assign(Image1.Picture.Graphic); {图像转成JPEG格式,如为JPEG图片则不必加}
MS:=TMemoryStream.Create;
MyJPEG.SaveToStream(MS);
MS.Position:=0;
ADOQuery1.Append;
ADOQuery1.FieldByName('image').Value:='1';
TBlobField(ADOQuery1.FieldByName('image')).LoadFromStream(MS);
ADOQuery1.Post;
messagebox(getactivewindow(),'图像保存完毕!','保存',mb_ok);
MS.Free;
Finally
MyJPEG.Free;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);读取图片
var
MyJPEG : TJPEGImage;
MS: TMemoryStream;
begin
MS:=TMemoryStream.Create;
MyJPEG:=TJPEGImage.Create;
ADOQuery1.Requery;
try
TBlobField(ADOQuery1.FieldByName('image')).SaveToStream(MS);
MS.Position:=0;
MyJPEG.LoadFromStream(MS);
Image2.Picture.Bitmap.Assign(MyJPEG);
finally
MS.Free;
MyJPEG.Free;
end;
end;

end.
敬请各位仁兄鼎立相助 帮小弟度过这一难关不胜感激!
 
简单是因为有众多高手在,重要是因为经理天天盯着
各位弟兄,救救小弟吧!!
我编写了一个关于照片存取数据库的程序可是老是报错目前主要的错是以下几种:
存入时出现的stream read error! Bitmap image is not valid 读取时出现JPEG error #53
我的代码是这样的
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DBCtrls, DB, ADODB, Grids, DBGrids, ExtCtrls, StdCtrls,jpeg,
ExtDlgs;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Image1: TImage;
Image2: TImage;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
ADOQuery1: TADOQuery;
DBImage1: TDBImage;
OpenPictureDialog1: TOpenPictureDialog;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);//存入数据库
var
MyJPEG : TJPEGImage;
MS: TMemoryStream;
begin
if OpenPictureDialog1.Execute then

Image1.picture.loadfromfile(OpenPictureDialog1.FileName);
MyJPEG:=TJPEGImage.Create;
Try
MyJPEG.Assign(Image1.Picture.Graphic); {图像转成JPEG格式,如为JPEG图片则不必加}
MS:=TMemoryStream.Create;
MyJPEG.SaveToStream(MS);
MS.Position:=0;
ADOQuery1.Append;
ADOQuery1.FieldByName('image').Value:='1';
TBlobField(ADOQuery1.FieldByName('image')).LoadFromStream(MS);
ADOQuery1.Post;
messagebox(getactivewindow(),'图像保存完毕!','保存',mb_ok);
MS.Free;
Finally
MyJPEG.Free;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);读取图片
var
MyJPEG : TJPEGImage;
MS: TMemoryStream;
begin
MS:=TMemoryStream.Create;
MyJPEG:=TJPEGImage.Create;
ADOQuery1.Requery;
try
TBlobField(ADOQuery1.FieldByName('image')).SaveToStream(MS);
MS.Position:=0;
MyJPEG.LoadFromStream(MS);
Image2.Picture.Bitmap.Assign(MyJPEG);
finally
MS.Free;
MyJPEG.Free;
end;
end;

end.
敬请各位仁兄鼎立相助 帮小弟度过这一难关不胜感激!
 
保存相片
procedure TForm3.BitBtn2Click(Sender: TObject);
var ms:tmemorystream;
begin
if (edit1.Text<>'') and (edit6.Text<>'') then //用户性名和用户编号不能为空
begin
ms:=tmemorystream.Create;
dbimage1.Picture.Bitmap.SaveToStream(ms);
ms.Position:=0;
table1.append;
table1.FieldByName('用户姓名').AsString:=edit1.Text;
table1.FieldByName('籍贯').AsString:=edit2.Text;
table1.FieldByName('年龄').AsString:=edit3.Text;
table1.FieldByName('地址').AsString:=edit4.Text;
table1.FieldByName('证件号码').AsString:=edit5.Text;
table1.FieldByName('用户编号').AsString:=edit6.Text;
table1.FieldByName('用户密码').AsString:=edit7.Text;
tblobfield(table1.FieldByName('相片')).LoadFromStream(ms);
xingbiehan();
table1.Post;
//增加用户过程
messagebox(0,'增加成功','提示',mb_ok);
end
else
begin
messagebox(0,'姓名和编号不能为空','注意',mb_ok);
end;
end;

取出相片
Image2.Picture.Graphic:=nil;
Application.ProcessMessages;
photofrom.Show;
ADOphoto.Active:=false;
ADOphoto.Close;
sqlphoto := 'select provider_no, provider_photo from base_provider ' +
' where provider_no=' + Quotedstr(trim(Edit_no.Text)) + '';
ADOphoto.SQL.Text := sqlphoto;
ADOphoto.Active:=true;
if ADOphoto.FieldByName('provider_photo').Value='' then exit;
Image2.Picture:=DBImage1.Picture;
// dbimage1.Picture.Graphic:=nil;
end;
 
多谢了 我试一下啊
 
你取图片的时候先用个bitmap,成功了以后再放到jpg里,用了两次try,这样可以捕捉到这个异常的吧,不处理就可以了,我以前遇到的是#41错误,
你也可以查查你的错误的含义,好象是你的图片格式不是jpg形式的,在头部位置就不对了。。
 
确实简单
 
lich兄弟 怎么扔一句拽话就走了呢 好歹帮忙出点主意撒
 
多谢xirhru大侠相助 可是这个程序怎么只能存BMP的 那JPEG的怎么办呢 比如数码登记照?
不好意思 让大家见笑了 刚刚入行 请各位原谅
 
接受多人答案
 
KeyLife富翁笔记
作者?: jianguobu
标题?: 把BMP,JPG图片保存到数据库中及从数据库中取出来显示在iamge上.
关键字: BMP,JPG,保存,iamge,
分类?: 个人专区
密级?: 公开
(评分: , 回复: 4, 阅读: 377) &amp;raquo;&amp;raquo;
procedure TInputResultForm.SaveClick(Sender: TObject);
var
JPeg: TJPegImage;
Bmp: TBitmap;
ff: Tgraphicfield; //存放bmp的字段
mm: tmemorystream;
begin
try
Bmp := TBitmap.Create;
JPeg := TJPegimage.Create;
mm:=Tmemorystream.create;
DM.adoExeAttch.Edit;
if OpenDialog1.Execute then
if UpperCase(copy(Opendialog1.FileName,length(Opendialog1.FileName)-2,3))='BMP' then begin
Bmp.LoadFromFile(Opendialog1.FileName);
JPeg.Assign(Bmp);
JPeg.savetostream(mm);
TBlobField(DM.adoExeAttch.FieldByName('Image1')).LoadFromStream(mm);
end else begin
JPeg.LoadFromFile(Opendialog1.FileName);
TBlobField(DM.adoExeAttch.FieldByName('Image1')).loadfromfile(Opendialog1.FileName);
end;
Image1.Picture.Assign(jpeg);
finally
JPeg.Free;
mm.free;
Bmp.Free;
end;
end;

procedure TInputResultForm.LoadClick(Sender: TObject);
var
m:TMemoryStream;
jpg:TJpegImage;
begin
inherited;
m := TmemoryStream.Create;
if TBlobField(DM.adoExeAttch.FieldByName('Image1')).BlobSize>0 then
try
(DM.adoExeAttchImage1 as TBlobField).SaveToStream(m);
jpg := TJpegImage.Create;
m.Position := 0;
jpg.LoadFromStream(m);
Image1.Picture.Assign(jpg);
jpg.Free;
except
showmessage('供应商上传的图片格式不正确,无法浏览!')
end else
Image1.Picture.Assign(nil);
m.Free;
end;
 
这个不行
 

Similar threads

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