如何存储图片信息到数据库中呢!
太多的时候我们要保存图片,电影,MP3,究竟该怎么处理这些信息呢呢!太多的情况下,我们认为应该存储图片保存的路径信息,但是我们考虑数据库要备份的问题的时候,以及数据库要实现转移的时候,可能给我们造成诸多不便!我觉得在某些情况下应该存储图片信息也许是个好主意;就象我们在设计数据库的时候,如果数据量很小的情况下,可以考虑用数据冗余的方法来减少视图的数量;设计数据库是个很灵活的过程,不是说那本书就可以完成任务的,要考虑到程序员和用户的使用方便取一个折中。
经常在CSDN上游逛,经常会遇到太多的相同的问题出现,我觉得应该总结一下,对后来者也有一点点帮助,以谢CSDN前辈们对我的帮助;这里讨论的是如何在BCB中存取图片信息。
第一个是用TADOQuery实现的,就是用SQL语句加上参数实现的。
参考http://expert.csdn.net/Expert/topic/1805/1805073.xml?temp=.556637这个写的,如果你不愿意找的话,原码如下:
if(OpenPictureDialog1->Execute())
{
String strSql = "INSERT INTO table1 (content)";
strSql = strSql + " VALUES(:picture)";
try{
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add(strSql);
ADOQuery1->Parameters->ParamByName("picture")->LoadFromFile(OpenPictureDialog1->FileName, ftBlob);
ADOQuery1->ExecSQL();
} catch(...) { return; }
}
还有一个是用Delphi写的,原码如下(没有调试):
//(1)如果使用的是TTable,则要将其ReadOnly属性先置为false,然后调用Edit函数;
//(2)如果使用的是TQuery,则要将其RequestLive属性先置为true,然后调用Edit函数;
var
ms : TADOBlobStream;
begin
query1.Edit;//只能是Edit;不能是Append 和 Insert;
ms := TADOBlobStream.Create(TBlobField(query1.FieldByName('BMP')), bmRead);
ms.Seek(0, soFromBeginning);
ms.SaveToFile('d:\l.bmp');
query1.Post;
ms.Free;
end;
同样那位大哥的BCB代码如下:
//(1)如果使用的是TTable,则要将其ReadOnly属性先置为false,然后调用Edit函数;
//(2)如果使用的是TQuery,则要将其RequestLive属性先置为true,然后调用Edit函数;
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
Table1->Edit();;//只能是Edit();不能是Append() 和 Insert();
TBlobField * pField=(TBlobField *)Table1->FieldByName("图字段名");
TBlobStream * pmem=new TBlobStream(pField,bmWrite);
pmem->Seek(0,soFromBeginning);
Graphics::TBitmap * pBitmap=new Graphics::TBitmap();
pBitmap->LoadFromFile("d:\\image\\a.bmp");
pBitmap->SaveToStream(pmem);
Table1->Post();
delete pBitmap;
delete pmem;
}
同样类似的BCB代码如下(上边的虽然没有调试,但和我写的道理还是一样的,就是用数据库中的一个字段和一个流对象,然后对流进行操作而已):
TMemoryStream *stream = new TMemoryStream();
Image1->Picture->Bitmap->SaveToStream(stream);
ADOTable1->Insert();
ADOTable1->FieldByName("title")->AsString=Edit1->Text;
((TGraphicField *)(ADOTable1->FieldByName("content")))->LoadFromStream(stream);
ADOTable1->Post();
delete stream;
读取的方法类似,当为了方便大家,还是写出来,如下:
include <clipbrd.hpp>
TStream *Stream1;
TJPEGImage *Pjp;
Pjp=new TJPEGImage();
ADOQuery1->Open();
try
{
Stream1=ADOQuery1->CreateBlobStream(ADOQuery1->FieldByName("treenodes"), bmRead);//treenodes是存放jpeg内容的字段,它的类型一定要用image
Pjp->LoadFromStream(Stream1);//Image2是TDBImage组件,它的DateSource,和FieldName属性要空着
Image2->Picture->Bitmap->Assign(Pjp);
delete Stream1;
}
__finally
{
ADOQuery1->Close();
delete Pjp;
}
还有更绝的,用个宏,如下:
以下是讀出各種類型的圖片的程序,支持ADO,BDE或TClientDataSet
#define PICTURE_MAP__(TBit) {TBit *PG = new TBit(); \
try {PG->LoadFromStream(TmpStream);\
Pic->Assign(PG); \
}\
catch(...)\
{delete PG ;\
return false;\
}\
delete PG;\
}
//----------------------------------------------------------------
//該模板將二進制字段中的圖像(GIF或JPG等等)使用Assign方法轉為TPicture,TBitmap等等。
template <class T >
ool LoadPhotoFromField(TField *F_Photo,const AnsiString Format,T *Pic)
if(!F_Photo->DataSet->Active) return false ;
if(F_Photo->IsNull) return false ;
else
{TStream *TmpStream = F_Photo->DataSet->CreateBlobStream(F_Photo,bmRead);
if(Format == ".JPG" || Format == ".JPEG")PICTURE_MAP__(TJPEGImage )
else if(Format == ".BMP") PICTURE_MAP__(Graphics::TBitmap)
// else if(Format == ".GIF") PICTURE_MAP__(TGIFImage )
else if(Format == ".ICO") PICTURE_MAP__(TIcon)
else if(Format == ".WMF" || Format ==".EMF") PICTURE_MAP__(TMetafile)
else return false ;
return true;
undef PICTURE_MAP__(TBit)
/如果要支持GIF,那你要安裝支持GIF的VCL類。
持多種格式
入:
f(OpenPictureDialog1->Execute())
{DataSet->Edit();
TBlobField *Field = (TBlobField*)DataSet->FieldByName("photo");
Field->LoadFromFile(OpenPictureDialog1->FileName);
DataSet->FieldByName("photoFormat")->AsString =
ExtractFileExt(OpenPictureDialog1->FileName).UpperCase();
ataSet->Post();
在http://expert.csdn.net/Expert/topic/1348/1348495.xml?temp=.5143854中,JSP老大的方法,其实也是一样的!:
if(!OpenPictureDialog1->Execute())
return;
ADOTable1->Insert();
ADOTable1->FieldByName("title")->AsString=Edit1->Text;
TADOBlobStream* Stream = (TADOBlobStream*)ADOTable1->CreateBlobStream(ADOTable1->FieldByName("content"),bmWrite);
Stream->LoadFromFile(OpenPictureDialog1->FileName);
delete Stream;
ADOTable1->Post();
在http://expert.csdn.net/Expert/topic/1326/1326759.xml?temp=.3533899中,打工仔大哥的方法:
读
TBlobField * pField=(TBlobField *)dm_police->y_find->FieldByName("image_photo");
TClientBlobStream * pmem=new TClientBlobStream(pField,bmRead);
pmem->Seek(0,soFromBeginning);
//Graphics::TBitmap * pBitmap=new Graphics::TBitmap();
TJPEGImage *pBitmap = new TJPEGImage();
pBitmap->LoadFromStream(pmem);
Image2->Picture->Assign(pBitmap);
delete pmem;
delete pBitmap;
写
dm_police->y_stress_man_photo->Open();
dm_police->y_stress_man_photo->Append();
dm_police->y_stress_man_photo->FieldByName("c_card")->Value=Edit3->Text;
TBlobField * pField=(TBlobField *)dm_police->y_stress_man_photo->FieldByName("image_photo");
TClientBlobStream * pmem=new TClientBlobStream(pField,bmWrite);
pmem->Seek(0,soFromBeginning);
//Graphics::TBitmap * pBitmap=new Graphics::TBitmap();
TJPEGImage *pBitmap = new TJPEGImage();
pBitmap->Assign(Image2->Picture->Graphic);
pBitmap->SaveToStream(pmem);
delete pmem;
delete pBitmap;
dm_police->y_stress_man_photo->Post();
dm_police->y_stress_man_photo->ApplyUpdates(-1);
在http://expert.csdn.net/Expert/topic/1383/1383453.xml?temp=.9190485中有:
Table1->Close();
Table1->Open();
Table1->Append();
Table1->FieldByName("ID")->AsString=Edit1->Text.ToInt();
Edit1->Text = StrToInt(Edit1->Text.ToInt()+1);
Table1->FieldByName("Name")->AsString=OpenDialog1->FileName;
Image1->Picture->LoadFromFile(OpenDialog1->FileName) ;
TBlobField * pField=(TBlobField *)Table1->FieldByName("Entity");
TStream *Stream2;
TMemoryStream *tt=new TMemoryStream();
Graphics::TBitmap * pBitmap=new Graphics::TBitmap();
pBitmap->Assign(Image1->Picture->Graphic);
pBitmap->SaveToStream(tt);
pField->LoadFromStream(tt);
Table1->Post();
说了这么多,你可能发现了,上边的代码都有一个共性,那就是用流来操作,好好,不知道你有没有发现呢!
TADOBlobStream:
Use c to access or modify the value of a BLOB or memo field in an ADO dataset. BLOB fields are represented by TBlobField objects and descendants of TBlobField such as TGraphicField and TMemoField.
TADOBlobStream allows objects that have no specialized knowledge of how data is stored in a BLOB field (raw binary data) to read or write such data by employing the uniform stream interface.
To use an ADO BLOB stream, create an instance of TADOBlobStream, use the methods of the stream to read or write the data, and then free the BLOB stream. Do not use the same instance of TADOBlobStream to access data from more than one record. Instead, create a new TADOBlobStream object every time you need to read or write BLOB data on a new record.
Use the Read method to copy the data from BLOB field object to a null-terminated string (or comparable) buffer. Use the Write method to copy from buffer to BLOB field object. When reading data from the BLOB field object, use the Size property to determine the volume of the field’s contents and to allocate the correct amount of space in memory for the receiving buffer.
TMemoryStream:
Use TMemoryStream to store data in a dynamic memory buffer that is enhanced with file-like access capabilities. TMemoryStream provides the general I/O capabilities of a stream object while introducing methods and properties to manage a dynamic memory buffer.
Memory streams are useful as intermediary objects that can hold information as well as read it from or write it to another storage medium. They provide a useful format for comparing the contents of streams, or for manipulating data that is stored in a less accessible medium. TJPEGImage:
Use TJPEGImage to read and write jpeg compressed image data. TJPEGImage handles the digital compression and decompression of still images for use in computer systems. It uses the data from an instance of TJPEGData, which contains the actual jpeg data source and is never modified. Each jpeg image object may share its TJPEGData object with other instances of a jpeg image by creating copies using the Assign method. The jpeg data source handles reference counting for the jpeg image objects that are linked to it.
TJPEGImage has an internal bitmap that represents the jpeg image. This internal image and the original source of the jpeg image are read only. TJPEGImage has properties that determine how each instance will handle color conversion, compression, decompression, performance, and so on.
The following are characteristics of this object. A TJPEGImage object:
Has no canvas (so it cannot draw onto a canvas). However, TJPEGImage implements the protected Draw method introduced in TGraphic, so it can draw itself on the canvas of another object.
Provides no access to the internal bitmap image that it creates for the JPEG image.
Performs reference counting and handle sharing by means of the TJPEGData object. Multiple instances can refer to the same TJPEGData image. TJPEGData is the actual owner of the file handle to the jpeg data source.
TBlobField:
TBlobField encapsulates the fundamental behavior common to binary large object (BLOB) fields. BLOB fields are database fields that contain raw binary data of arbitrary length. BLOB fields can represent different arbitrarily large data types. These data types are distinguished in the header of the binary data.
In addition to the field types supported directly, TBlobField is the direct ancestor of two BLOB field components: TMemoField (ftMemo) and TGraphicField (ftGraphic). These descendants represent BLOB fields that have headers peculiar to memos or graphic data, respectively.
TBlobField introduces new methods to support streaming data to and from the BLOB field and to support copying raw binary data between the BLOB field and a binary file. You can also use the stream returned by a dataset’s CreateBlobStream method to read or write the data managed by a BLOB field.
If you use the Fields editor at design time to create a persistent field component for the BLOB field, you can access it by name at runtime. When using dynamic field components, you can access the TBlobField instance using the dataset’s Fields property or FieldByName method.
TStream:
TStream is the base class type for stream objects that can read from or write to various kinds of storage media, such as disk files, dynamic memory, and so on.
Use specialized stream objects to read from, write to, or copy information stored in a particular medium. Each descendant of TStream implements methods for transferring information to and from a particular storage medium, such as a disk file, dynamic memory, and so on. In addition to methods for reading, writing, and copying bytes to and from the stream, stream objects permit applications to seek to an arbitrary position in the stream. Properties of TStream provide information about the stream, such as its size and the current position in the stream.
TStream also introduces methods that work in conjunction with components and filers for loading and saving components in simple and inherited forms. These methods are called automatically by global routines that initiate component streaming. They can also be called directly to initiate the streaming process. Note, however, that component streaming always involves two additional objects:
A component object that is passed as a parameter to the stream’s methods.
A filer object that is automatically created by the stream, and associated with the stream.
TStream should not be instantiated; it relies on pure virtual methods that must be overridden in descendant classes. Descendant stream objects, such as memory and file streams used for component streaming, are created automatically by the global functions ReadComponentRes and WriteComponentRes. For streaming other kinds of information, choose a descendant class according to the specific data and storage needs.
本文地址:http://com.8s8s.com/it/it5273.htm