zoukankan      html  css  js  c++  java
  • 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;
    }
    //---------------------------------------------------------------------------
  • 相关阅读:
    8.10
    今日头条笔试题 1~n的每个数,按字典序排完序后,第m个数是什么?
    Gym 100500B Conference Room(最小表示法,哈希)
    CodeForces 438D The Child and Sequence(线段树)
    UVALIVE 6905 Two Yachts(最小费用最大流)
    Gym Conference Room (最小表示法,哈希)
    hdu 2389 Rain on your Parade(二分图HK算法)
    Codeforces Fox And Dinner(最大流)
    zoj 3367 Counterfeit Money(dp)
    ZOJ3370. Radio Waves(2-sat)
  • 原文地址:https://www.cnblogs.com/mypsq/p/5109321.html
Copyright © 2011-2022 走看看