C++builder 图像字符流的存储和加载

时间:2023-03-09 00:14:06
C++builder 图像字符流的存储和加载
__fastcall TForm6::TForm6(TComponent* Owner)
: TForm(Owner)
{
#if 1 //for debug
AllocConsole();
AttachConsole( GetCurrentProcessId() ) ;
freopen( "CON", "w", stdout ) ;
#endif
Image1->Picture->LoadFromFile("HeadImage-UI/Photo-001.bmp");
strcpy(text,"图片信息.a");
}
//---------------------------------------------------------------------------
void __fastcall TForm6::LoadImageClick(TObject *Sender)
{ String strFileName = "123.bat";
//打开或创建目标文件
int nFileHandle;
if(FileExists(strFileName))
nFileHandle = FileOpen(strFileName,fmOpenWrite);
else
nFileHandle = FileCreate(strFileName); //定位到文件头
FileSeek(nFileHandle,0x0,); //把Image中的位图存入流中
TMemoryStream *ms = new TMemoryStream;
Image1->Picture->Bitmap->SaveToStream(ms); //先把图片流的大小写入文件中
DWORD dw = ms->Size;
FileWrite(nFileHandle,&dw,sizeof(dw));
//再把图片流吸入文件中
FileWrite(nFileHandle,ms->Memory,ms->Size);
//接着写入Edit文本的长度
dw = strlen(text);
cout<<dw<<endl;
FileWrite(nFileHandle,&dw,sizeof(dw));
//再把Edit的文本写入文件
FileWrite(nFileHandle,text,dw); delete ms;
FileClose(nFileHandle); Image1->Picture->Assign(NULL);
Edit1->Clear(); }
//---------------------------------------------------------------------------
void __fastcall TForm6::ShowImageClick(TObject *Sender)
{
String strFileName = "123.bat";
//打开图片流文件
int nFileHandle;
if(FileExists(strFileName))
nFileHandle = FileOpen(strFileName,fmOpenRead);
else
{
ShowMessage("File not find");
return;
}
//定位到文件头
FileSeek(nFileHandle,0x0,);
//先读取图像流的大小
DWORD dw;
FileRead(nFileHandle,&dw,sizeof(dw));
//根据图像流的大小,从文件中读取图像流
TMemoryStream *ms = new TMemoryStream;
byte *p = new byte[dw];
FileRead(nFileHandle,p,dw);
ms->Write(p,dw);
delete p; //把图像流中的位图显示到Image上面
ms->Position = ;
Image1->Picture->Bitmap->LoadFromStream(ms);
//接着读取文本长度
FileRead(nFileHandle,&dw,sizeof(dw)); //然后把指定长度的内容写到Edit中
char *str = new char[dw*];
FileRead(nFileHandle,str,dw);
str[dw]=0x0;
Edit1->Text = str;
cout<< dw <<endl;
delete str;
delete ms;
}
//---------------------------------------------------------------------------