有关把图片存到数据库中!(分不够再加) (100分)

  • 主题发起人 主题发起人 宫本宝藏
  • 开始时间 开始时间

宫本宝藏

Unregistered / Unconfirmed
GUEST, unregistred user!
我是个初学者,想请教个大富翁:
点击Button控件,调用OpenDialog选择图象文件,然后把该文件添加到数据库中!
我也开了以前的帖,但还是有点不明白!
(最好能给出详细代码,分数不够在加!)
 
bitmap.savetostream(yourstream);
blobstream.assign(yourstream);
 
From Borland
Streaming Bitmaps and other Binary Data to BLOB Fields - by Borland Developer
Support Staff
Abstract:basics on using TBLOBStream to read/write records in a database
If you are reading this article you have probably discovered that reading and
writing to and from BLOB fields is not as straight forward as their
counterparts, integer and character. The wonderful implicit assignments to
integer and character fields just don't exist. Don't despair. Accessing data
in a BLOB is not as simple as other field types, but is by no means difficult.
TBLOBStream
The main difference in accessing BLOB data is the use of streams for the actual
data transfer. Once you have a reference to your Dataset's BLOB field, you
open a stream to it by passing that reference to TBLOBStream's Create method.
TBLOBStream descends from TStream and encapsulates the operations that allow
your applications to read from or write to Binary Large Object(BLOB) fields.

There are a few things that you should take into consideration when creating
your BLOB streams(s). One is that you should make sure to create a new instance
of TBLOBStream for every field you plan to read from and free it when you are
done. That other is that even though TBLOBStream lets you create instances of
it to use with any kind of DataSet, it is best to instead call the particular
DataSet's CreateBLOBStream method. It works exactly like TBLOBStream.Create,
but it instantiates an object specific for that Dataset(TTable,TQuery).
My examples will uses this method.

A Simple Example: Streaming Bitmaps
While BLOBs can hold many different kinds of data, including MEMOs and... well
just about anything you want, my example will focus on the streaming of bitmap
files. These concepts hold true for all types of data.

Using the BDE
First, start a new Delphi application and drop on your database components for
your connection. This will include a TDataBase and TQuery. Hook your Query to
your Database object and set it's RequestLive property to true, since we will
be inserting data. Now we will write your bitmap image to you database field.
First we will want a Bitmap to work with. After declaring a TBitmap, create it
and load a file with your favorite picture into it.


MyFavoriteBitmap := TBitmap.Create;
MyFavoriteBitmap.LoadFromFile('drive:pathMyFavoriteBitmap.bmp');


Once you have a Bitmap ready and waiting, you can go ahead and create your BLOB
stream. To do this, you will need to get a reference to your blobfield from the
query. If you know the name for the field, TDataSet.FindByName is the easiest.
Now Pass this reference into CreateBLOBStream along with the
BlobStreamMode(bmRead,bmWrite,bmReadWrite), which will return reference to a
TStream. We are going to use this to actually stream the file using TBitmap's
SaveToStream method.


procedure SavePictureToDatabase;
var
BlobField: TField;
BS: TStream;
begin
with Query1 do
begin
Insert;
BlobField := FieldByName('picturefield');
BS := CreateBlobStream(BlobField,bmWrite);
Bitmap.SavetoStream(BS);
Post;
end;
end;


Using dbExpress Those of you using the new dbExpress components will find the
process is identical with a few minor exceptions. If you are using TSQLQuery
or TSQLTable, you are enjoying their extremely fast execution for your
reporting applications. But since they do not cache their data, their is no
way for you to edit any records, let alone your BLOBS. Don't forget that you
must use TSQLClientDataSet instead of TSQLDataSet's to achieve this
functionality. In code, the difference is trivial for writing yourt picture
to a database.


procedure SavePictureToDatabase;
var
BlobField: TField;
BS: TStream;
begin
with SQLClientDataSet1 do
begin
Insert;
BlobField := FieldByName('picturefield');
BS := CreateBlobStream(BlobField,bmWrite);
Bitmap.SavetoStream(BS);
Post;
end;
end;


When you are ready to read you bitmap back out, use the built in TBitmap method
and you are set.


Blobfield := FieldbyName('picturefield');
BS := CreateBlobStream(BlobField,bmReadWrite);
MyFavoriteBitmap.LoadFromStream(BS);


Other types of binary data
When moving in large chunks of data that do not have convenient loading and
saving methods like TBitmap, you must employ the TStream methods
(Read, ReadBuffer, Write, WriteBuffer) manually. You can even stream
components and their properties using WriteComponent and ReadComponent,
were you so inclined.


var
MyComponent: TMyComponent //any TComponent
begin
MyComponent := TMyComponent.Create;
MyComponent.aProperty := aValue;
BS := CreateBlobStream(Blobfield,bmReadWrite);
BS.WriteComponent(MyComponent);
end;
There you have it. A quick intro to using BLOBs to store binary data.
I think the moral of the story is quite clear: Don't take candy from
strangers', kids.
 
这是抓屏
TCanvas *dtCanvas = new TCanvas;
dtCanvas->Handle=GetDC(0);

Graphics::TBitmap *bitmap=new Graphics::TBitmap;
bitmap->Width=Width;
bitmap->Height=Height;

int nColors=GetDeviceCaps(Canvas->Handle,SIZEPALETTE);
LOGPALETTE* logPal=(LOGPALETTE*)new Byte[sizeof(LOGPALETTE)+(nColors-1)*sizeof(PALETTEENTRY)];
logPal->palVersion=0x300; logPal->palNumEntries=(Word)nColors; GetSystemPaletteEntries(Canvas->Handle,0,nColors,logPal->palPalEntry);
bitmap->Palette=CreatePalette(logPal);
delete[] logPal;

TRect src=BoundsRect;
TRect dest=Rect(0,0,Width,Height);
bitmap->Canvas->CopyRect(dest,dtCanvas,src);

bitmap->SaveToFile("Screen.bmp");

delete bitmap;
delete dtCanvas;

这是保存
var
jpegs:TJpegImage;
mstream: tstringstream;
begin
Query1.Close ; //进行存储
Query1.SQL.Clear ;
jpegs:=TJpegImage.Create;
jpegs.Assign(image1.Picture.Graphic);
mstream:=Tstringstream.create('');
jpegs.SaveToStream(mStream);
//在query1的SQL属性里写上:tx_temp2
Query1.SQl.ADD('Insert Into Table1 ('picture')values (:tx_temp2)') ;
query1.Params[0].AsBlob:=mstream.DataString;
end;

显示
var
jpegs:TJpegImage;
mstream: tstringstream;
begin
//查找出你想显示的记录
jpegs:=TJpegImage.Create;
mStream :=Query1.CreateBlobStream(Query1.FieldByName('picture'),bmRead);
try
mstream.Position:=0;
if mstream.Size>0 then
begin
jpegs.LoadFromStream(mstream);
image2.Picture.Assign(jpegs);
end
else
begin
image2.Picture.Assign(nil);
end;
finally
jpegs.Free;
mstream.Free;
end;
end;

//注意在uses里加上jpeg
应该没问题的
 
就是可以直接从Stream和文件读取和存储
 
[:D]
哈哈~~
正好也是我要找的
感谢楼上的兄弟们

流还这么好用
 
//保存JPG图片到SQL数据库
procedure TMenuForm.FootImageClick(Sender: TObject);
var
tempStream:TMemoryStream;
JpgPic:TJpegImage;
S:String;
begin
if FootImage.Picture.Width=0 then
begin
if OpenDialog.Execute then
begin
S:=OpenDialog.FileName;
S:=UpperCase(Copy(S,Length(S)-2,3));
if S='JPG' then
begin
try
JpgPic:=TJpegImage.Create;
tempStream:=TMemoryStream.Create;
tempStream.Clear;
a.Dataset.Edit;
JpgPic.LoadFromFile(OpenDialog.FileName);
FootImage.Picture.bitmap.assign(JpgPic);
JpgPic.SaveToStream(tempStream);
TBlobField(a.Dataset.FieldByName('图象')).LoadFromStream(tempStream) ;
a.Dataset.Post;
finally
JpgPic.Free;
tempStream.Free;
end;
end;
end;
end;
end;

//显示SQL数据库中的JPG图片
procedure TMenuForm.ShowImage;
var
MyJpeg:TJpegImage;
MyStm:TMemoryStream;
begin
a.GetSelRow;

if not a.DataSet.FieldByName('图象').IsNull then
begin
try
MyJpeg:=TJpegImage.Create;
MyStm:=TMemoryStream.Create;
MyStm.Clear;
TBlobField(a.Dataset.FieldByName('图象')).SaveToStream(MyStm);
MyStm.Position:=0;
MyJpeg.LoadFromStream(MyStm);
FootImage.Picture.BitMap.Assign(MyJpeg);
finally
MyJpeg.Free;
MyStm.Free;
end;
end
else
begin
if FootImage.Picture<>Nil then
begin
FootImage.Picture.Assign(Nil);
end;
end;
end;
 
TO宫本宝藏
有问题吗?有很多关于这方面的帖子,你可以查一下。
 
to DELPHI鸟:
是的!
我是第一次,接触图象的问题
我也试着做了几次,但我用Qurey的insert into进行添加时,但添加的图片是一片黑!
后来我听说要用Table,但我用Table比较少,所以不是很清楚!
希望你能说的细一点,不胜感激,我还可以给你加分的!
 
不能用SQL语句insert into来更新图象性或二进制的数据流。。。
其实用Query与Table都是一样的,一般是用字段类型为TBlobField的
LoadFromStream方法来加入图象数据,最后用Post等方法来更新的。。。
 
TQuery使用Insert Into当然可以,存入不需要用到TBlobField

var
MemoryStream:TMemoryStream;
St:TStringStream;
begin
MemoryStream:=TMemoryStream.Create;
MemoryStream.LoadFromFile(AFileName);
St:=TStringStream.Create;
St.CopyFrom(MemoryStream,MemoryStream.Size);
with Query1 do
begin
Close;
SQL.Clear;
SQL.Text:='Insert into table1(Files) values(:File)';
ParamByName('File').AsBlob:=St.DataString;
ExecSQL;
Close;
end;
MemoryStream.Free;
St.Free;

end;
 
怎么老是包错:没有定义AsBlob???
 
用流吧Tblobstream
 
begin
if odlgimg.Execute then
begin
tbimg.append;
tbimg.fieldbyname('id').asstring := '3';
(Tbimg.FieldByName('picture') as TBlobField).loadfromfile(odlgimg.Filename);
tbimg.post;
end;
end;
 
多人接受答案了。
 
后退
顶部