Memory-mapped files memorymappedfile using

Source: Internet
Author: User

Reference: http://blog.csdn.net/bitfan/article/details/4438458

The so-called memory map file, in fact, is to open up a piece of memory in the private area of the data, which often corresponds to a specific file on the hard disk. The process maps this area of memory to its own address space and accesses it as if it were accessing normal memory.

In the . NET, use the MemoryMappedFile object to represent a memory-mapped file, and through its createfromfile () method to create a memory-mapped file based on the disk's existing files, call this method to provide a corresponding to the disk's existing file The FileStream object.

Classes that need to be saved:

[Serializable]      Public class myimg    {        public  Image img;          Public string name;    }
View Code

MMF definition:

 Public classMMF {PrivateMemoryMappedFile file =NULL; PrivateMemorymappedviewstream Strem =NULL; PrivateMemorymappedviewaccessor acces =NULL;  PublicMMF () {file= Memorymappedfile.createoropen ("MYMMF",1024x768*1024x768, Memorymappedfileaccess.readwrite); Strem=file.            Createviewstream (); Acces=file.        Createviewaccessor (); }         Public voidWrite (intvalue) {acces. Write (0, value); }         Public intRead () {intvalue; Acces. Read (0, outvalue); returnvalue; }         Public voidwriteclass (myimg img) {iformatter format=NewBinaryFormatter (); Format.        Serialize (Strem, IMG); }         Publicmyimg ReadClass () {iformatter format=NewBinaryFormatter (); returnFormat. Deserialize (Strem) asmyimg; }    }
View Code

Interface code:

Private voidButton1_Click (Objectsender, EventArgs e) {            using(OpenFileDialog dlg =NewOpenFileDialog ()) {dlg. Filter="*.png|*.png"; if(DLG. ShowDialog () = =DialogResult.OK) { This. pictureBox1.Image =Image.FromFile (dlg.                    FileName);  This. Label1. Text =path.getfilenamewithoutextension (dlg.                FileName); }            }        }        Private voidButton2_Click (Objectsender, EventArgs e) {myimg img=NewMyimg () {img = This. picturebox1.image, name = This. Label1.            Text};        Myfile.writeclass (IMG); }        Private voidButton3_Click (Objectsender, EventArgs e) {myimg img=Myfile.readclass ();  This. pictureBox1.Image =img.img;  This. Label1. Text =Img.name; }        Private voidButton4_Click (Objectsender, EventArgs e) {Label2. Text=Myfile.read ().        ToString (); }        Private voidButton5_click (Objectsender, EventArgs e) {Myfile.write (int. Parse ( This. TextBox1.Text)); }
View Code

Reference: http://blog.csdn.net/bitfan/article/details/4438458

Memory-mapped files memorymappedfile using

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.