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