图形保存问题,请帮我看一下,好吗?!!谢谢!! (10分)

  • 主题发起人 主题发起人 老人头
  • 开始时间 开始时间

老人头

Unregistered / Unconfirmed
GUEST, unregistred user!
delphi +sql server2000 怎样存储(保存+显示)图片啊? 急呀!

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Table1: TTable;
Image1: TImage;
Button1: TButton;
Button2: TButton;
DataSource1: TDataSource;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
FDataLink:TFieldDataLink
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
opendialog1.Execute;
image1.Picture.LoadFromFile(opendialog1.filename);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
MyJPEG : TJPEGImage;
MS: TMemoryStream;
begin
MyJPEG := TJPEGImage.Create;
try
with MyJPEG do
begin
Assign(Image1.Picture.Graphic);
MS:=TMemoryStream.create;
SaveToStream(MS);
MS.Position:=0;
Table1.Edit;

TBlobField(Table1.FieldbyName('tu')).LoadFromStream(MS);
Table1.Post;

end;
finally
MyJPEG.Free;
end;
end;

end.

///////////////////////////////////////

procedure TDBImage.LoadPicture;
type TGraphicFmt=(gfUnKnown,gfBitmap,gfJpg);
var BlobStrm: TStream;
GraphicFmt: TGraphicFmt;
procedure getGraphicClass;
var b1,b2,b3: Byte;
begin
//Bmp:42 4d Jpg:FF D8 Gif: 47 49 46 wmf: D7 CD Ico:00 00 01
BlobStrm.Seek(0,0);
BlobStrm.Read(B1,1);
BlobStrm.Read(B2,1);
BlobStrm.Read(B3,1);
GraphicFmt:=gfUnknown;
if (B1=$42) and (B2=$4d) then GraphicFmt:=gfBitmap
else if (B1=$FF) and (B2=$D8) then GraphicFmt:=gfJpg;
end;
begin
if not FPictureLoaded and (not Assigned(FDataLink.Field) or
FDataLink.Field.IsBlob) then
// Picture.Assign(FDataLink.Field);
begin
if Assigned(FDataLink.Field) and(not FDataLink.Field.IsNull) then
begin
BlobStrm:=FDataLink.DataSet.CreateBlobStream(FDataLink.Field, bmRead);
try
GraphicFmt:=gfUnknown;
if BlobStrm.Size > 10 then getGraphicClass;
case GraphicFmt of
gfJpg:begin
if (Picture.Graphic=nil)or(not (Picture.Graphic is TJpegImage)) then
begin
if Picture.Graphic<>nil then Picture.Graphic.Free;
Picture.Graphic:=TJpegImage.Create;
end;
end;
gfBitmap:begin
if (Picture.Graphic=nil)or(not (Picture.Graphic is TBitmap)) then
begin
if Picture.Graphic<>nil then Picture.Graphic.Free;
Picture.Graphic:=TBitmap.Create;
end;
end;
else begin
if Picture.Graphic<>nil then Picture.Graphic.Free;
Picture.Graphic:=nil;
end;
end;
if Picture.Graphic<>nil then
begin
BlobStrm.Seek(0,0);
Picture.Graphic.LoadFromStream(BlobStrm);
end else Paint;
finally
BlobStrm.Free;
end;
end
else Picture.Bitmap.Assign(nil);
end;
end;
procedure TDBImage.UpdateData(Sender: TObject);
var BlobStrm: TStream;
begin
//if Picture.Graphic is TBitmap then
// FDataLink.Field.Assign(Picture.Graphic) else
if Picture.Graphic<>nil then
begin
BlobStrm := FDataLink.DataSet.CreateBlobStream(FDataLink.Field, bmWrite);
try
Picture.Graphic.SaveToStream(BlobStrm);
finally
BlobStrm.Free;
end;
end else
FDataLink.Field.Clear;
end;
请问在装jpg图象时,它为何会提示
BITMAP IMAGE IS NOT VALID?
麻烦您们帮我看一下啦!!!
 
用BLOB字段,和TDBimage控件,openfilebox和tdbnavigator控件只需设置属性就可以了。
 
gjdemj2000您好!!
如果我用的是SQL SERVER2000,那么数据库又应该用哪一种数据类型代替BLOB字段啊!?
 
TDBImage缺省不支持Jpeg图,你可以修改一下使用:
修改TDBImage的源码,改:
procedure TDBImage.LoadPicture;
type TGraphicFmt=(gfUnKnown,gfBitmap,gfJpg);
var BlobStrm: TStream;
GraphicFmt: TGraphicFmt;
procedure getGraphicClass;
var b1,b2,b3: Byte;
begin
//Bmp:42 4d Jpg:FF D8 Gif: 47 49 46 wmf: D7 CD Ico:00 00 01
BlobStrm.Seek(0,0);
BlobStrm.Read(B1,1);
BlobStrm.Read(B2,1);
BlobStrm.Read(B3,1);
GraphicFmt:=gfUnknown;
if (B1=$42) and (B2=$4d) then GraphicFmt:=gfBitmap
else if (B1=$FF) and (B2=$D8) then GraphicFmt:=gfJpg;
end;
begin
if not FPictureLoaded and (not Assigned(FDataLink.Field) or
FDataLink.Field.IsBlob) then
// Picture.Assign(FDataLink.Field);
begin
if Assigned(FDataLink.Field) and(not FDataLink.Field.IsNull) then
begin
BlobStrm:=FDataLink.DataSet.CreateBlobStream(FDataLink.Field, bmRead);
try
GraphicFmt:=gfUnknown;
if BlobStrm.Size > 10 then getGraphicClass;
case GraphicFmt of
gfJpg:begin
if (Picture.Graphic=nil)or(not (Picture.Graphic is TJpegImage)) then
begin
if Picture.Graphic<>nil then Picture.Graphic.Free;
Picture.Graphic:=TJpegImage.Create;
end;
end;
gfBitmap:begin
if (Picture.Graphic=nil)or(not (Picture.Graphic is TBitmap)) then
begin
if Picture.Graphic<>nil then Picture.Graphic.Free;
Picture.Graphic:=TBitmap.Create;
end;
end;
else begin
if Picture.Graphic<>nil then Picture.Graphic.Free;
Picture.Graphic:=nil;
end;
end;
if Picture.Graphic<>nil then
begin
BlobStrm.Seek(0,0);
Picture.Graphic.LoadFromStream(BlobStrm);
end else Paint;
finally
BlobStrm.Free;
end;
end
else Picture.Bitmap.Assign(nil);
end;
end;
procedure TDBImage.UpdateData(Sender: TObject);
var BlobStrm: TStream;
begin
//if Picture.Graphic is TBitmap then
// FDataLink.Field.Assign(Picture.Graphic) else
if Picture.Graphic<>nil then
begin
BlobStrm := FDataLink.DataSet.CreateBlobStream(FDataLink.Field, bmWrite);
try
Picture.Graphic.SaveToStream(BlobStrm);
finally
BlobStrm.Free;
end;
end else
FDataLink.Field.Clear;
end;

 
可能是你的图象格式不正确,你试试用jpg等格式!
 
TYZhang
gjdemj2000
快乐每一天
三位好!!
谢谢您们的指点!!
希望您们以后多多支持我!!
多多关注我的问题!
谢谢!!
 

Similar threads

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