分享
 
 
 

BCB数据库图像保存技术

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

下面的内容来自http://community.csdn.net/Expert/topic/3661/3661202.xml?temp=.8916742

由于收藏的文章有时间限制,时间一长就找不到了,所以就放到这了。(copy+paster

)

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;

}

数据类型一定要是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();

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有