Programming C++Builder Using TBlobField and TBlobStream in C++Builder
Notice: Undefined variable: relatedArticlesCount in /home/wwwroot/codeback.net/plugins/content/tags.php on line 149

First, we assume you have a table with a blobfield named "image", then you could use the following codes to read or write the contents from or to the "image" field.

void __fastcall ReadBlobField()
{
    TBlobField* pField = (TBlobField*)pQuery->FieldByName("Image");  
    TBlobStream* pmem = new TBlobStream(pField, bmRead);  
    pmem->Seek(0, soFromBeginning);  
    Graphics::TBitmap* pBitmap = new Graphics::TBitmap();  
    pBitmap->LoadFromStream(pmem);  
    Image1->Picture->Assign(pBitmap);  
    delete pBitmap;  
    delete pmem;
}

void _fastcall WriteBlobField()
{
    pQuery->Edit();  //or pQuery->Insert() or pQuery->Append() ;
    TBlobField* pField = (TBlobField*)pQuery->FieldByName("Image");
    TBlobStream* pmem = new TBlobStream(pField, bmWrite);  
    pmem->Seek(0, soFromBeginning);  
    Graphics::TBitmap* pBitmap = new Graphics::TBitmap();  
    pBitmap->Assign(Image1->Picture->Graphic->Bitmap);  
    pBitmap->SaveToStream(pmem);  
    delete pBitmap;  
    delete pmem;  
    pQuery->Post();   
}

When you are using an ado connection, you should use the TADOBlobStream to access the database.

void __fastcall adoReadBlobField()

    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;   
}

void __fastcall adoWriteBlobField()
{
    pQuery->Edit();   //or pQuery->Insert() or pQuery->Append() ;
    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->Bitmap);  
    pBitmap->SaveToStream(pmem);  
    delete pBitmap;  
    delete pmem;  
    pQuery->Post();
}

When you are using the TClientDataSet to connect to the database, you should use TClientBlobStream to access the database field.

void __fastcall clientReadBlobField()

    TBlobField* pField = (TBlobField*)pQuery->FieldByName("Image");   
    TClientBlobStream* pmem = new TClientBlobStream(pField, bmRead);  
    pmem->Seek(0, soFromBeginning);  
    Graphics::TBitmap* pBitmap = new Graphics::TBitmap();  
    pBitmap->LoadFromStream(pmem);  
    Image1->Picture->Assign(pBitmap);  
    delete pBitmap;  
    delete pmem;   
}

void __fastcall clientWriteBlobField()
{
    pQuery->Edit();   //or pQuery->Insert() or pQuery->Append() ;
    TBlobField* pField = (TBlobField*)pQuery->FieldByName("Image");   
    TClientBlobStream* pmem = new TClientBlobStream(pField, bmWrite);  
    pmem->Seek(0, soFromBeginning);  
    Graphics::TBitmap* pBitmap = new Graphics::TBitmap();  
    pBitmap->Assign(Image1->Picture->Graphic->Bitmap);  
    pBitmap->SaveToStream(pmem);  
    delete pBitmap;  
    delete pmem;  
    pQuery->Post();
}

Have a good luck! Well, you maybe have interesting in these articles:
How to use an ini file in C++Builder .
An example on how to use GetFormImage() and Assign() functions in C++Builder .
An example on using scanline to access bitmap pixels in C-Builder .
How to convert an icon to a bitmap in C++Builder .
Use the TWebBrowser as an html editor in delphi .
Draw a flowing line like photoshop selection tools .


Add comment


Security code
Refresh

Programming - C++Builder