• 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,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;
    }
    //---------------------------------------------------------------------------
  • 相关阅读:
    vue强制更新$forceUpdate()
    js数组拼接成字符串,去除最后一个逗号
    JavaScript数组遍历:for、foreach、for in、for of、、().each的区别
    json.stringify()的妙用,json.stringify()与json.parse()的区别
    第四次博客作业-结对项目
    于达——第九次作业
    于达——第八次作业
    软件工程第三次作业——关于软件质量保障初探
    于达——第七次作业
    于达——第六次作业
  • 原文地址:https://www.cnblogs.com/mypsq/p/5109321.html
Copyright © 2020-2023  润新知