BCB数据库图像保存技术

类别:编程语言 点击:0 评论:0 推荐:

下面的内容来自
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();
  }


 

本文地址:http://com.8s8s.com/it/it24017.htm