BCB database image save technology

xiaoxiao2021-03-06  38

The following content is from http://community.9cbs.net/expert/topic/3661/3661202.xml?temp=.8916742 Since the collection of the article has time limit, the time is not found, so it will be put this. . (Copy PASTER)

BCB database image save technology plus header file #include 1void __fastcall tform1 :: spesedbutton1click (Tobject * sender) {OpenDialog2-> filename = ""; if (OpenDialog2-> execute ()) {string f = OpenDialog2- > Filename; if (FileExists (f)) {string ext = extractfileext (f) .llowercase (); if (ext == ". BMP" || ext == ". JPG" || ext == ". JPEG" ) {String JPG = CHANGEFILEEXT (EXTRACTFILENAME (F), ". JPG"); TJPEGIMAGE * J = New TjpegImage; if (ext == ") {graphics :: tbitmap * b = new graphics :: tbitmap; b -> LoadFromFile (f); j-> assign (b); j-> compressionQuality = 90; J-> compress (); delete b;} else {tfilestream * f s = 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 Be sure to delete Tadobstream 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-> loadingfromstream (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-> SaveTroupTream (PMEM); delete pMEM; PQUERY-> POST ();

If you use a fixed field in TTable, it is simpler. The following adotable1PIC field is to read the blob data in Access TMemoryStream * s = new tmemorystream (); adotable1pic-> savetostream (s); s); s); s); s); s); s); s); s); s); s) // **** The head position must be zero *** tjpegimage * jp = new tjpegimage (); jp-> loadingfromstream (s); image1-> picture-> bitmap-> assign (jp); delete jp; Delete S; write data to the blob field IF (OpenDialog1-> execute ()) {adotable1-> disableControls (); adotable1-> append (); adotable1pic-> loadingfromfile (OpenDialog1-> filename); adotable1name-> value = OpenDialog1-> filename; adotable1-> post (); adotable1-> enableControls ();

3void __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 = "Save Image Exception";}} 4 In BCB, the following method is stored in the picture, but the code is displayed No need to write, DBIMAGE is automatically displayed.

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;

Data Type To IMAGE Save JPEG to SQL Database: IF (OpenPictureDialog1-> EXECUTE ()) {adoquery1-> Edit (); tblobfield * Field = (TBLOBFIELD *) AdoQuery1-> FieldbyName ("photo"); Field-> loadFromFile (OpenPictureDialog1-> filename); adoquery1-> post ();

/ / The following is to read JPEG from the database and display in the TDBIMAGE control. #Include TSTREAM * STREAM1; TJPEGIMAGE * PJP;

PJP = New TjpegImage ();

Adoquery1-> open (); try {stream1 = adoquery1-> createblobstream (adoQuery1-> fieldByname ("Treenodes"), bmread); // Treenodes is a field that stores JPEG content, its type must be used by image pjp-> loadingfromstreamstream (Stream1); // image2 is a TDBIMAGE component, its datesource, and the fieldname property to be empty image2-> picture-> bitmap-> assign (pjp); delete stream1;} __finally {adoQuery1-> close (); delete PJP }

Below is a better way of others, the following is the following is the program that reads various types of pictures, supporting 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; /} // ----- -------------------------------------------------- ------- // This template uses the image of the binary field (GIF or JPG, etc.) to TPicture, Tbitmap, and more. (! F_Photo-> DataSet-> Active) template bool LoadPhotoFromField (TField * F_Photo, const AnsiString Format, T * Pic) {if return false; if (F_Photo-> IsNull) return false; else {TStream * TmpStream = F_photo-> dataset-> createblobstream (f_photo, bmread); if (Format == ".jpg" || == ".jpeg") Picture_map __ (tjpegimage) else if (Format == ".bmp") Picture_Map__ Graphics :: Tbitmap) // Else If (Format == ")) Else IF (Format ==") Else IF (Format == ") Else IF (Format ==" .WMF "|| Format == ". EMF") Picture_map __ (tmetafile) Else Return false;} Return true;} # undef picture_map __ (tbit) // If you want to support GIF, then you want to install the VCL class that supports GIF.

转载请注明原文地址:https://www.9cbs.com/read-76164.html

New Post(0)