关于从数据库读取图形文件的问题(100分)

  • 主题发起人 主题发起人 heimukai850414
  • 开始时间 开始时间
H

heimukai850414

Unregistered / Unconfirmed
GUEST, unregistred user!
本人刚开学学习delphi,望达人指点
我想用Image组件读取Blob字段中的图片信息,并将图片显示出来,对于JPEG图片,通过TJPegImage类进行中间过渡,通过Image组件的Canvas属性绘制出Jpeg图像
程序运行的时候报错:raised exception class EJPEG with message 'JPEG error #42'
我不知道该怎么做,麻烦达人看一下(用Access)
源程序:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, DBCtrls, ExtCtrls, Grids, DBGrids, Db, DBTables, Buttons, jpeg,
ExtDlgs;
type
TForm1 = class(TForm)
Table1: TTable;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
OpenDialog1: TOpenDialog;
Database1: TDatabase;
Button1: TButton;
DBNavigator1: TDBNavigator;
Image1: TImage;

procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Table1AfterScroll(DataSet: TDataSet);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if opendialog1.Execute then
begin
table1.Edit;
table1.Fields[1].Clear;
Tblobfield(table1.Fields[1]).loadfromfile(opendialog1.FileName);
Table1.Fields[2].AsString:=extractfileext(opendialog1.FileName);
table1.Post;
end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Table1.Close;
database1.Close;
end;

procedure TForm1.Table1AfterScroll(DataSet: TDataSet);
var
I: TJPegImage;

begin
Image1.Picture.Assign(nil);
//针对Jpeg图象的处理方法
//通过TJPegImage过渡,并且通过Canvas属性绘制图象。
if SameText(Table1.Fields[2].AsString, '.JPG') then
begin
I := TJpegImage.Create;
TBlobField(Table1.Fields[1]).SaveToFile('d:/1.Jpg');
//构建TJpegImage对象 I
I.LoadFromFile('d:/1.JPG');
//绘制Image1。
Image1.Canvas.Draw(0, 0, I);
I.Free;
end
else if SameText(Table1.Fields[2].AsString, '.BMP') then
//BMP格式直接Assign处理。
Image1.Picture.bitmap.Assign(TBlobField(Table1.Fields[1]));

end;

end.


主要组件对象属性如下:
object Form1: TForm1
object Image1: TImage
AutoSize = True
end
object DBGrid1: TDBGrid
Align = alBottom
DataSource = DataSource1
end
object Button1: TButton
end
object DBNavigator1: TDBNavigator
DataSource = DataSource1
end
object Table1: TTable
Active = True
AfterScroll = Table1AfterScroll
DatabaseName = 'b1'
TableName = 'b3'
end
object DataSource1: TDataSource
DataSet = Table1
end
object OpenDialog1: TOpenDialog
end
object Database1: TDatabase
AliasName = 'b1'
Connected = True
DatabaseName = 'b4'
LoginPrompt = False
Params.Strings = (
'DATABASE NAME='
'USER NAME='
'ODBC DSN=b'
'OPEN MODE=READ/WRITE'
'BATCH COUNT=200'
'LANGDRIVER='
'MAX ROWS=-1'
'SCHEMA CACHE DIR='
'SCHEMA CACHE SIZE=8'
'SCHEMA CACHE TIME=-1'
'SQLPASSTHRU MODE=SHARED AUTOCOMMIT'
'SQLQRYMODE='
'ENABLE SCHEMA CACHE=FALSE'
'ENABLE BCD=FALSE'
'ROWSET SIZE=20'
'BLOBS TO CACHE=64'
'BLOB SIZE=32'
'PASSWORD=')
SessionName = 'Default'
end
end
 
procedure TForm1.Table1AfterScroll(DataSet: TDataSet);
var
I: TJPegImage;

Pics :TMemoryStream; //========加一个变量,这样你直接用内存,不要去存盘.
J: TBitmap; //========中转一下

begin
Image1.Picture.Assign(nil);
//针对Jpeg图象的处理方法
//通过TJPegImage过渡,并且通过Canvas属性绘制图象。
if SameText(Table1.Fields[2].AsString, '.JPG') then
begin
I := TJpegImage.Create;
J := TBitmap.Create;


{ TBlobField(Table1.Fields[1]).SaveToFile('d:/1.Jpg');
//构建TJpegImage对象 I
I.LoadFromFile('d:/1.JPG'); }
//===============这段改成如下==========================
TBlobField(Table1.Fields[1]).SaveToStream(Pics);
Pics.Seek(0,soFromBeginning);
I.LoadFromStream(Pics);
//=========然后直接Assign, 如果要缩放,先用 J.Canvas.StretchDraw================
J.Assign(I);
Image1.width := J.width;
Image1.heigth := J.heigth;
Image1.Picture.Bitmap := J;

{ //绘制Image1。
Image1.Canvas.Draw(0, 0, I); }

//==========最后记得释放所有Pics bitmap 和 jpeg对象=================

I.Free;
end
else if SameText(Table1.Fields[2].AsString, '.BMP') then
//BMP格式直接Assign处理。
Image1.Picture.bitmap.Assign(TBlobField(Table1.Fields[1]));

end;
 

Similar threads

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