sql数据库有个image类型的数据列。如何在CB中实现往其中添加图片并预览?

zeng822 2003-05-23 12:35:20
sql数据库有个image类型的数据列。如何在CB中实现往其中添加图片并预览?
...全文
380 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzhong2 2003-09-13
  • 打赏
  • 举报
回复
数据类型一定要是image
存jpeg到SQL数据库:
if(OpenPictureDialog1->Execute())
{
ADOQuery1->Edit();
TBlobField *Field = (TBlobField*)ADOQuery1->FieldByName("photo");
Field->LoadFromFile(OpenPictureDialog1->FileName);
ADOQuery1->Post();
}

//以下是从数据库读jpeg,并显示在TDBImage控件中
#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 >
bool 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類。



支持多種格式
存入:
if(OpenPictureDialog1->Execute())
{DataSet->Edit();
TBlobField *Field = (TBlobField*)DataSet->FieldByName("photo");
Field->LoadFromFile(OpenPictureDialog1->FileName);
DataSet->FieldByName("photoFormat")->AsString =
ExtractFileExt(OpenPictureDialog1->FileName).UpperCase();
DataSet->Post();
}
binbin 2003-09-13
  • 打赏
  • 举报
回复
ADOQuery1->SQL->Text = "insert into table values('abc', :BlobField)";
ADOQuery1->Parameters->Items[0]->LoadFromFile("文件名", ftBlob);
ADOQuery1->ExecSQL();

图片预览用TImage控件就是了.
IT-司马青衫 2003-09-12
  • 打赏
  • 举报
回复
BCB数据库图像保存技术
加上头文件#include<jpeg.hpp>
1
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
OpenDialog2->FileName="";
if (OpenDialog2->Execute())
{
String f=OpenDialog2->FileName;
if (FileExists(f))
{
String ext=ExtractFileExt(f).LowerCase();
if (ext==".bmp" || ext==".jpg"|| ext==".jpeg")
{
String jpg=ChangeFileExt(ExtractFileName(f),".jpg");
TJPEGImage *j=new TJPEGImage;
if (ext==".bmp")
{
Graphics::TBitmap *b=new Graphics::TBitmap;
b->LoadFromFile(f);
j->Assign(b);
j->CompressionQuality=90;
j->Compress();
delete b;
}
else
{
TFileStream *fs=new TFileStream(f,fmOpenRead);
j->LoadFromStream(fs);
delete fs;
}
DataModule1->ADODataSet1->Edit();
DBEdit2->Text=jpg;
TStream *bs=DataModule1->ADODataSet1->CreateBlobStream(
DataModule1->ADODataSet1->FieldByName("照片图像"),bmReadWrite);
bs->Position=0;
j->SaveToStream(bs);
delete j;
delete bs;
DispJpg();
}
}
}
}


2
记住一定要在post前delete TADOBlobStream对象。
//读
TBlobField * pField=(TBlobField *)pQuery->FieldByName("Image");
TADOBlobStream* pmem=new TADOBlobStream (pField,bmRead);
pmem->Seek(0,soFromBeginning);
Graphics::TBitmap * pBitmap=new Graphics::TBitmap();
pBitmap->LoadFromStream(pmem);
Image1->Picture->Assign(pBitmap);
delete pBitmap;
delete pmem;
//写
pQuery->Edit();
TBlobField * pField=(TBlobField *)pQuery->FieldByName("Image");
TADOBlobStream * pmem=new TADOBlobStream (pField,bmWrite);
pmem->Seek(0,soFromBeginning);
Graphics::TBitmap * pBitmap=new Graphics::TBitmap();
pBitmap->Assign(Image1->Picture->Graphic);
pBitmap->SaveToStream(pmem);
delete pBitmap;
delete pmem;
pQuery->Post();

如果你在ttable中用了固定字段,就更简单了,以下的ADOTable1pic字段就是
读取access中的blob数据
TMemoryStream *s = new TMemoryStream();
ADOTable1pic->SaveToStream(s);
s->Position=0; //****流首位置必须为零***
TJPEGImage *jp = new TJPEGImage();
jp->LoadFromStream(s);
Image1->Picture->Bitmap->Assign(jp);
delete jp;
delete s;
写数据到access的blob字段
if (OpenDialog1->Execute())
{ADOTable1->DisableControls();
ADOTable1->Append();
ADOTable1pic->LoadFromFile(OpenDialog1->FileName);
ADOTable1name->Value=OpenDialog1->FileName;
ADOTable1->Post();
ADOTable1->EnableControls();
}


3
void __fastcall TForm2::DBImage2Click(TObject *Sender)
{
try
{
if(OpenPictureDialog1->Execute());
{
AnsiString MyFilesExtension = UpperCase(ExtractFileExt(OpenPictureDialog1->FileName));
if(MyFilesExtension==".JPG")
{
TJPEGImage *jpg=new TJPEGImage();
jpg->LoadFromFile(OpenPictureDialog1->FileName);
Clipboard()->Assign(jpg);
DBImage1->DataSource->DataSet->Edit();
DBImage1->PasteFromClipboard();
DBImage1->DataSource->DataSet->Post();
delete jpg;
}
if(MyFilesExtension==".BMP")
{
EditDataSource1->DataSet->Edit();
DataModule1->FindADODataSetDSDesigner7->LoadFromFile(OpenPictureDialog1->FileName);
}
}
}
catch(...)
{
StatusBar1->Panels->Items[1]->Text="保存图像发生异常";
}
}

4
在BCB中采用下术方法存入图片,不过显示代码不用写了,DBImage自动显示.

if(OpenPictureDialog1->Execute());
{
AnsiString MyFilesExtension = UpperCase(ExtractFileExt(OpenPictureDialog1->FileName));
if(MyFilesExtension==".JPG")
{
TJPEGImage *jpg=new TJPEGImage();
jpg->LoadFromFile(OpenPictureDialog1->FileName);
Clipboard()->Assign(jpg);
DBImage1->DataSource->DataSet->Edit();
DBImage1->PasteFromClipboard();
DBImage1->DataSource->DataSet->Post();
delete jpg;
}
stevenjscn 2003-05-27
  • 打赏
  • 举报
回复
在imgTest->Picture->LoadFormFile("Image的地址”〕;后加上
tblTest->Post();
可以为该程序加一OPENDIALOG以便能方便地找到IMAGE的地址
stevenjscn 2003-05-27
  • 打赏
  • 举报
回复
可以为FORM添加一TADOTable(tblTest),并设置其Connection和TABLENAME,添加一DATASOURCE(dsTest)添加一DBImage(imgTest)并绑定其DATAFIELD,添加一BUTTON为btnADD.在btnADDClick 中写以下代码:

tblTest->Open();
tblTest->Edit();
imgTest->Picture->LoadFromFile("Image的地址");

Image的调入会自动加入。
wjh1014 2003-05-23
  • 打赏
  • 举报
回复
// Example 6: Loading a JPEG from a database

#include <jpeg.hpp>

void __fastcall TForm1::Button3Click(TObject *Sender)
{
// query the image table
Query->SQL->Text = "select * from image_test\n"
"where string_field = 'space.jpg'";
Query->Active = true;

// Create a memory stream and copy the image from the blob
// field into the stream. Note that Queryimage_field is a
// a persistent TBlobField created from the fields editor
std::auto_ptr<TMemoryStream> stream (new TMemoryStream);
Queryimage_field->SaveToStream(stream.get());

// create a jpeg image object, and tell it to load the jpeg
// from the memory stream
stream->Position = 0;
std::auto_ptr<TJPEGImage> jpeg(new TJPEGImage);
jpeg->LoadFromStream(stream.get());

// Now load the jpeg into the image. This step
// decompresses the JPEG to a bitmap
Image1->Picture->Bitmap->Assign(jpeg.get());

Query->Active = false;
}
So how do you save a JPEG image to a database? Well, that depends on whether you just need to save an existing JPEG file on your file system, or whether you need to compress a bitmap into a JPEG before saving it. If you only need to save a JPEG file from the file system, then use the code from Examples 1-3. You can write a JPEG file directly to the database, just like you would for a BMP file.

If you need to compress an image before saving it to the database, then you will once again need to employ the TJPEGImage class. The next two code examples demonstrate how you can do this. The first example assumes that the source image is a .BMP file on your file system. The second example saves an image that has already been loaded into a TImage component. Note that for these two examples, I assume that you are use BDE query components. If you are using ADO controls or table controls, then substitute code as neede. See Examples 1-3 for more details.

//-------------------------------------------------------------
// Example 7: Saving a BMP file as a JPEG image to a database.

#include <jpeg.hpp>

void __fastcall TForm1::Button2Click(TObject *Sender)
{
// load a bitmap into a TBitmap object
std::auto_ptr<TBitmap> bitmap(new TBitmap;
bitmap->->LoadFromFile("androm.bmp");

// create a jpeg object and assign the bitmap
// to the jpeg. This performs the JPEG compression step.
std::auto_ptr<TJPEGImage> jpeg(new TJPEGImage);
jpeg->Assign(bitmap.get());

// Now create a stream object and write the compressed JPEG
// image to the stream. Once we have the JPEG data in the
// stream, we can get it into the database.
std::auto_ptr<TMemoryStream> stream (new TMemoryStream);
jpeg->SaveToStream(stream.get());
stream->Position = 0;

// Save the stream to the database. This is exactly like Example 1
Query1->Params->ParamByName("string_field")->AsString = "androm.bmp";
Query1->Params->ParamByName("image_field") ->SetBlobData(stream->Memory,
stream->Size);

// execute the query to perform the insert
Query1->ExecSQL();
}

//-------------------------------------------------------------
// Example 8: Writing a TImage as a JPEG to a database.

#include <jpeg.hpp>

void __fastcall TForm1::Button2Click(TObject *Sender)
{
// create a jpeg object and assign the bitmap of the
// TImage to the jpeg object.
std::auto_ptr<TJPEGImage> jpeg(new TJPEGImage);
jpeg->Assign(Image1->Picture->Bitmap);

// Now create a stream object and write the compressed JPEG
// image to the stream. Once we have the JPEG data in the
// stream, we can get it into the database.
std::auto_ptr<TMemoryStream> stream (new TMemoryStream);
jpeg->SaveToStream(stream.get());
stream->Position = 0;

// Save the stream to the database. This is exactly like Example 1
Query1->Params->ParamByName("string_field")->AsString = "androm.bmp";
Query1->Params->ParamByName("image_field") ->SetBlobData(stream->Memory,
stream->Size);

// execute the query to perform the insert
Query1->ExecSQL();
}
Notes
Note 1: If you are reading bitmaps, icons, or Windows meta files from a database, you can save a few lines of code by using the TDBImage data-aware control. You can connect the TDBImage to a blob field via a TDataSource component. The TDBImage control is so easy to use, that I didn't bother describing it. The FISHFACT example program that ships with BCB demonstrates how to use this control. Note that you can only use TDBImage to view images, and you can only view native Windows images (ie not JPEGs).

Note 2: The code examples for displaying images did not account for result sets that contain more than one record. This is one of the nice features of TDBImage. When you use a TDBImage, it automatically updates the image on the screen as you scroll through the dataset. If you don't use TDBImage, then you will need to add some supporting code if you're result sets return multiple rows. You will need to update the image on the screen each time you scroll from one record to another. The dataset components provide events that can help you accomplish this (namely AfterOpen, AfterClose, and AfterScroll).

Note 3: The VCL provides a class called TBlobStream that is worth mentioning in this FAQ. TBlobStream is similar to TFileStream. TFileStream encapsulates a file in a stream object. When you read from or write to a TFileStream, you are actually reading from or writing to the underlying file. TBlobStream does the same thing, except that it encupulates reading and writing blob fields.

When you create a TBlobStream, you pass it two constructor arguments. The first is a TBlobField. This is the field that you want to read or write. The second argument is an enum that tells the blob stream whether it will be reading from or writing to the underlying blob field. After you construct a blob stream, any operation that you perform on it will affect the internal blob field.

We could have used TBlobStream in our examples. The code example below demonstrates how we could have written Example 4 using TBlobStream

//-------------------------------------------------------------
// Example 9: Loading a bitmap using a blob stream
void __fastcall TForm1::Button3Click(TObject *Sender)
{
// query the image table
Query->Active = false;
Query->SQL->Text = "select * from image_test\n"
"where string_field = 'androm.bmp'";
Query->Active = true;

// create a blob stream on the field
std::auto_ptr<TBlobStream> stream
(new TBlobStream(Query->FieldByName("image_field"),bmRead));

// Tell the picture control to load from the blob stream
Image1->Picture->Bitmap->LoadFromStream(stream.get());
Query->Active = false;
}
wjh1014 2003-05-23
  • 打赏
  • 举报
回复
//-------------------------------------------------------------
// Example 4: Loading a bitmap from a database with a
// query component (BDE or ADO)
void __fastcall TForm1::Button3Click(TObject *Sender)
{
// query the image table
Query->Active = false;
Query->SQL->Text = "select * from image_test\n"
"where string_field = 'androm.bmp'";
Query->Active = true;

// Create a memory stream and copy the image from the blob
// field into the stream. Note that Queryimage_field is a
// a persistent TBlobField created from the fields editor
std::auto_ptr<TMemoryStream> stream (new TMemoryStream);
Queryimage_field->SaveToStream(stream.get());

// save the stream to a BMP file
stream->Position = 0;
stream->SaveToFile("androm.bmp");

// load the stream into the a TImage
stream->Position = 0;
Image1->Picture->Bitmap->LoadFromStream(stream.get());

Query->Active = false;
}


//-------------------------------------------------------------
// Example 5: Loading a bitmap from a database with a
// table component (BDE or ADO)
void __fastcall TForm1::Button3Click(TObject *Sender)
{
// query the image table
Table->Open();

// Create a memory stream and copy the image from the blob
// field into the stream. Note that Tableimage_field is a
// a persistent TBlobField created from the fields editor
std::auto_ptr<TMemoryStream> stream (new TMemoryStream);
Tableimage_field->SaveToStream(stream.get());

// save the stream to a BMP file
stream->Position = 0;
stream->SaveToFile("androm.bmp");

// load the stream into the a TImage
stream->Position = 0;
Image1->Picture->Bitmap->LoadFromStream(stream.get());

Table->Close();
}
Note:
--------------------------------------------------------------------------------

The examples above utilize the SaveToStream method of TBlobField. This is a new member function introduced in TBlobField that is not present in the base class TField. Since TField does not contain a SaveToStream member function, you cannot write code like this:

Table->FieldByName("image_field")->SaveToStream(stream);
If you do, you will get a compiler error stating that SaveToStream is not a member of TField. The error is caused because FieldByName returns a TField *. Although the field in this case is a TBlobField instance, the compiler must use the static return type of the function to verify the syntax of the statement. There is no way the compiler can know that FieldByName will return a TBlobField in this case. All the compiler knows is that the return type is a TField of some kind, and TField does not have a SaveToStream method.

There are a couple of ways to get around this problem. The first is to use persistent fields. When the Fields Editor creates persistent fields, it uses a field class that corresponds to the field type from the dataset. String fields are declared as TStringField objects, and blob fields are declared as TBlobField objects. Since blob fields are declared as TBlobFields, you can use the persistent field to call the SaveToStream method.

Another way to deal with this issue is to downcast the result of FieldByName to a TBlobField. If you choose this method, I encourage you to use dynamic_cast to perform the cast.

TBlobField field =
dynamic_cast<TBlobField *>(query->FieldByName("image_field"));
if(field)
field->SaveToStream(stream);

--------------------------------------------------------------------------------
wjh1014 2003-05-23
  • 打赏
  • 举报
回复
// Example 1: Inserting a bitmap using a BDE query component
void __fastcall TForm1::Button2Click(TObject *Sender)
{
// load a bitmap into a memory stream object
std::auto_ptr<TMemoryStream> stream (new TMemoryStream);
stream->LoadFromFile("androm.bmp");
stream->Position = 0;

// Assign the parameters of the query. The SQL property of the query
// is set to:
//
// insert image_test
// (
// string_field,
// image_field
// )
// values
// (
// :string_field,
// :image_field
// )
//
// Note that the image_field param should have its DataType set to ftBlob
//
Query1->Params->ParamByName("string_field")->AsString = "androm.bmp";
Query1->Params->ParamByName("image_field") ->SetBlobData(stream->Memory,
stream->Size);

// execute the query to perform the insert
Query1->ExecSQL();
}

//-------------------------------------------------------------
// Example 2: Inserting a bitmap using a ADO query component
void __fastcall TForm1::Button2Click(TObject *Sender)
{
// load a bitmap into a memory stream object
std::auto_ptr<TMemoryStream> stream (new TMemoryStream);
stream->LoadFromFile("androm.bmp");
stream->Position = 0;

// Assign the parameters of the query. The SQL property of the query
// is the same SQL from the BDE example. The image_field param should
// have its DataType set to ftVarBytes, ftBlob or ftGraphic
ADOQuery->Parameters->ParamByName("string_field")->Value= "androm.bmp";
ADOQuery->Parameters->ParamByName("image_field")->LoadFromStream(stream.get(),
ftGraphic);

// execute the query to perform the insert
ADOQuery->ExecSQL();
}

//-------------------------------------------------------------
// Example 3: Inserting a bitmap using a table component (BDE or ADO)
void __fastcall TForm1::Button2Click(TObject *Sender)
{
// load a bitmap into a memory stream object
std::auto_ptr<TMemoryStream> stream (new TMemoryStream);
stream->LoadFromFile("androm.bmp");
stream->Position = 0;

// Add a record to the table, fill in the fields, and then post the record.
// Note that Table1string_field and Table1image_field are persistent TField
// objects. The image field must be a TBlobField, or a TBlobField
// descendent or this code won't compile. TBlobField is the only field type
// that provides a LoadFromStream method. Also note that if you use
// FieldByName to reference the field, you will need to cast the result to
// TBlobField before you call LoadFromStream because FieldByName returns a
// TField pointer, not a TBlobField pointer.
//
the image_field property should a TBlobField.
Table1->Append();
Table1string_field->AsString = "androm.bmp";
Table1image_field ->LoadFromStream(stream.get(),
ftGraphic);
Table1->Post();
}
zeng822 2003-05-23
  • 打赏
  • 举报
回复
晕,看不懂。我菜鸟一个
能不能直接点,知道一个图片的地址,然后写入表的image列?

1,178

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 数据库及相关技术
社区管理员
  • 数据库及相关技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧