__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,0);
//把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,0);
//先读取图像流的大小
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 = 0;
Image1->Picture->Bitmap->LoadFromStream(ms);
//接着读取文本长度
FileRead(nFileHandle,&dw,sizeof(dw));
//然后把指定长度的内容写到Edit中
char *str = new char[dw*2];
FileRead(nFileHandle,str,dw);
str[dw]=0x0;
Edit1->Text = str;
cout<< dw <<endl;
delete str;
delete ms;
}
//---------------------------------------------------------------------------
原文链接: https://www.cnblogs.com/mypsq/p/5109321.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/226979
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!