码迷,mamicode.com
首页 > 移动开发 > 详细

内存映射文件MemoryMappedFile使用

时间:2016-12-28 14:50:38      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:ble   tostring   memory   fan   dcl   png   contract   cli   serial   

参考资料: http://blog.csdn.net/bitfan/article/details/4438458

 

所谓内存映射文件,其实就是在内存中开辟出一块存放数据的专用区域,这区域往往与硬盘上特定的文件相对应。进程将这块内存区域映射到自己的地址空间中,访问它就象是访问普通的内存一样。

.NET中,使用MemoryMappedFile对象表示一个内存映射文件,通过它的CreateFromFile()方法根据磁盘现有文件创建内存映射文件,调用这一方法需要提供一个与磁盘现有文件相对应的FileStream对象。

 

需要保存的类:

技术分享
[Serializable]
    public class MyImg
    {
        public Image img;
        public string name;
    }
View Code

 

MMF定义:

技术分享
public class MMF
    {
        private MemoryMappedFile file = null;
        private MemoryMappedViewStream strem = null;
        private MemoryMappedViewAccessor acces = null;
        public MMF()
        {
            file = MemoryMappedFile.CreateOrOpen("myMMF", 1024 * 1024, MemoryMappedFileAccess.ReadWrite);
            strem = file.CreateViewStream();
            acces = file.CreateViewAccessor();
        }

        public void Write(int value)
        {
            acces.Write(0, value);
        }

        public int Read()
        {
            int value;
            acces.Read(0, out value);
            return value;
        }

        public void WriteClass(MyImg img)
        {
            IFormatter format = new BinaryFormatter();
            format.Serialize(strem, img);
        }

        public MyImg ReadClass()
        {
            IFormatter format = new BinaryFormatter();
            return format.Deserialize(strem) as MyImg;
        }
    }
View Code

 

界面代码:

技术分享
private void button1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dlg = new OpenFileDialog())
            {
                dlg.Filter = "*.png|*.png";
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    this.pictureBox1.Image = Image.FromFile(dlg.FileName);
                    this.label1.Text = Path.GetFileNameWithoutExtension(dlg.FileName);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MyImg img = new MyImg() { img = this.pictureBox1.Image, name = this.label1.Text };
            myFile.WriteClass(img);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MyImg img = myFile.ReadClass();

            this.pictureBox1.Image = img.img;
            this.label1.Text = img.name;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            label2.Text = myFile.Read().ToString();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            myFile.Write(int.Parse(this.textBox1.Text));
        }
View Code

 

 

参考资料: http://blog.csdn.net/bitfan/article/details/4438458

内存映射文件MemoryMappedFile使用

标签:ble   tostring   memory   fan   dcl   png   contract   cli   serial   

原文地址:http://www.cnblogs.com/gujf2016/p/6229384.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!