如何在DBImage中加载JPG图片?
如何用OpenPictrueDailog在DBImage中打开JPG图片并保存到数据库?
[解决办法]
ADO 组件,简单的写了一下,其实这类问题已经在论坛被问了很多次,搜索一下已经
结了的帖子就知道
// 二进制写表
// ---------------------------------------
AnsiString filename = " ";
if ( OpenDialog1-> Execute() )
{
filename = OpenDialog1-> FileName;
if ( FileExists ( filename ) == false)
return;
}
else
{
return;
}
TMemoryStream* pMS = new TMemoryStream;
pMS-> LoadFromFile(FileName); // 文件读入内存流
pMS-> Seek(0,soFromBeginning);
ADOQuery1-> SQL-> Clear();
ADOQuery1-> SQL-> Add( "insert into imagetab (title, image) values (:title, :image); ");
ADOQuery1-> Parameters-> ParamByName( "title ")-> Value = Edit1-> Text;
ADOQuery1-> Parameters-> ParamByName( "image ")-> LoadFromStream(pMS, ftBlob);
ADOQuery1-> ExecSQL();
// ---------------------------------------
// 二进制读取,相关的操作你自己写了
TMemoryStream* pMS = new TMemoryStream;
TBlobField* pField = (TBlobField*) ADOQuery2-> FieldByName( "image ");
pMS-> Seek(0,soFromBeginning);
pMS-> Size = 0;
pField-> SaveToStream(pMS);
pMS-> SaveToFile( "newfile ");
ADOQuery2-> Close();
delete pMS;
pMS = NULL;
[解决办法]
#include <Jpeg.hpp>
将DBImage1的属性AutoDisplay设为false,在DBImage1对应的Table或者Query控件的事件AfterScroll里写代码:
void __fastcall TForm1::Table1AfterScroll(TDataSet *DataSet)
{
TMemoryStream *ms = DBImage1-> DataSource-> DataSet
-> CreateBlobStream(DBImage1-> DataSource->
DataSet-> FieldByName(DBImage1-> DataField),
bmRead);
TJPEGImage* Jpeg = new TJPEGImage;
Jpeg-> LoadFromStream(ms);
DBImage1-> Picture-> Bitmap-> Assign(Jpeg);
delete ms;
}