BCB database Image Storage Technology

Source: Internet
Author: User

The following content comes from
Http://community.csdn.net/Expert/topic/3661/3661202.xml? Temp =. 8916742.
Since there is a time limit for the favorite articles, it cannot be found after a long time, so it is placed here. (Copy + Paster)

BCB database Image Storage Technology
Add the header file # 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 ");
Tsung image * j = new Tsung image;
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 ("photo image"), bmreadwrite );
BS-> position = 0;
J-> savetostream (BS );
Delete J;
Delete BS;
Dispjpg ();
}
}
}
}

2
Remember to delete the tadoblobstream object before post.
// Read
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;
// Write
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 ();

If you use a fixed field in tTable, it is simpler. The following adotable1pic field is
Read BLOB data in access
Tmemorystream * s = new tmemorystream ();
Adotable1pic-> savetostream (s );
S-> position = 0; // ***** the first position of the stream must be zero ***
Tsung image * JP = new Tsung image ();
JP-> loadfromstream (s );
Image1-> picture-> Bitmap-> assign (JP );
Delete JP;
Delete S;
Blob field for writing data to access
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 ")
{
Tsung image * JPG = new Tsung image ();
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 = "An exception occurred while saving the image ";
}
}

4
In BCB, the following method is used to store images. However, the display code does not need to be written, and dbimage is automatically displayed.

If (openpicturedialog1-> execute ());
{
Ansistring myfilesextension = uppercase (extractfileext (openpicturedialog1-> filename ));
If (myfilesextension = ". jpg ")
{
Tsung image * JPG = new Tsung image ();
Jpg-> loadfromfile (openpicturedialog1-> filename );
Clipboard ()-> assign (JPG );
Dbimage1-> datasource-> dataset-> edit ();
Dbimage1-> pastefromclipboard ();
Dbimage1-> datasource-> dataset-> post ();
Delete JPG;
}

The data type must be image.
Save jpeg to SQL database:
If (openpicturedialog1-> execute ())
{
Adoquery1-> edit ();
Tblobfield * field = (tblobfield *) adoquery1-> fieldbyname ("photo ");
Field-> loadfromfile (openpicturedialog1-> filename );
Adoquery1-> post ();
}

// Read jpeg from the database and display it in the tdbimage control.
# Include <clipbrd. HPP>

Tstream * stream1;
Tsung image * pjp;

Pjp = new tsf-image ();

Adoquery1-> open ();
Try
{
Stream1 = adoquery1-> createblobstream (adoquery1-> fieldbyname ("treenodes"), bmread); // treenodes is the field that stores JPEG content, and its type must be Image
Pjp-> loadfromstream (stream1 );
// Image2 is the tdbimage component. Its datesource and fieldname attributes must be empty.
Image2-> picture-> Bitmap-> assign (pjp );
Delete stream1;
}
_ Finally
{
Adoquery1-> close ();
Delete pjp;
}

The following is a better way for others to store various images
The following is a program for generating various types of video clips. It supports ado, BDE, or tclientdataset.
# Define picture_map _ (Tbit) {Tbit * Pg = new Tbit ();/
Try {PG-> loadfromstream (tmpstream );/
PIC-> assign (PG );/
}/
Catch (...)/
{Delete PG ;/
Return false ;/
}/
Delete PG ;/
}
//----------------------------------------------------------------
// The template uses the Assign Method to convert the image (GIF or JPG) in the binary field to tpicture, tbitmap, and so on.
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 _ (t1_image)
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)
// If You Want To support GIF, you need to install the VCL type that supports GIF.

 

Supports multiple formats
Deposit:
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 ();
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.